Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Window Handle vs. Process Handle

Posted on 2004-08-21
4
Medium Priority
?
459 Views
Last Modified: 2008-01-09
Hi all,

I am starting a process like this:

    Dim pInfo As New ProcessStartInfo
    Dim p As Process

     pInfo.FileName = "C:\Program Files\Internet Explorer\iexplore.exe"
     pInfo.Arguments = "http://www.yahoo.com"

        'Start the process.
        p = Process.Start(pInfo)

        'MessageBox.Show(p.Handle.ToString())

Works fine to start the process with IE and Yahoo's homepage in it.  Then I need to get the IE window handle.  I use FindWindwEx to find that window based on the class and caption of the window.  But I'd like to be able to reference the window handle directly and not have to seach for it with FindWindowEx.  I will have a number of them up at a time and I might have different instances of the IEFrame class but it is possible the captions might be the same.

So my question is...is the process handle the same as the window handle ?  If not, how can I get the window handle based on the process handle ?

Thanks,

John


0
Comment
Question by:trzesniakj
  • 2
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 11860209
The process handle is not the same as the window handle.  A process is simply a piece of running code and does not necessarily have a user interface with an associated window handle.  The process handles is what the operating system uses internally to manage the process.

To get the handle for a process that has a window, use code such as:

    Dim pInfo As New ProcessStartInfo
    Dim p As Process

    pInfo.FileName = "C:\Program Files\Internet Explorer\iexplore.exe"
    pInfo.Arguments = "http://www.yahoo.com"

    'Start the process.
    p = Process.Start(pInfo)

    ' wait for the app to completely load
    ' and thus have a window handle
    While Not p.WaitForInputIdle()
        Application.DoEvents()
    End While

    ' display the window handle
    MsgBox(p.MainWindowHandle.ToString)

Regards,

Idle_Mind
0
 
LVL 4

Expert Comment

by:Daniellus83
ID: 11860366

Dear John,.. it took me a while to figure out how to get it.. but it turns out to be quit simple:

        MessageBox.Show(p.Id)

If you use this, you get the Windows PID. This is to ONLY unique known identification throughout the system:

----
"....An application can obtain a handle to a process that can be used as a parameter to many process-information and control functions.
   You can use this handle to   initialize a WaitHandle or to call native methods with platform invoke.

   This process handle is private to an application--in other words, process handles cannot be shared. A process also has a process Id which, unlike the Handle,
   is unique and, therefore, valid throughout the system.

   Only processes started through a call to Start set the Handle property of the corresponding Process instances. .."
----


So if you then want to get the handles you should use:
----------------------------------------------------------------------------
        Dim pInfo As New ProcessStartInfo
        Dim p, p_ie As Process

        pInfo.FileName = "I:\Program Files\Internet Explorer\iexplore.exe"
        pInfo.Arguments = "http://www.yahoo.com"

        'Start the process.
        p = Process.Start(pInfo)

        Dim Processes() As Process
        Processes = Process.GetProcesses()

        For Each p_ie In Processes
            If p_ie.Id = p.Id Then
                MessageBox.Show(" (P)ID = " & p_ie.Id & ", MainWindowHandle = " & p_ie.MainWindowHandle.ToString & " and Handle = " & p_ie.Handle.ToString)
            End If
        Next
----------------------------------------------------------

Hopes it helps you...
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 11860489
Daniellus83,

I already answered the question.  As in my code, it is simply:

    p.MainWindowHandle

Furthermore, in your code, the For Each loop is completely unnecessary since you started the process.  Why go through every process in memory trying to find one that you already have a reference to?!

Also, you must wait for the application to finish loading to ensure that you have a window handle to retrieve.  That is the purpose of the While loop in my code.

    While Not p.WaitForInputIdle()
        Application.DoEvents()
    End While

Regards,

Idle_Mind
0
 
LVL 4

Expert Comment

by:Daniellus83
ID: 11860553
Dear Idle_Mind; you are right; I was typing and than called for dinner... ;-/ I continued but you already got it.

And you are indeed right about the loop... I put it elsewhere under a button. That was also my 'wait-for-idle' method... Nevermind you win this competition.. ;~)

But I can give you an advice (that is calculated into my solution with the for-loop ;-)):

Use a try-catch statement to get the Id/handle... Since the user can close (by accident) the app...

Daniël
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question