473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 6028
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bill D | last post by:
In a simple Windows forms application, the main form is called Form1. Within this form's class, I refer to the the running instance of this main class as "this" as in this.Text = "NEW TEXT" I...
11
by: objectref | last post by:
Hi to all, is there a way to get the window handle of the main window of an application or process ? For example, if someone opens Microsoft Word, he gets a window so he/she can write text....
17
by: Fred Hebert | last post by:
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...
5
by: Jay | last post by:
I have a situation where the user clicks on a button in a DataGrid to launch a popup window via javascript. In the popup window the user does some things that result in changes to the underlying...
2
by: Peter Row | last post by:
Hi, I am trying to draw on top of the main menu area of a form (this is just a test at the moment). I'm handling the forms paint event and using the following code: private void...
2
by: Mahesh Devjibhai Dhola | last post by:
Hi, I have one program, where i am using IO and Socket Asynchronous methods "BeginXXX" and "EndXXX". Many time, it happens that in Async delegate method, some exception occurs and if i dont...
2
by: iloveprincess | last post by:
Hi, I'm developing windows application using VB.Net 2005. I would like to send 'save' message using 'SendMessage' API to the excel appication. I've already got a handle of the excel window with...
9
by: DanielJohnson | last post by:
I am wondering where does the value returned by main goes. Suppoes main returns some number say 42, where is it stored. Supposed a shell script call a C program and the program returns the value...
4
by: urkel | last post by:
Hello everybody, I am getting bogged-down by these errors troubling me for the last few days I am writing a C program with a main and several user-defined functions. One of the user-defined...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.