471,337 Members | 835 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

How to get the main for handle in .NET

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?
Jul 21 '05 #1
3 5883
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);

--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"Fred Hebert" <fh*****@hotmail.com> wrote in message news:Xn*******************************@207.46.248. 16...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?

Jul 21 '05 #2
"Dave" <NO*********@dotcomdatasolutions.com> wrote in
news:uF**************@TK2MSFTNGP09.phx.gbl:
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.
Jul 21 '05 #3
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()).Messag e);
Hope it helps

--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"Fred Hebert" <fh*****@hotmail.com> wrote in message news:Xn*******************************@207.46.248. 16...
"Dave" <NO*********@dotcomdatasolutions.com> wrote in
news:uF**************@TK2MSFTNGP09.phx.gbl:
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.

Jul 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Bill D | last post: by
11 posts views Thread by objectref | last post: by
17 posts views Thread by Fred Hebert | last post: by
2 posts views Thread by Peter Row | last post: by
2 posts views Thread by Mahesh Devjibhai Dhola | last post: by
9 posts views Thread by DanielJohnson | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.