Jump to content
Sign in to follow this  
Guest mnasgowitz

Got ProcessId but need window handle.

Recommended Posts

Guest mnasgowitz

First off let me say that the AutoItX3.Control is great and has almost provided me everything that I need.

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

Share this post


Link to post
Share on other sites

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

<{POST_SNAPBACK}>

Hello 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

Share this post


Link to post
Share on other sites

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.....


[u]Do more with pre-existing apps![/u]ANYGUIv2.8

Share this post


Link to post
Share on other sites

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.....

<{POST_SNAPBACK}>

Thankx quaizywabbit,

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

Share this post


Link to post
Share on other sites
Guest mnasgowitz

It seems that getting all the window handles and then looking for the ones with the PID will work. When doing this I find that more then one handle could be related to a PID. It makes sense since an application could have more then one window displayed at a time.

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

Share this post


Link to post
Share on other sites

Example in c++:

#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

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...