Gregory Khra schrieb:
> 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
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);
...
}
}
Gregory Khra - 27 Jun 2007 17:24 GMT
> Normally that should not be a problem.
I am getting a compiler error: cannot convert parameter ... from
'System::IntPtr' to 'HWND'. This error was the reason for my post.
Gregory
Mattias Sjögren - 27 Jun 2007 19:46 GMT
>I am getting a compiler error: cannot convert parameter ... from
>'System::IntPtr' to 'HWND'. This error was the reason for my post.
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

Signature
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.