Jump to content

Using Handles in C# .NET 4.5


ch1234
 Share

Go to solution Solved by Richard Robertson,

Recommended Posts

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

  • Solution

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.

Link to comment
Share on other sites

  • 1 month later...

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

 

Link to comment
Share on other sites

  • 4 months later...

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
Link to comment
Share on other sites

 

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
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...