Alexey Kulikov
Alexey Kulikov

Reputation: 1147

Get current process Id from DLL

I have trying to hook WinApi using AppInitHook. So it works perfectly but I need to hook only some processes not all. The question is how to get process id where dll is loaded? For example dll was loaded for MyApp.exe, how can I get this process id?

Regarsd!

ps sorry im not hardcore WinApi programmer and my question mybe so easy, but its now hard for me)

Upvotes: 2

Views: 7758

Answers (2)

Serge Rogatch
Serge Rogatch

Reputation: 15030

To do something when DLL is loaded, you need to define DllMain function as in the code example below. Then you can get process ID when the process gets attached to the DLL (also in the code example).

BOOL APIENTRY DllMain(HMODULE hModule, DWORD  reasonForCall, LPVOID lpReserved)
{
    hModule; lpReserved;
    switch (reasonForCall) {
    case DLL_PROCESS_ATTACH:
    {
        uint32_t pid = GetCurrentProcessId();
        // Do something depending on the process ID in |pid|
        break;
    }
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

Upvotes: 0

Remy Lebeau
Remy Lebeau

Reputation: 596041

Look at the GetCurrentProcessId() function.

Upvotes: 2

Related Questions