Solved

Window Handle vs. Process Handle

Posted on 2004-08-21
4
450 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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