Hi, I made a simple program that can help me to discover when a window is going to be closed or when is going to be opened.
I made the dll and loaded from my program, but anityng was printed if I close or open a window in my WINDOWS desktop.
I have added the code program and also the dinamic link library, if somoone can help me.
Kind regards
What I have tried:
/////////////////////////////////THIS IS MY PROGRAM//////////////////////////////
#include "stdafx.h"
#include <windows.h>
int _tmain(int argc, _TCHAR* argv[])
{
HMODULE dll = LoadLibrary(L"C:\\Users\\Agostino\\Desktop\\ConsoleApplication4\\Debug\\ConsoleApplication4.dll");
if(dll == NULL) {
printf("The DLL could not be found.\n");
getchar();
return -1;
}else{
HOOKPROC addr = (HOOKPROC)GetProcAddress(dll, "SetCbtHook");
if(addr != NULL) {
printf("The function was found.\n");
//getchar();
}else{
printf("The function was not found.\n");
//getchar();
return -1;
}
}
printf("Program successfully hooked.\nPress enter to unhook the function and stop the program.\n");
getchar();
return 0;
}
///////////THIS IS THE DYNAMIC LINK LIBRARY: ConsoleApplication4.dll///////////////
#include "stdafx.h"
#include <stdio.h>
#include "windows.h"
#pragma data_seg(".CYRUZ_CBTHOOK_PRAGMA_SECTION")
HWND g_hNotifyWin = NULL;
HHOOK g_hHook = NULL;
#pragma data_seg()
#pragma comment(linker, "/section:.CYRUZ_CBTHOOK_PRAGMA_SECTION,rws")
const DWORD C_ACTIVATE = RegisterWindowMessage(L"CYRUZ_CBTHOOK_MSG_HCBT_ACTIVATE");
const DWORD C_CLICKSKIPPED = RegisterWindowMessage(L"CYRUZ_CBTHOOK_MSG_HCBT_CLICKSKIPPED");
const DWORD C_CREATEWND = RegisterWindowMessage(L"CYRUZ_CBTHOOK_MSG_HCBT_CREATEWND");
const DWORD C_DESTROYWND = RegisterWindowMessage(L"CYRUZ_CBTHOOK_MSG_HCBT_DESTROYWND");
const DWORD C_KEYSKIPPED = RegisterWindowMessage(L"CYRUZ_CBTHOOK_MSG_HCBT_KEYSKIPPED");
const DWORD C_MINMAX = RegisterWindowMessage(L"CYRUZ_CBTHOOK_MSG_HCBT_MINMAX");
const DWORD C_MOVESIZE = RegisterWindowMessage(L"CYRUZ_CBTHOOK_MSG_HCBT_MOVESIZE");
const DWORD C_QS = RegisterWindowMessage(L"CYRUZ_CBTHOOK_MSG_HCBT_QS");
const DWORD C_SETFOCUS = RegisterWindowMessage(L"CYRUZ_CBTHOOK_MSG_HCBT_SETFOCUS");
const DWORD C_SYSCOMMAND = RegisterWindowMessage(L"CYRUZ_CBTHOOK_MSG_HCBT_SYSCOMMAND");
const DWORD C_STOPMSGPUMP = RegisterWindowMessage(L"CYRUZ_CBTHOOK_MSG_STOPMSGPUMP");
HINSTANCE g_hDll = NULL;
BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);
extern "C" __declspec(dllexport) BOOL SetCbtHook(HWND hWnd, DWORD threadId);
extern "C" __declspec(dllexport) BOOL UnsetCbtHook();
static LRESULT CALLBACK CbtProcCb(int nCode, WPARAM wParam, LPARAM lParam);
static DWORD WINAPI MessagePump(LPVOID lpThreadParameter);
BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch ( fdwReason )
{
case DLL_PROCESS_ATTACH:
g_hDll = hinstDLL;
return true;
}
}
BOOL SetCbtHook(HWND hWnd, DWORD threadId)
{
if ( g_hNotifyWin != NULL )
return false;
g_hHook = SetWindowsHookEx(WH_CBT, (HOOKPROC)CbtProcCb, g_hDll, threadId);
if ( g_hHook != NULL )
{
g_hNotifyWin = hWnd;
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)MessagePump, NULL, 0, 0);
return true;
}
return false;
}
BOOL UnsetCbtHook()
{
if ( g_hNotifyWin == NULL )
return false;
BOOL isUnhooked = UnhookWindowsHookEx(g_hHook);
if ( isUnhooked )
g_hNotifyWin = NULL;
PostMessage(g_hNotifyWin, C_STOPMSGPUMP, 0, 0);
return isUnhooked;
}
static LRESULT CALLBACK CbtProcCb(int nCode, WPARAM wParam, LPARAM lParam)
{
if ( nCode < 0 )
return CallNextHookEx(g_hHook, nCode, wParam, lParam);
DWORD uMsg = NULL;
switch ( nCode )
{
case HCBT_ACTIVATE:
uMsg = C_ACTIVATE;
break;
case HCBT_CLICKSKIPPED:
uMsg = C_CLICKSKIPPED;
break;
case HCBT_CREATEWND:
uMsg = C_CREATEWND;
break;
case HCBT_DESTROYWND:
uMsg = C_DESTROYWND;
break;
case HCBT_KEYSKIPPED:
uMsg = C_KEYSKIPPED;
break;
case HCBT_MINMAX:
uMsg = C_MINMAX;
break;
case HCBT_MOVESIZE:
uMsg = C_MOVESIZE;
break;
case HCBT_QS:
uMsg = C_QS;
break;
case HCBT_SETFOCUS:
uMsg = C_SETFOCUS;
break;
case HCBT_SYSCOMMAND:
uMsg = C_SYSCOMMAND;
break;
}
SendNotifyMessage(g_hNotifyWin, uMsg, wParam, lParam);
return CallNextHookEx(g_hHook, nCode, wParam, lParam);
}
static DWORD WINAPI MessagePump(LPVOID lpThreadParameter)
{
BOOL bRet; MSG aMsg;
while ( (bRet = GetMessage(&aMsg, NULL, 0, 0)) != 0 )
{
if ( bRet == -1 )
{
if ( g_hNotifyWin != NULL )
UnsetCbtHook();
return -1;
}
if ( aMsg.message == C_STOPMSGPUMP )
return 0;
TranslateMessage(&aMsg);
DispatchMessage(&aMsg);
}
return 0;
}