Solved

Window Handle vs. Process Handle

Posted on 2004-08-21
4
446 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 85

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 85

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now