Click here to Skip to main content
15,889,651 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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")
 
// Messages to handle in the server application.
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");
 
// Message to quit from the message pump thread.
const DWORD C_STOPMSGPUMP  = RegisterWindowMessage(L"CYRUZ_CBTHOOK_MSG_STOPMSGPUMP");
 
// DLL module handle.
HINSTANCE g_hDll = NULL;
 
// Functions declaration.
BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);
//extern "C" __declspec(dllexport) INT myfunc();
extern "C" __declspec(dllexport) BOOL SetCbtHook(HWND hWnd, DWORD threadId);    // Exported
extern "C" __declspec(dllexport) BOOL UnsetCbtHook();                           // Exported

static LRESULT CALLBACK CbtProcCb(int nCode, WPARAM wParam, LPARAM lParam);
static DWORD WINAPI MessagePump(LPVOID lpThreadParameter);
 
// Dll entry point.
BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    switch ( fdwReason )
    {
        case DLL_PROCESS_ATTACH:
            g_hDll = hinstDLL;
            return true;
    }
}

// Hooking function.
BOOL SetCbtHook(HWND hWnd, DWORD threadId)
{
    // Hook already initialized.
    if ( g_hNotifyWin != NULL )
        return false;
 
    g_hHook = SetWindowsHookEx(WH_CBT, (HOOKPROC)CbtProcCb, g_hDll, threadId);
    if ( g_hHook != NULL )
    {   // Success
        g_hNotifyWin = hWnd;
        CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)MessagePump, NULL, 0, 0);
        return true;
    }
     
    return false;
}
 
// Unhooking function.
BOOL UnsetCbtHook()
{
    // Hook not initialized.
    if ( g_hNotifyWin == NULL )
        return false;
     
    BOOL isUnhooked = UnhookWindowsHookEx(g_hHook);
    if ( isUnhooked )
        g_hNotifyWin = NULL;
 
    // Send the stop message to the message pump thread.
    PostMessage(g_hNotifyWin, C_STOPMSGPUMP, 0, 0);
    return isUnhooked;
}
 
// Hook callback.
static LRESULT CALLBACK CbtProcCb(int nCode, WPARAM wParam, LPARAM lParam)
{
    // Return without further processing if nCode < 0. Check CBTProc nCode for info:
    // http://msdn.microsoft.com/en-us/library/windows/desktop/ms644977(v=vs.85).aspx
    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);
}
 
// Thread for message pumping. Check SetWindowsHookEx Remarks for info:
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms644990(v=vs.85).aspx
// This should not be needed if the hooking application implements a message loop.
static DWORD WINAPI MessagePump(LPVOID lpThreadParameter)
{
    BOOL bRet; MSG  aMsg;
    while ( (bRet = GetMessage(&aMsg, NULL, 0, 0)) != 0 )
    {
        if ( bRet == -1 )
        {   // Unhook in case of GetMessage error if hook is still running.
            if ( g_hNotifyWin != NULL ) 
                UnsetCbtHook();
            return -1;
        }
         
        if ( aMsg.message == C_STOPMSGPUMP )
            return 0;
 
        TranslateMessage(&aMsg);
        DispatchMessage(&aMsg);
    }
    return 0;
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900