how to find the path of another running process

Just starting out? Need help? Post your questions and find answers here.
Suirad
User
User
Posts: 42
Joined: Sun Sep 20, 2009 7:37 pm
Location: Melbourne, Florida, USA

how to find the path of another running process

Post 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
Last edited by Suirad on Tue Jun 29, 2010 6:18 am, edited 1 time in total.
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: how to find the path of another running process

Post 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
sorry for my bad english
Suirad
User
User
Posts: 42
Joined: Sun Sep 20, 2009 7:37 pm
Location: Melbourne, Florida, USA

Re: how to find the path of another running process

Post by Suirad »

well sorry, i didnt know it was frowned upon to add a bit of color to my posts
Mr Coder
User
User
Posts: 54
Joined: Tue Apr 13, 2010 8:02 am

Re: how to find the path of another running process

Post by Mr Coder »

When you're in someone else's house, you abide by their rules.
Suirad
User
User
Posts: 42
Joined: Sun Sep 20, 2009 7:37 pm
Location: Melbourne, Florida, USA

Re: how to find the path of another running process

Post 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
DarkPlayer
Enthusiast
Enthusiast
Posts: 107
Joined: Thu May 06, 2010 11:36 pm

Re: how to find the path of another running process

Post 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
Suirad
User
User
Posts: 42
Joined: Sun Sep 20, 2009 7:37 pm
Location: Melbourne, Florida, USA

Re: how to find the path of another running process

Post by Suirad »

thank you, that was exactly what i was looking for
Post Reply