Got ProcessId but need window handle.
#1
Guest_mnasgowitz_*
Posted 03 May 2005 - 05:23 PM
So I have the following needs
1. Ability to launch an application.
2. Ability to manage the state of the launched application window.
I understand this ok,
processId = oAutoIt.Run(sApplication);
.
oAutoIt.ProcessExists(processId)
.
oAutoIt.ProcessClose(processId);
The problem is that I also need to control the application window and to do that you must have the window handle. I know that you can use the title but that can change and the class does not seem to be unique enough to distinguish which application is the one I launched. I am looking for a unique reference to the application that allows me to control it and that seems to be the handle.
I know I can do
var handle = oAutoIt.WinGetHandle(title, "")
But there I go again using a title and what I really want is the handle by way of the processId I got when I launched the application.
BTW, I have played with stuff like
var e = new Enumerator( GetObject( "winmgmts:" ).InstancesOf( "Win32_process"));
but it does not seem to get me any closer.
Any suggestions how to make this robust or am I missing something simple?
- Mark
#2
Posted 05 May 2005 - 07:35 PM
Hello Mark,First off let me say that the AutoItX3.Control is great and has almost provided me everything that I need.
...
Any suggestions how to make this robust or am I missing something simple?
- Mark
AutoItX has indeed no function to retrieve the Window-handle from a given window.
I guess the author assumed that the programming language you are working on has much better capabilities to do such things.
You could call the Windows 'FindWindow' API function to retrieve the window handle, given the window name. It ony depends on your programming language how to format this command (example given in C++):
HWND FindWindow(LPCTSTR lpClassName, LPCTSTR lpWindowName );
See also:
http://msdn.microsoft.com/library/en-us/wi.../findwindow.asp
Regards,
-Sven
#3
Posted 05 May 2005 - 10:28 PM
Hello Mark,
AutoItX has indeed no function to retrieve the Window-handle from a given window.
it does...but I think what he's asking is how to convert a pid to a window handle.
I know I've seen something like this posted on here before, but I can't find it.....
#4
Posted 06 May 2005 - 08:07 AM
Thankx quaizywabbit,it does...but I think what he's asking is how to convert a pid to a window handle.
I know I've seen something like this posted on here before, but I can't find it.....
I guess I need to go back to school to learn READING :-)
Anyway, here is some code to retrieve the Window Handle, given the PID. It is in written in VB.
The trick is to use FindWindow() without parameters, walk to every open window found, retrieve the PID using GetWindowThreadProcessId(). If the PID is equal to the one you are looking for, then you have found the window.
' Return the window handle for an instance handle. Private Function InstanceToWnd(ByVal target_pid As Long) As _ Long Dim test_hwnd As Long Dim test_pid As Long Dim test_thread_id As Long ' Get the first window handle. test_hwnd = FindWindow(ByVal 0&, ByVal 0&) ' Loop until we find the target or we run out ' of windows. Do While test_hwnd <> 0 ' See if this window has a parent. If not, ' it is a top-level window. If GetParent(test_hwnd) = 0 Then ' This is a top-level window. See if ' it has the target instance handle. test_thread_id = _ GetWindowThreadProcessId(test_hwnd, _ test_pid) If test_pid = target_pid Then ' This is the target. InstanceToWnd = test_hwnd Exit Do End If End If ' Examine the next window. test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT) Loop End Function
Regards,
-Sven
#5
Guest_mnasgowitz_*
Posted 10 May 2005 - 01:48 PM
I think this would be a nice addition to AutoItX but then again I might be doing something a little off the wall.
Thanks for your help and the learning experience!
-Mark
Edited by mnasgowitz, 10 May 2005 - 01:49 PM.
#6
Posted 13 August 2005 - 10:29 AM
#include <windows.h> #include "autoit3.h" int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { HANDLE hProcess; DWORD dProcId; // Get process id. dProcId = AU3_ProcessWait("notepad.exe",0); // Get handle from process id. hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dProcId); return 0; }
Just replace the commands with the ones from your programming lang of choice.
Edited by AznSai, 13 August 2005 - 11:47 AM.
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users





