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")
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?
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.
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.
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.
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.
[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.
[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?