Code:
#include <windows.h>
#include <stdio.h>
WNDPROC lpCalculator;
HWND hButton;
BOOL bButtonVisible;
HANDLE hEvent;
// ================================================
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
if (uMsg == WM_DESTROY) {
SetEvent(hEvent);
}
if (uMsg == WM_COMMAND &&
LOWORD(wParam) == 1020 &&
HIWORD(wParam) == BN_CLICKED) {
MessageBox(NULL, "I got clicked", NULL, 0);
}
return CallWindowProc(lpCalculator, hwnd, uMsg, wParam, lParam);
}
// ================================================
void DoEvents(void) {
MSG Msg;
while ( PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE) ) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}
// ===============================================
BOOL WINAPI LibMain(HANDLE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)
{
if (fdwReason==DLL_PROCESS_ATTACH) {
HWND hFind = NULL;
DWORD wndProcessId;
RECT rc;
DWORD thisProcessId=GetCurrentProcessId();
do
{
hFind=FindWindowEx(NULL, hFind,NULL, "Calculator");
GetWindowThreadProcessId(hFind, &wndProcessId);
} while (thisProcessId!=wndProcessId);
hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
lpCalculator = (WNDPROC) SetWindowLongPtr(hFind, GWL_WNDPROC, (LONG_PTR) WndProc);
GetWindowRect(hFind,&rc);
MoveWindow(hFind, rc.left, rc.top, (rc.right-rc.left) + 500, rc.bottom - rc.top, TRUE);
hButton = CreateWindowEx(0, "BUTTON", "MYBUT",
WS_CHILD | WS_VISIBLE,
500,5,100,100,hFind,(HMENU)1020,
(HINSTANCE) GetWindowLong(hFind, GWL_HINSTANCE), NULL);
if (!hButton) MessageBox(NULL,"couldn't create button","yo", MB_OK);
else MessageBox(NULL,"created button","yo", MB_OK);
bButtonVisible=TRUE;
while (MsgWaitForMultipleObjects(1, &hEvent, FALSE, INFINITE, QS_ALLEVENTS) != WAIT_OBJECT_0) {
DoEvents();
}
}
return TRUE;
}