How to get the main for handle in .NET

  • Thread starter Thread starter Fred Hebert
  • Start date Start date
F

Fred Hebert

I am trying to use a 3rd party DLL that requires the main window handle as
a parameter.

e.g. MyFunc(WHND MyHandle);

The example looks something like this:

Result = MyFunc(Handle);

Where "Handle" is the Win32 handle.

Trying to use the example as written results in a compiler error:
cannot convert parameter 5 from 'int' to 'HWND'

First isn't the HWND basically an unsigned integer? I have tried type
casting and several other things, but nothing the compiler likes.

Does anyone know how to make this work?
 
Is the call into unmanged code? (i.e. external declaration or assembly reference)
If it's an assembly reference, is it a managed wrapper around an unmanaged library?
First isn't the HWND basically an unsigned integer?
Yes, but it doesn't really matter if the bits your sending are low-order. This is because the int and uint both take up 32 bits of
memory. Once you start sending larger numbers, you may have to use "unchecked" code.

Either way, use the IntPtr class to wrap handles:

int myint = MainWindow.hWnd; // assuming hWnd returns a System.Int32.
IntPtr NativeRuntimeHandle = new IntPtr(myint);
 
int myint = MainWindow.hWnd; // assuming hWnd returns a
System.Int32.
IntPtr NativeRuntimeHandle = new IntPtr(myint);

OK I think we are going in the wrong direction. Here's the documentation
I dug up.

Win32 info since the DLL is a Win32 DLL...

HWND - Handle to a window

Usage example:
BOOL ShowWindow( HWND hWnd, int nCmdShow );

Drilling down a bit it appears that HWND is just an unsigned integer, not
some structure or anything.

OK now for the .NET application that is trying to use the DLL.

I created a form and put a button on it.

In the form create, I load the DLL and resolve the function references.

The button on click tries to call the function from the DLL.

The form, basically a default form:

public __gc class Form1 : public System::Windows::Forms::Form

has a property "Handle" the help text says:

[C++]
public: __property virtual IntPtr get_Handle();

Property Value
An IntPtr that contains the window handle (HWND) of the control.

Implements
IWin32Window.Handle

Remarks
The value of the Handle property is a Windows HWND. If the handle has
not yet been created, referencing this property will force the handle to
be created.

Finally, I have tried everything I can think of to get the compiler to
accept/convert the .NET form handle to a HWND. It's got me stumped.
 
I see what you mean.

The unmanaged declaration declares HWND, yet I'm suggesting to pass in a IntPtr.

The reason for this is simple:

The Marshaller recognizes an IntPtr as a "native handle". It will correctly marshal the "value" of the IntPtr into the uint that
the unmanaged code expects.

If your having trouble with the function, I highly doubt it has anything to do with the IntPtr --> HWND marshaling.

I have already implemented a managed ShowWindowAsync implementation that I've tested. It works fine. If this is the function your
having trouble with, you can examine my C# code and compare for differences:

[DllImport("user32.dll", SetLastError = true)]
private static extern bool ShowWindowAsync(IntPtr window, int cmd);


Calling it with the following parameters will be successful, in context of course:

IntPtr hWnd = MyWindow.Handle; // must be a valid window handle
int SW_SHOW = 5;

bool success = ShowWindowAsync(hWnd, SW_SHOW);

if (!success)
// Show the FormatMessage result of the last win32 error:
MessageBox.Show(this, new Win32Exception(Marshal.GetLastWin32Error()).Message);


Hope it helps

--
Dave Sexton
[email protected]
-----------------------------------------------------------------------
Fred Hebert said:
int myint = MainWindow.hWnd; // assuming hWnd returns a
System.Int32.
IntPtr NativeRuntimeHandle = new IntPtr(myint);

OK I think we are going in the wrong direction. Here's the documentation
I dug up.

Win32 info since the DLL is a Win32 DLL...

HWND - Handle to a window

Usage example:
BOOL ShowWindow( HWND hWnd, int nCmdShow );

Drilling down a bit it appears that HWND is just an unsigned integer, not
some structure or anything.

OK now for the .NET application that is trying to use the DLL.

I created a form and put a button on it.

In the form create, I load the DLL and resolve the function references.

The button on click tries to call the function from the DLL.

The form, basically a default form:

public __gc class Form1 : public System::Windows::Forms::Form

has a property "Handle" the help text says:

[C++]
public: __property virtual IntPtr get_Handle();

Property Value
An IntPtr that contains the window handle (HWND) of the control.

Implements
IWin32Window.Handle

Remarks
The value of the Handle property is a Windows HWND. If the handle has
not yet been created, referencing this property will force the handle to
be created.

Finally, I have tried everything I can think of to get the compiler to
accept/convert the .NET form handle to a HWND. It's got me stumped.
 
Back
Top