Solved

Window Handle vs. Process Handle

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 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

17 Experts available now in Live!

Get 1:1 Help Now