ch1234 Posted May 7, 2013 Posted May 7, 2013 (edited) 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 ") EndIfEdit: 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 May 7, 2013 by ch1234
JohnOne Posted May 8, 2013 Posted May 8, 2013 (edited) 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 May 8, 2013 by JohnOne AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans.
Richard Robertson Posted May 9, 2013 Posted May 9, 2013 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.
ch1234 Posted May 10, 2013 Author Posted May 10, 2013 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?
JohnOne Posted May 10, 2013 Posted May 10, 2013 Well the fact that the help file does not mention it can take a handle is the main reason. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans.
Solution Richard Robertson Posted May 12, 2013 Solution Posted May 12, 2013 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. ch1234 1
c.haslam Posted June 14, 2013 Posted June 14, 2013 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
Richard Robertson Posted June 16, 2013 Posted June 16, 2013 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.
anrapas Posted November 11, 2013 Posted November 11, 2013 (edited) 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 November 11, 2013 by anrapas
Richard Robertson Posted November 14, 2013 Posted November 14, 2013 (edited) 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 November 14, 2013 by Richard Robertson
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now