Create a FUNCTIONING DLL for Windows Applications (Such as games on the Microsoft Store and the Calculator application)

  c++, dll-injection

I do not own the following code. The code was copied just to make sure that I had no errors within the actual code itself. The code works when injecting into other x64 type applications but has no functionality when injected into something like "Calculator.exe". The DLL itself injects (guaranteeing it injects through "Process Hacker 2") but doesn’t actually do anything once injected.

#include "pch.h"
#include <iostream>

DWORD WINAPI MainThread(HMODULE hModule)
{
    AllocConsole();
    FILE* f;
    freopen_s(&f, "CONOUT$", "w", stdout);

    printf("Calculator Injection");

    Sleep(2000);
    FreeConsole();

    DWORD moduleBase = (DWORD)GetModuleHandle(L"Calculator.exe");

    while (!GetAsyncKeyState(VK_DELETE))
    {

    }

    if (GetAsyncKeyState(VK_DELETE))
    {
        fclose(f);
    }
    
    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)MainThread, hModule, 0, nullptr));
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

Source: Windows Questions C++

LEAVE A COMMENT