Home > Enterprise >  How to get process name in an injected dll?
How to get process name in an injected dll?

Time:07-02

I have this basic internal dll:

#include "pch.h"

DWORD WINAPI HackThread(HMODULE hModule)
{
    uintptr_t moduleBase = (uintptr_t)GetModuleHandle(L"Mainmodule123.exe");

    AllocConsole();
    FILE* f;
    freopen_s(&f, "CONOUT$", "w", stdout);

    std::cout << "Injected" << std::endl;
    while (!GetAsyncKeyState(VK_END) & 1) {
        Sleep(10)
    }
    
    fclose(f);
    FreeConsole();
    FreeLibraryAndExitThread(hModule, 0);
    return 0;
}


BOOL APIENTRY DllMain(HMODULE hModule,
    DWORD  ul_reason_for_call,
    LPVOID lpReserved
)
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    {
        CloseHandle(CreateThread(nullptr, 0, (LPTHREAD_START_ROUTINE)HackThread, hModule, 0, nullptr));
    }
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

I want to get a handle to the main module of the application, for example: GetModuleHandle(L"Mainmodule123.exe")

The problem, is that this application is changing the module numbers randomly.

The main module name is the same as the process name. So I need to detect the process name that I'm attached to.

How can I get the process name I'm attached to in an internal dll?

CodePudding user response:

You can pass in NULL for the function GetModuleHandle() which still returns a handle to the process running, I hope this isn't for any game hacking :(

  • Related