Our COM component interface has the next method:
HRESULT CreatePluginWindow([in] HWND hParent, [in] RECT* prcView);
We are going to use it in .NET application but interop for this interface is looking like this:
void CreatePluginWindow(ref interop.alfrontx._RemotableHandle hParent, ref interop.alfrontx.tagRECT prcView)
According my investigation there is no way to use this method without unsafe code. I'd not like to change COM interface in order to use other than HWND type for hParent, because it's used in many C++ components. I'd not like to make changes in interop because they are compiled automatically on each build.
Is there any other way to solve this problem?
I think that you can convert an IntPtr to a _RemotableHandle using the following code:
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.
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:
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.