1

I have succefully hooked a game and I need to do read and write with GetPixel and SetPixel to the game window.

To do a test I have created this code:

Here my hooked function:

My_hooked_function(int a3)
{

    HWND hWnd = GetForegroundWindow();

    if (hWnd == NULL)
         hWnd = GetDesktopWindow();

    if (a3==49)
    {

        string buf;
        LF::Log_Update("Button Pressed");
        
        SetWindowLongA(hWnd, GWL_WNDPROC, (LONG_PTR)&WndProc);
        // SetWindowSubclass(hWnd, &WndProc, 1, 0);
        //DefSubclassProc
        // DrawPixels(hWnd);
    }
}

here my test function to draw something in the game (that is a old 32 bit that required directx 11):

void DrawPixels(HWND hwnd) {

    PAINTSTRUCT ps;
    RECT r;

    LF::Log_Update("DrawPixels Entrato");

    GetClientRect(hwnd, &r);

    if (r.bottom == 0) {

        return;
    }

    HDC hdc = BeginPaint(hwnd, &ps);

    for (int i = 0; i < 1000; i++) {

        int x = rand() % r.right;
        int y = rand() % r.bottom;
        SetPixel(hdc, x, y, RGB(0, 0, 0));

        for (int i = 1; i <= 4; i++)
        {
            SetPixel(hdc, x - i, y, RGB(0, 0, 0));
            SetPixel(hdc, x + -i, y, RGB(0, 0, 0));

            SetPixel(hdc, x, y + i, RGB(0, 0, 0));
            SetPixel(hdc, x, y - i, RGB(0, 0, 0));
        }

    }

    EndPaint(hwnd, &ps);

}

and here the window procedure callback:

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
    WPARAM wParam, LPARAM lParam) {

    LF::Log_Update("Enter here");

    DrawPixels(hwnd);

    switch (msg) {

    case WM_PAINT:

        LF::Log_Update("For some reason not enter here");

        DrawPixels(hwnd);

        break;

    case WM_DESTROY:

        PostQuitMessage(0);
        return 0;
    }

return DefWindowProcW(hwnd, msg, wParam, lParam);

}

In my code I have 2 problems:

  1. I need to attach and not replace the window procedure (SetWindowLongA is not right in this case) because the game must continue to work. I need only to paint some line to the game existing window.
  2. I have tested the function "DrawPixels" to an empty c++ windows project and If I put it in the "WM_PAINT" message of the WndProc work perfectly but not in the game.

I searching for a way to draw some pixel on a external DirectX 11 game.

Can you help me please ?

Thanks !

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.