Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / June 2007

Tip: Looking for answers? Try searching our database.

how to pass Form handle to unmanaged code

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gregory Khra - 26 Jun 2007 23:22 GMT
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
Hans-Gerd Sandhagen - 27 Jun 2007 06:06 GMT
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.


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.