0

我们在 idl 文件中声明了一个方法,如下所示:

[id(7), helpstring("method SomeFunction")] HRESULT SomeFunction([in] INT_PTR windowHandle, [out, retval] VARIANT_BOOL* dlgResult);

我们用这个方法在 C# 中实现它:

public bool SomeFunction(int windowHandle)

这在构建 32 位时工作正常,但是当我们构建 x64 时,类型不匹配,因为 idl 正在寻找 64 位值,而 c# 需要 32 位值。

在 C# 中使用 IntPtr 不起作用,因为代码在 C# 中需要 long 或 int。

我可以在 c# 方法中使用任何类型,根据平台编译为 long 或 int 吗?

我宁愿不必通过两次使用该函数来复制代码,而且由于向后兼容性,我不确定我们是否可以将 IDL 更改为仅使用 Int64。

任何帮助,将不胜感激。谢谢

4

2 回答 2

0

Go into Project Properties... and to the Build tab.

In there, you can find "Conditional compilation symbols", add WIN64 for your x64 platform. Save.

For "AnyCPU", you probably want to remove that platform...

Then in code use:

#if WIN64
     long handle;
     public bool SomeFunction(long windowHandle)
#else
     int handle;
     public bool SomeFunction(int windowHandle)
#endif

However I think you can use the IntPtr and convert its value to an int/long depending on its Size property...

于 2011-04-26T13:00:14.257 回答
0

你确定IntPtr不适合你吗?该IntPtr结构有一个构造函数重载,它接受一个Int32,另一个接受一个Int64。此外,它有一个ToInt32()方法和一个ToInt64()方法。您的代码将像这样工作:

public bool SomeFunction(IntPtr windowHandle)
{
    // Implementation here.
    // If you need the value of the pointer in the implementation,
    // you can use:
    // long actualValue = windowHandle.ToInt64();
}

long actualHandle = 1234;
IntPtr handlePtr = new IntPtr(actualHandle);
bool returnValue = SomeFunction(windowHandle);
于 2011-04-26T14:13:31.063 回答