Jump to content
Sign in to follow this  
ch1234

Using Handles in C# .NET 4.5

Recommended Posts

ch1234

I'm trying to create a C# App with some AutoIt code and I can't get the Handles working. I'm using the basic AutoIt handle example and it won't run correctly. Are there any glaring omissions in my code?

AutoItX3Lib.AutoItX3 autoit = new AutoItX3Lib.AutoItX3();
        
string handle = autoit.WinGetHandle("[CLASS:Notepad]", "this one");
// MessageBox.Show(handle, "");
autoit.WinActivate(handle);
autoit.ControlSend(handle, "", "Edit1", " test test test ");

I've registered the dll etc and other AutoIt commands such as Send and Sleep are working as expected so I don't understand what's wrong here. I want the notepad window to be brought to the front (ie be the active window) and then send the text to it. If I enable the MessageBox, it returns the correct hex code for the handle.

This is the AutoIt code I've adapted:

; Identify the Notepad window that contains the text "this one" and get a handle to it
; Get the handle of a notepad window that contains "this one"
Local $handle = WinGetHandle("[CLASS:Notepad]", "this one")
If @error Then
    MsgBox(4096, "Error", "Could not find the correct window")
Else
WinActivate($handle)
    ; Send some text directly to this window's edit control
    ControlSend($handle, "", "Edit1", " AbCdE ")
EndIf

Edit: I've also tried using WinTitleMatchMode and that doesn't seem to have an effect.

autoit.Opt("WinTitleMatchMode", 4);

Edit 2: I compiled the AutoIt file into an exe and then called that from my C# and it works as expected. I'd rather have it all in the one file but it'll have to do if I can't get it working the way I want.

I guess the problem is that I'm taking the handle and storing it as a string and then passing that string back in as the title for WinActivate/ControlSend - so it's looking for a notepad file with the handle string as its title.

Is there an easy way to get a window title from a handle or pass a handle through instead of a title?

Edited by ch1234

Share this post


Link to post
Share on other sites
JohnOne

autoit.WinActivate("[CLASS:Notepad]", "this one");

autoit.ControlSend("[CLASS:Notepad]", "this one", "Edit1", " test test test ");

EDIT:

Sorry, I see you are just trying to get handles working.

But I'm not sure WinActivate in AutoItx accepts handles, so I'd try to get that question answered first.

In any case, you are not passing it a handle, you are passing it a string representation of a handle, so you'd have to find a native c# method of converting that string representation to a handle proper, before you got the result you expect, if indeed it will accept a handle.

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
Richard Robertson

No, you have to pass it a string. Passing it an actual window handle will cause the dll to try to read it as a string resulting in undefined behavior which will most likely cause a program crash.

Share this post


Link to post
Share on other sites
ch1234

EDIT:

Sorry, I see you are just trying to get handles working.

But I'm not sure WinActivate in AutoItx accepts handles, so I'd try to get that question answered first.

 

 

What makes you think it can't accept handles? (I'm genuinely curious. I'm pretty much a novice at AutoIt/AutoItX).

 

No, you have to pass it a string. Passing it an actual window handle will cause the dll to try to read it as a string resulting in undefined behavior which will most likely cause a program crash.

 

Ok, so I'm already passing it a string. Can you see any reasons/incorrect assumptions on my part as to why it won't work?

Share this post


Link to post
Share on other sites
Richard Robertson

I had forgotten about this. To use the handle, you'll actually call it like this.

AutoItX3Lib.AutoItX3 autoit = new AutoItX3Lib.AutoItX3();
        
string handle = autoit.WinGetHandle("[CLASS:Notepad]", "this one");
// MessageBox.Show(handle, "");
autoit.WinActivate(handle);
autoit.ControlSend("[HANDLE:" + handle + "]", "", "Edit1", " test test test ");

 

AutoItX uses a slightly different mechanism than normal AutoIt.

  • Like 1

Share this post


Link to post
Share on other sites
ch1234

Fantastic. Thanks a million!

Share this post


Link to post
Share on other sites
c.haslam

Richard,

I see in the code you offer, you use the string handle for WinActivate(). Should I use the string handle for WinGetPosX(), WinGetPosY(), WinGetPosWidth() and WInGetPosHeight() ? I am getting inconsistent results in PerfectScript. Or should I be coding

"[HANDLE:" + handle + "]"

I have much else working well using the COM interface in PerfectScript.


Spoiler

CDebug Dumps values of variables including arrays and DLL structs, to a GUI, to the Console, and to the Clipboard

 

Share this post


Link to post
Share on other sites
Richard Robertson

Yes, you need to pass it in a string in the form "[HANDLE:" + handle + "]" where the handle variable is a string returned from AutoItX's WinGetHandle function.

AutoItX is just weird that way.

Share this post


Link to post
Share on other sites
anrapas

I'm trying to use this way but with the handle provided by 
 

[DllImport("user32.dll", EntryPoint = "EnumDesktopWindows", ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumDelegate lpEnumCallbackFunction, IntPtr lParam);
 
but it does not work properly. Is there any difference between this handle and the one provided by AutoItX's WinGetHandle function?
 
Thanks a lot.
Edited by anrapas

Share this post


Link to post
Share on other sites
Richard Robertson

 

I'm trying to use this way but with the handle provided by 

 

[DllImport("user32.dll", EntryPoint = "EnumDesktopWindows", ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumDelegate lpEnumCallbackFunction, IntPtr lParam);
 
but it does not work properly. Is there any difference between this handle and the one provided by AutoItX's WinGetHandle function?
 
Thanks a lot.

 

When you say "does not work properly" what do you mean? This function doesn't return a handle, but your delegate will get called with child window handles.

Also, where are you trying to use the handle?

Edited by Richard Robertson

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×