Search code examples
c#c++.netcomcom-interop

Pass managed IntPtr window handle parameter to C++ COM dll function expecting HWND


I'm creating a COM object that needs a HWND for a window handle coming from WPF (HwndHost). All I have is IntPtr which comes from HwndHost.Handle.

How do I pass this correctly to my COM object? I'm not using P/Invoke.

I don't care whether the conversion happens in .NET or C++, but prefer whichever is best practice.


Solution

  • Since type safety is out of the window already anyway, can't you just pass it as an int? A hwnd is just a 32-bit value (not sure if it's 64 bits on 64 bit Windows versions?). Pass it as an int to your COM object, cast it to an HWND there and you're good to go.