Basically, I want to do something after user clicks X (closes) a specific window.
This is my approach:
HWND handle;
LRESULT WINAPI procedure(int code, WPARAM wParam, LPARAM, lParam){
CWPSTRUCT* j = (CWPSTRUCT*) lParam;
WPSTRUCT* w = (CWPSTRUCT*) wParam;
if(w->message == WM_SYSCOMMAND && j->message == SC_CLOSE){
//do something
}
}
Void doSomething(){
HINSTANCE hDLL = LoadLibrary("User32.dll");
HWND WINAPI FindWindow(_In_opt_ LPCTSTR lpClassName, _In_opt_ LPCTSTR lpWindowName);
HHOOK WINAPI SetWindowsHookEx(_In_ int idHook, _In_ HOOKPROC lpfn, _In_ HINSTANCE hMod, _In_ DWORD dwThreadId);
handle = FindWindow("Notepad", NULL) //for example
DWORD threadId = GetWindowThreadProcessId(handle, NULL);
HHOOK hhook = SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC) &procedure, (HINSTANCE) NULL, threadId);
return;
}
This doesn't work as the application never enters the procedure().
Edit: After a suggestion from -RbMm i found threadId from handle. Thanks.
DWORD threadId = GetWindowThreadProcessId(handle, NULL);
HHOOK hhook = SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC) &procedure, (HINSTANCE) NULL, threadId);