How to get a Form handle?

Ask general questions here.
User avatar
Aracknid
Posts: 342
Joined: Tue Aug 10, 2010 3:23 pm
Location: Toronto, Ontario, Canada

How to get a Form handle?

Post by Aracknid » Wed Jul 13, 2011 10:00 pm

I saw this post: http://www.ranorex.com/forum/post3169.html?hilit=get form handle#p3169

And I tried the code as follows (note BrowserWin is a Form element):

Dim hwnd as long
hwnd = cint(BrowserWin.Element.GetAttributeValue("Handle").tostring)

And it gives me a number, but not the same value I saw in Spy.

How can I get the handle number I saw in Spy? I saw that it returns intprt, but I just assummed I could convert that to a long and get the same thing....

Thanks,

Aracknid

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: How to get a Form handle?

Post by Support Team » Thu Jul 14, 2011 2:46 pm

Aracknid wrote:I saw this post: post3169.html?hilit=get form handle#p3169
I added a new post to that forum thread, since the code you copied from there is not what we at Ranorex recommend (you can do it that way, but it is not as easy as the recommended way :D ). Basically, you should always use the Ranorex adapter classes to get attribute values, since then you don't need to use the attribute names a strings:
Ranorex.Form form = ...; // your code to get the form
Ranorex.NativeWindow window = new Ranorex.NativeWindow(form);
IntPtr handle = window.Handle;
See following section in the Ranorex User Guide for more information on Ranorex adapters:
http://www.ranorex.com/support/user-gui ... apter.html
Aracknid wrote:And it gives me a number, but not the same value I saw in Spy.
If you get the handle from the same element, it will give you the same value. Just make sure that you use the same element and be aware that the window handle changes every time the window is created.
Aracknid wrote:I saw that it returns intprt, but I just assummed I could convert that to a long and get the same thing....
If you need to get the value as a long, you can do so, but both handleLong.ToString() and handle.ToString() will give you the same value.
long handleLong = handle.ToInt64();
Regards,
Alex
Ranorex Team
.
Image