Page 1 of 1

how to find the path of another running process

Posted: Tue Jun 29, 2010 4:35 am
by Suirad
as the subject says, i am wanting to find the path of an exe that is running on the same machine. searching through the pb docs and the forum i was unable to find a solution. would anyone be able to answer my question(hopefully a snippet) or point me in the right direction.

Thanks for your time.

Edit: Note this is for windows

Re: how to find the path of another running process

Posted: Tue Jun 29, 2010 5:22 am
by Josh
Suirad wrote:would anyone be able to answer my question
would you be able to write your question in the board-standardformat?
i hope nobody will answer at such kind of 'importend' questions and a mod will litter this topic

Re: how to find the path of another running process

Posted: Tue Jun 29, 2010 6:20 am
by Suirad
well sorry, i didnt know it was frowned upon to add a bit of color to my posts

Re: how to find the path of another running process

Posted: Tue Jun 29, 2010 7:09 am
by Mr Coder
When you're in someone else's house, you abide by their rules.

Re: how to find the path of another running process

Posted: Tue Jun 29, 2010 7:14 am
by Suirad
Mr Coder wrote:When you're in someone else's house, you abide by their rules.
Well that's fine and dandy if the rules are clearly stated.
But that's off topic and already dealt with

Re: how to find the path of another running process

Posted: Tue Jun 29, 2010 12:39 pm
by DarkPlayer
Hello,

you did not tell us what information you have about the process. I assume that you know the PID of it, so here is the unofficial way to get the Path of an Executable if you know the PID:

Code: Select all

Structure UNICODE_STRING
  Length.w
  MaxiumLength.w
  *Buffer
EndStructure

Prototype.i GetVolumePathNameW(lpszFileName.p-unicode, *lpszVolumePathName, cchBufferLength.i)
Prototype.i GetVolumePathNameA(lpszFileName.p-ascii, *lpszVolumePathName, cchBufferLength.i)

CompilerIf #PB_Compiler_Unicode
  Global GetVolumePathName.GetVolumePathNameW
CompilerElse
  Global GetVolumePathName.GetVolumePathNameA
CompilerEndIf

#ProcessImageFileName = 27

Procedure FindStringIndex(Text.s, FindString.s, Index.i)
 
  Protected Position = 0
  Protected i.i
 
  For i = 1 To Index
    Position = FindString(Text, FindString,Position+1)
    If Position = 0
      ProcedureReturn 0
    EndIf
  Next
 
  ProcedureReturn Position
 
EndProcedure

Procedure GetPathFromPID_Init()
  Library = OpenLibrary(#PB_Any,"kernel32.dll")
  If Library
   
    CompilerIf #PB_Compiler_Unicode
      GetVolumePathName = GetFunction(Library,"GetVolumePathNameW")
    CompilerElse
      GetVolumePathName = GetFunction(Library,"GetVolumePathNameA")
    CompilerEndIf
    ProcedureReturn #True
    
  EndIf
EndProcedure

Procedure.s GetPathFromPID(PID.i)
  Protected HProcess.i
  Protected *Memory.UNICODE_STRING
  Protected Name.s = ""
  Protected Size.s
  Protected Position.i
  Protected Library.i
 
  If PID = 4 : ProcedureReturn "SYSTEM" : EndIf
 
  *Memory = AllocateMemory(2*#MAX_PATH)
  If *Memory
    HProcess = OpenProcess_(#PROCESS_QUERY_INFORMATION,0,PID)
    If HProcess
      If ZwQueryInformationProcess_(HProcess ,#ProcessImageFileName,*Memory,2*#MAX_PATH,0) = 0
        Name = PeekS(*Memory\Buffer,*Memory\Length,#PB_Unicode)
        Position = FindStringIndex(Name,"\",3)
        If Position

          Protected DeviceString.s = Space(128)
         
          If GetVolumePathName(Name,@DeviceString,128)
            Name = DeviceString + Mid(Name,Position+1) 
          EndIf

        EndIf
      EndIf
      CloseHandle_(HProcess)
    EndIf
    FreeMemory(*Memory)
  EndIf
  ProcedureReturn Name
EndProcedure
Example usage:

Code: Select all

If GetPathFromPID_Init()
  Debug GetPathFromPID(...)
EndIf
For more Information, have a look at: http://www.purebasic.fr/english/viewtop ... 12&t=42608

DarkPlayer

Re: how to find the path of another running process

Posted: Tue Jun 29, 2010 10:52 pm
by Suirad
thank you, that was exactly what i was looking for