我们的COM组件接口有下一个方法:
HRESULT CreatePluginWindow([in] HWND hParent, [in] RECT* prcView);我们将在.NET应用程序中使用它,但此接口的互操作如下所示:
void CreatePluginWindow(ref interop.alfrontx._RemotableHandle hParent, ref interop.alfrontx.tagRECT prcView)根据我的调查,没有不安全的代码是无法使用这种方法的。我不想为了使用hParent的HWND类型而更改COM接口,因为它在许多C++组件中使用。我不喜欢在互操作中进行更改,因为它们是在每次构建时自动编译的。
有没有其他方法来解决这个问题?
发布于 2010-11-02 20:25:55
这不是Tlbimp做的,这是在构建非托管COM服务器时由midl.exe注入的。窗口句柄是互操作的障碍,因为它在32位程序中是32位值,在64位程序中是64位值。就像所有的把手一样。
查看Windows SDK include目录中的wtypes.idl文件,您会发现其中声明了_RemotableHandle。再往下一点,你会看到#define将HWND映射到一个RemotableHandle:
#ifndef _MIDL_DECLARE_WIREM_HANDLE
DECLARE_WIREM_HANDLE( HWND );
// etc..
#endif据我所知,当您将/D _MIDL_DECLARE_WIREM_HANDLE传递给midl.exe时,您将获得一个没有RemoteHandle包装器的类型库。诚然,我真的不明白这是如何工作的。
发布于 2011-10-18 01:08:42
我认为您可以使用以下代码将IntPtr转换为_RemotableHandle:
interop.alfrontx._RemotableHandle HWNDtoRemotableHandle(IntPtr handle)
{
return (interop.alfrontx._RemotableHandle)Marshal.PtrToStructure(handle, typeof(interop.alfrontx._RemotableHandle));
}因此,您可以很容易地使用上面的函数从IntPtr中获取_RemotableHandle,并且不需要在.NET程序集中更改类型。
https://stackoverflow.com/questions/4077326
复制相似问题