Thread: Getting the file name of a process Handle

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    100

    Getting the file name of a process Handle

    Hi all,

    Im trying to use the GetModuleFileName for a running process (which has been opened with OpenProcess). The code below isnt my exact code but it does show what im trying to do.

    Code:
    
    HANDLE myProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE, processInfo->th32ProcessID);
    
    char g[MAX_PATH];
    GetModuleFileName((HMODULE)myProcess, g, MAX_PATH);
    MessageBox(NULL, g, "this is the file name", MB_OK);
    The code just prints out junk value in the MessageBox which I assume is because Im trying to cast myProcess of type HANDLE to type HMODULE.

    Thanks for any help :-)

    Oh btw, the problem isnt with the OpenProcess method, that works fine.

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Google GetModuleFileNameEx. It takes a HANDLE to hProcess as a first parameter, and HMODULE as a second parameter. I assume that you can pass NULL for hModule and get what you want.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    As another option, this msdn example uses Module32First
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM