473,763 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to get the main forM 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?
Nov 17 '05 #1
17 2270
"Fred Hebert" <fh*****@hotmai l.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);
Typo? You mean this?

MyFunc(HWND 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, it is always a good idea to post code exactly as written; the devil
is in the details.

This is a guess: If you must, cast the 5th parameter:

change ...(... x, ...);

to

...(... (HWND) x, ...);
First isn't the HWND basically an unsigned integer?
Using Von Neumann machines, everything is basically an integer. So, what?
:-)
I have tried type casting and several other things,
but nothing the compiler likes.


As I said, show the bad code, the exact text of the error message and what
you have tried.

Regards,
Will
Nov 17 '05 #2
Sorry about the typo, was just trying to simplify.

It's pulled from various places, but here is the actual code:

typedef int (_stdcall *pOmniConnect) (char * IpAddress,
unsigned int Port,
unsigned int Timeout,
unsigned char EncryptionKey[16],
HWND NotifyWindow);
....
pOmniConnect OmniConnect;
....
The dll is loaded and addresses resolved (not relevant to problem)
....
OmniConnect(IpE dit->Text,
PortEdit->Text->ToInt32,
TimeoutEdit->Text->ToInt32,
BinKey,
Handle); // this line is the problem
As Written:
error C2664: 'int(... the func template ...)': cannot convert parameter 5
from 'int' to 'HWND'

Tried type casting e.g. (HWND)Handle);
error C2440" 'type cast' : cannot convert from 'System::IntPtr ' to 'HWND'

Tried various this->Handle, and Handle->xxx conversions but similar
problems.
Nov 17 '05 #3
"Fred Hebert" <fh*****@hotmai l.com> wrote in message
news:Xn******** *************** ********@207.46 .248.16...
Sorry about the typo, was just trying to simplify.

It's pulled from various places, but here is the actual code:

typedef int (_stdcall *pOmniConnect) (char * IpAddress,
unsigned int Port,
unsigned int Timeout,
unsigned char EncryptionKey[16],
HWND NotifyWindow);
...
pOmniConnect OmniConnect;
...
The dll is loaded and addresses resolved (not relevant to problem)
...
OmniConnect(IpE dit->Text,
PortEdit->Text->ToInt32,
TimeoutEdit->Text->ToInt32,
BinKey,
Handle); // this line is the problem
As Written:
error C2664: 'int(... the func template ...)': cannot convert parameter 5
from 'int' to 'HWND'

Tried type casting e.g. (HWND)Handle);
error C2440" 'type cast' : cannot convert from 'System::IntPtr ' to 'HWND'

Tried various this->Handle, and Handle->xxx conversions but similar
problems.


How is Handle declared?

Regards,
Will
Nov 17 '05 #4
"William DePalo [MVP VC++]" <wi***********@ mvps.org> wrote in
news:uC******** ******@TK2MSFTN GP14.phx.gbl:

How is Handle declared?

Regards,
Will


It's not in my code. It should be the handle of the main form.

According to the docs:
[C++]
public: __property virtual IntPtr get_Handle();

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

Implements
IWin32Window.Ha ndle

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.

----

And Handle.get_Hand le() doesn't work either.
Nov 17 '05 #5
"Fred Hebert" <fh*****@hotmai l.com> wrote in message
news:Xn******** *************** ********@207.46 .248.16...
"William DePalo [MVP VC++]" <wi***********@ mvps.org> wrote in
news:uC******** ******@TK2MSFTN GP14.phx.gbl:
According to the docs:
[C++]
public: __property virtual IntPtr get_Handle();

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

Implements
IWin32Window.Ha ndle

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.


I have no idea why a window handle should be returned as an IntPtr. <Sigh>

Does this IntPtr have a ToInt32() method? If so, can you use it to get
yourself an integer and cast that to a window handle?

Regards,
Will
Nov 17 '05 #6
Hi Will,
I have no idea why a window handle should be returned as an IntPtr. <Sigh>
Because IntPtr represents a platform-specific pointer size (meaning, it
holds a 64-bit value on 64-bit platforms with the 64-bit version of the
framework).
Does this IntPtr have a ToInt32() method? If so, can you use it to get
yourself an integer and cast that to a window handle?


It does, but a better option is to use IntPtr::ToPoint er() which returns a
void* :)

So you can just do (HWND)Handle.To Pointer();

--
Tomas Restrepo
to****@mvps.org
Nov 17 '05 #7
Hi Tomas,
Because IntPtr represents a platform-specific pointer size (meaning, it
holds a 64-bit value on 64-bit platforms with the 64-bit version of the
framework).


Oh. :-)

Geez, just what I need, something else new to get up to speed on.

Regards,
Will
Nov 17 '05 #8
Hi Will,
Oh. :-)

Geez, just what I need, something else new to get up to speed on.


Jajajajaa. Well, in that sense, it's not all that different (source code
wise) from using ULONG_PTR and friends instead of ULONG :)
--
Tomas Restrepo
to****@mvps.org
http://www.winterdom.com/
Nov 17 '05 #9
"Tomas Restrepo \(MVP\)" <to****@mvps.or g> wrote in
news:us******** ******@TK2MSFTN GP15.phx.gbl:

So you can just do (HWND)Handle.To Pointer();


Sounds good, but neither ToInt32 or ToPointer works, and in fact they both
return EXACTLY the same error.

error C3374: managed function 'System::IConve rtible::ToInt32 ' requires
argument list

Any more ideas? This is really frustrating because I am sure it is
something simple.
Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6052
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 "Handle" is the Win32 handle.
1
2203
by: Bill Strass | last post by:
Problem: Access main form/subform as front end to SQL Server backend. Add/edit via subforms work fine. Not so with main form. Set up a master-detail form-subform using two views linked from SQL Server. The subform allows inserts an updates, while the main form will not. The only solution I can think of is to recreate using tables imported from SQL Server, so that the data is all local. This is despite the fact that the views in SQL...
4
7018
by: Dave Boyd | last post by:
Hi, I have two very similar forms each with a subform. The main form gets a few fields from the user and passes this back to a query that the subform is bound to. The requery is done when the user enters the last qualifying field on the main form. In one case this works fine, the subform shows the data the user wants to update -- which means showing all the data put in previously (ie showing this via the requery and the continuous...
2
12045
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 want to do something like change this Text on the Form1 window from within another class. Trying Form1.Text = "TEXT FROM CLASS" or Form1.ActiveForm.Text = "TEXT FROM CLASS"
4
3008
by: Grant Schenck | last post by:
I have a C# form application. Once my main window is showing I want to pop-up a login type dialog. What event would I trap to affect this? I tried handling the form load but the main form window is not yet visible then. Thanks! Grant Schenck
3
3085
by: Robert W. | last post by:
I have a separate thread that handles data transfer operations (with a mobile device). I've set things up so that a small notification form is displayed in the lower right corner of the window. This all works fine. But what I can't figure out how to do is to get it to display on top of my main form, if the main form is maximized. I've tried all of the following: statusForm = new frmNotify(); statusForm.Show(); // Display...
0
1219
by: Rajiv Das | last post by:
Environment: Visual Studio 2005, Beta 2 ..Net 2.0 Windows XP, SP2 C# Generics ------------------------------- Hi, I have a Windows Form whose contents I would like to dynamically change. I
5
3997
by: Ivan | last post by:
I am used to VB6 and am not sure how to do this in Vstudio .NET. I have a main form which calls other forms. I want to disable that main form while other ones are called. I tried hiding it and creating a new instance of the main form when returning to it but than my application is just creating more forms. How do I hide the main form and return back to it when exiting another form?
2
2389
by: campos | last post by:
Hi all, I ran into a headache problem. I have a windows form with a progress bar on it. Then I new a thread to do calculation for a long time. I want the progress bar to show the calculation progress. So I use a shared variable in calculation thread to allow main thread read it periodically in order to show the progress. Quesion comes out. If I just wait and don't make any operation during the whole calculation, the main form is OK,...
0
9386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9998
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3523
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.