|
| 1 | +#include <windows.h> |
| 2 | +#include <tlhelp32.h> |
| 3 | +#include <cstdio> |
| 4 | + |
| 5 | +// Function to get the PID of the target process by name |
| 6 | +DWORD GetProcessIdByName(const char* processName) |
| 7 | +{ |
| 8 | + DWORD processId = 0; |
| 9 | + PROCESSENTRY32 pe32; |
| 10 | + pe32.dwSize = sizeof(PROCESSENTRY32); |
| 11 | + |
| 12 | + HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 13 | + if (hProcessSnap == INVALID_HANDLE_VALUE) |
| 14 | + return 0; |
| 15 | + |
| 16 | + if (Process32First(hProcessSnap, &pe32)) |
| 17 | + { |
| 18 | + do |
| 19 | + { |
| 20 | + if (strcmp(pe32.szExeFile, processName) == 0) |
| 21 | + { |
| 22 | + processId = pe32.th32ProcessID; |
| 23 | + break; |
| 24 | + } |
| 25 | + } while (Process32Next(hProcessSnap, &pe32)); |
| 26 | + } |
| 27 | + |
| 28 | + CloseHandle(hProcessSnap); |
| 29 | + return processId; |
| 30 | +} |
| 31 | + |
| 32 | +static HWND targetHwnd = NULL; |
| 33 | +static DWORD targetPID = 0; |
| 34 | + |
| 35 | +static BOOL CALLBACK EnumWindowsCallback(HWND hwnd, LPARAM lParam) |
| 36 | +{ |
| 37 | + targetHwnd = NULL; |
| 38 | + |
| 39 | + DWORD currentPID; |
| 40 | + GetWindowThreadProcessId(hwnd, ¤tPID); |
| 41 | + |
| 42 | + if (currentPID == targetPID) |
| 43 | + { |
| 44 | + // We found a window that belongs to the target process |
| 45 | + targetHwnd = hwnd; |
| 46 | + return FALSE; // Stop enumeration |
| 47 | + } |
| 48 | + |
| 49 | + return TRUE; // Continue enumeration |
| 50 | +} |
| 51 | + |
| 52 | +// Function to draw an image on a window using GDI |
| 53 | +void DrawImageOnWindow(HWND hwnd) |
| 54 | +{ |
| 55 | + // Get the device context (DC) of the target window |
| 56 | + HDC hdc = GetDC(hwnd); |
| 57 | + if (!hdc) |
| 58 | + { |
| 59 | + printf("Failed to get device context.\n"); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + // Create a memory DC to hold the bitmap |
| 64 | + HDC memDC = CreateCompatibleDC(hdc); |
| 65 | + if (!memDC) |
| 66 | + { |
| 67 | + printf("Failed to create memory DC.\n"); |
| 68 | + ReleaseDC(hwnd, hdc); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + // Load an image from file (use LoadImage for simplicity) |
| 73 | + HBITMAP hBitmap = (HBITMAP)LoadImageA(NULL, "monika.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); |
| 74 | + if (!hBitmap) |
| 75 | + { |
| 76 | + printf("Failed to load image.\n"); |
| 77 | + ReleaseDC(hwnd, hdc); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + // Get the bitmap dimensions |
| 82 | + BITMAP bmp_info; |
| 83 | + GetObject(hBitmap, sizeof(BITMAP), &bmp_info); |
| 84 | + |
| 85 | + // Select the bitmap into the memory DC, this will change memDC mapping area to the bmp file content |
| 86 | + SelectObject(memDC, hBitmap); |
| 87 | + |
| 88 | + // BitBlt (copy) the image from the memory DC to the window DC |
| 89 | + BitBlt(hdc, 0, 0, bmp_info.bmWidth, bmp_info.bmHeight, memDC, 0, 0, SRCCOPY); |
| 90 | + |
| 91 | + // Clean up |
| 92 | + DeleteDC(memDC); |
| 93 | + DeleteObject(hBitmap); |
| 94 | + ReleaseDC(hwnd, hdc); |
| 95 | +} |
| 96 | + |
| 97 | +int main() |
| 98 | +{ |
| 99 | + // Get the PID of the target process |
| 100 | + targetPID = GetProcessIdByName("target.exe"); |
| 101 | + if (!targetPID) |
| 102 | + { |
| 103 | + printf("Target process not found.\n"); |
| 104 | + return -1; |
| 105 | + } |
| 106 | + printf("Target process found with PID %lu\n", targetPID); |
| 107 | + |
| 108 | + // Get the window handle of the target process |
| 109 | + EnumWindows(EnumWindowsCallback, 0); |
| 110 | + if (!targetHwnd) |
| 111 | + { |
| 112 | + printf("Failed to find window of target process.\n"); |
| 113 | + return -1; |
| 114 | + } |
| 115 | + printf("Window handle of target process: 0x%p\n", targetHwnd); |
| 116 | + |
| 117 | + // Draw an image on the target window |
| 118 | + DrawImageOnWindow(targetHwnd); |
| 119 | +} |
0 commit comments