| re: Excel Process ID
Thanks Tom!!
Your Idea worked! I had to modify your code alittle bit to make it
work. The Modified code is as follows:
Private Declare Function GetWindowThreadProcessId Lib "user32" ( _
ByVal hWnd As Integer, _ <-- Needed to be changed to an Integer
ByRef lpdwProcessId As IntPtr) As IntPtr
' create the excel instance...
Dim exl As Excel.Application = New Excel.Application
' let's get it's process!
Dim processId As IntPtr
' we ignore the return value, since we don't care
' about the thread id...
GetWindowThreadProcessId(exl.Hwnd, processId)
Dim excelProcess As Process =
Process.GetProcessById(processId.ToInt32())
Debug.WriteLine(processId)
excelProcess.Kill()
I am now able to terminate the correct EXCEL process from within my
code if excel does not close properly. Thanks again Tom, You the
man!
Powaguy
Tom Shelton <tom@YOUKNOWTHEDRILLmtogden.com> wrote in message news:<15vlg6xhimm33$.1wdlesv3erh7v.dlg@40tude.net> ...[color=blue]
> On 31 Aug 2004 18:16:11 -0700, Powerguy wrote:
>[color=green]
> > Hi all,
> >
> > I am looking for a way to get the Process id (or a handle) of an EXCEL
> > process created from within my code.
> >
> > For example when the following code is executed:
> >
> > Dim EXL As Excel.Application = New Excel.Application
> >
> > a new instance of EXCEL.EXE is created in the task manager.
> >
> > I am looking for some code (or ideas) of how to identify this
> > particular instance of EXCEL.EXE AS IT IS CREATED. I know I can use
> > something like this:
> >
> > Dim localByName As Process() = Process.GetProcessesByName("excel")
> >
> > but this code will give me ALL instances of EXCEL processes running on
> > the computer. I know I can also get the users of the processes but
> > what if I myself had just say 5 instances of EXCEL already running on
> > my computer?? How do I identify which is the Process Id of EXCEL
> > created (opened) by my code??
> >
> > I'm sure some smart people out there have the answers! Appreciate any
> > help.
> >
> > PowaGuy[/color]
>
> Well, it looks like the Application object has an hWnd property that
> returns a handle to the main application window... Using that information
> and a little API majic, we can get what your after :)
>
> Private Declare Function GetWindowThreadProcessId Lib "user32" ( _
> ByVal hWnd As IntPtr, _
> ByRef lpdwProcessId As IntPtr) As IntPtr
>
> ' create the excel instance...
> Dim exl As Excel.Application = New Excel.Application
>
> ' let's get it's process!
> Dim processId As IntPtr
>
> ' we ignore the return value, since we don't care
> ' about the thread id...
> GetWindowThreadProcessId (exl.hWnd, processId)
>
> Dim excelProcess As Process = Process.GetProcessById (processId.ToInt32())
>
> That should do it :)[/color] |