Results 1 to 6 of 6

Thread: find path of running process

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    find path of running process

    i have code that can loop through all running processes, match the name of the file and kill the process, but i want to find the path of the processes' exe file, any code sample to do this please?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  2. #2
    Addicted Member Witis's Avatar
    Join Date
    Jan 2011
    Location
    VB Forums Online Freedom Mode: Operational
    Posts
    213

    Re: find path of running process

    Check Out GetProcessImageFileName API to get the device path, then you have to convert the device path to a windows file path, have fun!
    All men have an inherent right to life, the right to self determination including freedom from forced or compulsory labour, a right to hold opinions and the freedom of expression, and the right to a fair trial and freedom from torture. Be aware that these rights are universal and inalienable (cannot be given, taken or otherwise transferred or removed) although you do risk losing the aforementioned rights should you fail to uphold them e.g Charles Taylor; United Nations sources: http://www.un.org/en/documents/udhr/, http://www.ohchr.org/EN/Professional...ages/CCPR.aspx. Also Charles I was beheaded on the 30th of January of 1649 for trying to replace parliamentary democracy with an absolute monarchy, the same should happen to Dr Phil and Stephen Fry; source: http://www.vbforums.com/showthread.p...ute-Monarchism.

    The plural of sun is stars you Catholic turkeys.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: find path of running process

    thanks, i got that working now, but i cheated converting the device path
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: find path of running process

    This may be of some interest;

    Code:
    Private Const PROCESS_QUERY_INFORMATION = &H400&
    Private Const PROCESS_VM_READ = &H10&
    Private Const MAX_PATH = 260&
    
    Private Declare Function GetModuleFileNameEx Lib "psapi" Alias "GetModuleFileNameExA" _ 
     (ByVal hProcess As Long, ByVal hModule As Long, ByVal lpFilename As String, ByVal nSize As Long) As Long
    Private Declare Function OpenProcess Lib "kernel32" _
     (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Private Declare Sub CloseHandle Lib "kernel32" (ByVal hObject As Long)
    
    'returns the full path of a process identified by Pid.
    Private Function GetPathFromPid(ByVal Pid As Long) As String
        Dim hProcess As Long, s As String * MAX_PATH
        hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, Pid)
        GetModuleFileNameEx hProcess, 0, s, MAX_PATH    'this call crashes under Win 95, 98 and probably NT4 and ME
        CloseHandle hProcess
        GetPathFromPid = Left$(s, InStr(s, vbNullChar) - 1)
    End Function

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: find path of running process

    This may be of some interest;
    this certainly works too
    damn, too many choices now
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: find path of running process

    To help make your mind up see the Remarks in http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width