Re: Obtain the process id for a given filename

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Chris \( Val \) (chrisval_at_bigpond.com.au)
Date: 03/25/04


Date: Thu, 25 Mar 2004 22:55:53 +1100

Hello David, and thanks for trying to help.

I was already aware of the tools you mention, but I
was asked to write such a function, if at all possible.

It seems there are no APIs to do this, and I will need
to resort to some low level code, yikes :-).

Thanks again.
Chris Val

"David Wang [Msft]" <someone@online.microsoft.com> wrote in message
news:%23G39AlhEEHA.624@TK2MSFTNGP10.phx.gbl...
| Use a tool like NTHANDLE at www.sysinternals.com to do what you want.
|
| You pass in a filename, and NTHANDLES returns the process name/IDs that have
| a handle open.
|
| FYI: your question is really "how do I obtain the processes that have a
| handle open to a given file".
|
| --
| //David
| IIS
| This posting is provided "AS IS" with no warranties, and confers no rights.
| //
| "Chris ( Val )" <chrisval@bigpond.com.au> wrote in message
| news:c3s4pi$2bptrc$1@ID-110726.news.uni-berlin.de...
| Hi Tom - thanks for the help.
|
| I have seen examples of this previously, but it's not
| really what I was looking to do. Perhaps I can explain
| what I'm after with some sample code:
|
| Private Function GetPID( ByVal filename As String ) As Long
| '
| Dim Result As Long
|
| Result = 'Do whatever it takes to get the PID associated
| 'with the program that has the file locked...
|
| GetPID = Result
| '
| End Function
|
| Once I get the PID, then I guess the easy part is running
| an query on the Win32_Process class, to obtain the Name
| property, etc...
|
| It's a VB type function, but I hope it explains better
| what I want to do.
|
| Thanks.
| Chris Val
|
|
| "Tom Lavedas" <tlavedas@hotmail.remove.com> wrote in message
| news:1291a01c411a7$65aeed70$a401280a@phx.gbl...
| | Her's an example of one way that finds all instances of
| | running processes that match a supplied name ...
| |
| | sComputer = "."
| | sAppName = "Excel.exe" ' for example
| |
| | set oWMIService = GetObject("winmgmts:" _
| | & "{impersonationLevel=impersonate}!\\" _
| | & sComputer & "\root\cimv2")
| |
| | Set cThreadList = oWMIService.ExecQuery _
| | ("Select * from Win32_Process where Name = '" _
| | & sAppName & "'",, 48)
| |
| | For Each oThread in cThreadList
| | s = s & oThread.ProcessID & ", "
| | Next
| | wsh.echo "Process ID(s):", _
| | Replace(s & "None Found", ", None Found", "")
| |
| | Tom Lavedas
| | ===========
| |
| | >-----Original Message-----
| | >Hi all - I'm finding it really difficult to find a way
| | >to do this (if at all possible ). Does anyone know of
| | >a way to obtain the process id of a filename ?
| | >
| | >Any help appreciated.
| | >
| | >Cheers.
| | >Chris Val
| | >.
| | >
|
|
|



Relevant Pages

  • Re: Obtain the process id for a given filename
    ... Private Function GetPID(ByVal filename As String) As Long ... Once I get the PID, then I guess the easy part is running ... | running processes that match a supplied name ... ...
    (microsoft.public.scripting.wsh)
  • Re: Obtain the process id for a given filename
    ... Why do you need to re-invent the wheel and rewrite NTHANDLE when it already ... | Once I get the PID, then I guess the easy part is running ... | "Tom Lavedas" wrote in message ... | | running processes that match a supplied name ... ...
    (microsoft.public.scripting.wsh)
  • Re: find process PID
    ... but it needs the entire program path (wich I don't ... I'd suggest CreateProcess, if Shell looks ... You can use GetModuleFileName against any given PID. ... approach to finding running processes will vary depending on OS. ...
    (comp.lang.basic.visual.misc)
  • Re: Terminating a process
    ... To 'print out' the name and PID, ... You might also add a last line in your script ... ... If the first Echo does not appear, it means you have not matched the name of the process in the list WMI keeps. ... >> Tom Lavedas ...
    (microsoft.public.scripting.vbscript)
  • Obtain the process id for a given filename
    ... running processes that match a supplied name ... ... sAppName = "Excel.exe" ' for example ... For Each oThread in cThreadList ... Tom Lavedas ...
    (microsoft.public.scripting.wsh)