tlbimp.exe converts HWND to _RemotableHandle

試著忘記壹切 提交于 2019-12-09 04:24:28

It is not Tlbimp that does this, this was injected by midl.exe when you built the unmanaged COM server. A window handle is an interop obstacle because it is a 32-bit value in a 32-bit program and a 64-bit value in a 64-bit program. Like all handles.

Take a look at the wtypes.idl file in the Windows SDK include directory, you'll find _RemotableHandle declared there. A bit further down, you'll see the #define that maps the HWND to a RemotableHandle:

#ifndef _MIDL_DECLARE_WIREM_HANDLE
DECLARE_WIREM_HANDLE( HWND );
// etc..
#endif

As near as I can tell, when you pass the /D _MIDL_DECLARE_WIREM_HANDLE to midl.exe then you'll get a type library without the RemoteHandle wrapper. Admittedly I don't really understand how this is supposed to work.

I think that you can convert an IntPtr to a _RemotableHandle using the following code:

    interop.alfrontx._RemotableHandle HWNDtoRemotableHandle(IntPtr handle)
    {
        return (interop.alfrontx._RemotableHandle)Marshal.PtrToStructure(handle, typeof(interop.alfrontx._RemotableHandle));
    }

So, you can easily use the above function to get the _RemotableHandle from a IntPtr and there is no need to change the type inside the .NET assembly.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!