all groups > dotnet interop > june 2007 >
You're in the

dotnet interop

group:

how to pass Form handle to unmanaged code


how to pass Form handle to unmanaged code Gregory Khra
6/26/2007 3:22:06 PM
dotnet interop:
How can I pass a Windows Form handle to unmanaged DLL that expects HWND?
Even if I have a wrapper calss between managed C# and unmanaged C++, IntPtr
does not by default cast to HWND. Should I force it? Can somebody give an
example?
Gregory
Re: how to pass Form handle to unmanaged code Hans-Gerd Sandhagen
6/27/2007 12:00:00 AM
Gregory Khra schrieb:
[quoted text, click to view]

Normally that should not be a problem. Example:

C++ DLL:

#include <windows.h>

void NativeFct (HWND hWnd);




C# Wrapper:

class NativeMethods {
[DllImport("my.dll", EntryPoint = "NativeFct")]
public static extern void NativeFct(IntPtr hWnd);
}


C# Form:

class MyForm : Form {
void MyFunc () {
...
NativeMethods(this.Handle);
...
}
Re: how to pass Form handle to unmanaged code Gregory Khra
6/27/2007 9:24:03 AM
[quoted text, click to view]

I am getting a compiler error: cannot convert parameter ... from
'System::IntPtr' to 'HWND'. This error was the reason for my post.
Re: how to pass Form handle to unmanaged code Mattias Sjögren
6/27/2007 8:46:03 PM
[quoted text, click to view]

Then I guess you're using C++ even though your original post said C#.

In C++ you want to call IntPtr::ToPointer and then cast the returned
void* to HWND.


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
AddThis Social Bookmark Button