To draw text on a window using an injected C++ DLL, you can utilize the SetWindowsHookEx function from the Windows API to install a keyboard or mouse hook and then handle the WM_PAINT message in the hook procedure to draw the text on the window.
Here's an example code that demonstrates this approach:
#include <Windows.h>
#include <iostream>
// Global variables to store the hook handle and original window procedure
HHOOK g_hook = NULL;
WNDPROC g_originalWndProc = NULL;
// Hook procedure to handle the WM_PAINT message and draw the text
LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HC_ACTION)
{
// Cast the event data to the MSG structure
MSG* pMsg = reinterpret_cast<MSG*>(lParam);
if (pMsg->message == WM_PAINT)
{
HWND hwnd = pMsg->hwnd;
HDC hdc = GetDC(hwnd); // Get device context of the window
RECT rect;
GetClientRect(hwnd, &rect); // Get window's client area rectangle
HFONT hFont = CreateFont(24, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
OUT_OUTLINE_PRECIS, CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY,
VARIABLE_PITCH, TEXT("Arial")); // Create font
HFONT hOldFont = (HFONT)SelectObject(hdc, hFont); // Select the font into the device context
SetTextColor(hdc, RGB(0, 0, 0)); // Set text color (black)
SetBkMode(hdc, TRANSPARENT); // Set background mode to transparent
DrawText(hdc, "Injected Text", -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE); // Draw the text
SelectObject(hdc, hOldFont); // Restore original font
DeleteObject(hFont); // Delete the font
ReleaseDC(hwnd, hdc); // Release the device context
}
}
// Call the next hook procedure in the hook chain
return CallNextHookEx(g_hook, nCode, wParam, lParam);
}
// Function to install the hook and inject the DLL
bool InjectDLL(HWND hwnd)
{
g_hook = SetWindowsHookEx(WH_KEYBOARD, HookProc, NULL, GetCurrentThreadId());
if (g_hook == NULL)
{
std::cerr << "Failed to install the hook!" << std::endl;
return false;
}
// Store the original window procedure
g_originalWndProc = reinterpret_cast<WNDPROC>(GetWindowLongPtr(hwnd, GWLP_WNDPROC));
// Replace the window procedure with our custom procedure
if (!SetWindowLongPtr(hwnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(HookProc)))
{
std::cerr << "Failed to replace the window procedure!" << std::endl;
UnhookWindowsHookEx(g_hook);
return false;
}
return true;
}
// Function to restore the original window procedure and uninstall the hook
void UninjectDLL(HWND hwnd)
{
if (g_originalWndProc != NULL)
{
// Restore the original window procedure
SetWindowLongPtr(hwnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(g_originalWndProc));
g_originalWndProc = NULL;
}
if (g_hook != NULL)
{
// Uninstall the hook
UnhookWindowsHookEx(g_hook);
g_hook = NULL;
}
}
int main()
{
HWND hwnd = GetForegroundWindow(); // Get the handle to the active window
if (InjectDLL(hwnd))
{
std::cout << "DLL injected successfully!" << std::endl;
// Do your other operations here...
UninjectDLL(hwnd);
std::cout << "DLL uninjected successfully!" << std::endl;
}
return 0;
}
In the above code, the InjectDLL() function installs the keyboard hook using the SetWindowsHookEx() function and replaces the window procedure with the hook procedure using the SetWindowLongPtr() function. The hook procedure handles the WM_PAINT message and draws the desired text on the window based on the given window handle.
The UninjectDLL() function restores the original window procedure using the SetWindowLongPtr() function and uninstalls the hook using the UnhookWindowsHookEx() function.
Note that the above code injects the DLL into the active foreground window. If you need to inject into a different window, you can modify the hwnd variable in the main() function accordingly.
Make sure to compile the DLL as a separate project and inject it into the target process using a suitable technique like DLL injection or process injection.