2

I have a service (lets say myservice) which tries to find out path of another application (lets say myapp.exe). myservice only knows about the name of application not the full path. my code is as follow:

HMODULE hModule = GetModuleHandle(TEXT("myapp.exe"));
if( hModule == NULL )
{
    // error 126
    return false;
}
int ret = GetModuleFileName(hModule, szBuffer, dwBufferSize);
if( !ret )
{
    .......
    return false;
}

GetModuleHandle always returns with 126 error mod_not_found. how can ! achieve this functionality.

Thanks, KM.

user2731777
  • 171
  • 2
  • 11
  • possible duplicate of [C++ Windows - How to get process path from its PID](http://stackoverflow.com/questions/1933113/c-windows-how-to-get-process-path-from-its-pid) – Jonathon Reinhart Sep 26 '13 at 01:36
  • 1
    From the MSDN documentation for `GetModuleHandle`: "The module must have been loaded by the calling process." The documentation for `GetModuleFileName` says: "To locate the file for a module that was loaded by another process, use the `GetModuleFileNameEx` function." Really you should use the latter and get a handle to the *process*. – jamesdlin Sep 26 '13 at 01:39

1 Answers1

2

You need to enumerate running processes using EnumProcesses(), calling OpenProcess() and GetModuleFileNameEx() on each process ID until you find the filename you are interested in, then you will have its full path.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770