使用 INT_PTR 的 IDL 声明的 C# 实现
我们在 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。
任何帮助将不胜感激。 谢谢
We have a method declared in an idl file like this:
[id(7), helpstring("method SomeFunction")] HRESULT SomeFunction([in] INT_PTR windowHandle, [out, retval] VARIANT_BOOL* dlgResult);
We do the implementation of it in C# with this method:
public bool SomeFunction(int windowHandle)
That works fine when building 32 bit, however when we build x64 the types don't match as the idl is looking for a 64 bit value and the c# expects a 32 bit value.
Using IntPtr in the C# doesn't work because the code needs a long or an int in c#.
Is there any type i can use in the c# method that will compile to long or int depending on the platform?
I'd rather not have to duplicate code by having the function twice, and i'm not sure we can change the IDL to just use Int64 due to backwards compatibility.
Any help would be appreciated.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
进入“项目属性...”并进入“构建”选项卡。
在那里,您可以找到“条件编译符号”,为您的 x64 平台添加 WIN64。节省。
对于“AnyCPU”,您可能想要删除该平台...
然后在代码使用中:
但是我认为您可以使用 IntPtr 并根据其 Size 属性将其值转换为 int/long...
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:
However I think you can use the IntPtr and convert its value to an int/long depending on its Size property...
您确定
IntPtr
不适合您吗?IntPtr
结构具有一个采用Int32
的构造函数重载,以及另一个采用Int64
的构造函数重载。此外,它还有一个ToInt32()
方法和一个ToInt64()
方法。你的代码会像这样工作:Are you sure
IntPtr
wouldn't work for you? TheIntPtr
structure has a constructor overload that takes anInt32
, and another that takes anInt64
. Additionally, it has aToInt32()
method and aToInt64()
method. Your code would work something like this: