I am trying to create a dll where I can use to monitor all of system events (process creation, destruction, etc). This is what I have come up so far:
DLL main - the entry point of my DLL:
// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
#include "CBTHook.h"
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
CBT::CBTHook::SetHandle(hModule);
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
DLL Header file
//Dll header file - function signatures
#ifdef CBTHOOKDLL_EXPORTS
#define CBTHOOKDLL_API __declspec(dllexport)
#else
#define CBTHOOKDLL_API __declspec(dllimport)
#endif
namespace CBT
{
class CBTHook
{
public:
CBTHook();
static void SetHandle(HINSTANCE handle);
void InstallHook();
void UnistallHook();
LRESULT CALLBACK HookProcedure(int nCode, WPARAM wparam, LPARAM lparam);
~CBTHook();
private:
static HINSTANCE currentProcessHandle;
HOOKPROC hkprcSysMsg;
static HHOOK hookID;
};
}
CBTHook.cpp
// CBTHook.cpp : Defines the exported functions for the DLL application.
//
#pragma once
#include "stdafx.h"
#include "CBTHook.h"
#include <Windows.h>
#include <iostream>
using namespace std;
namespace CBT
{
CBTHook::CBTHook()
{
}
void CBTHook::SetHandle(HINSTANCE handle)
{
currentProcessHandle = handle;
}
void CBTHook::InstallHook()
{
hookID = SetWindowsHookEx(WH_CBT,HookProcedure, currentProcessHandle, 0);
}
void CBTHook::UnistallHook()
{
UnhookWindowsHookEx(hookID);
}
LRESULT CALLBACK CBTHook::HookProcedure(int nCode, WPARAM wparam, LPARAM lparam)
{
if (nCode >= 0)
{
switch (nCode)
{
case HCBT_CREATEWND:
cout << "Created!~" << endl;
break;
case HCBT_DESTROYWND:
cout << "Destroied!~" << endl;
break;
default:
cout << "sth else" << endl;
break;
}
}
else
return CallNextHookEx(hookID, nCode, wparam, lparam);
}
}
Now the problem is that, SetWindowsHookEx
wont accept the HookProcedure
while as far as I have read and seen on the net the return value of the function in question is correct. I get the error:
error C3867: 'CBT::CBTHook::HookProcedure': function call missing argument list; use '&CBT::CBTHook::HookProcedure' to create a pointer to member
Doing as suggested by above doesn't solve the problem either!
What am I missing here?
Your hook procedure must be a free function or a static class method. If you want to call a class instance method, you need to wrap that call in one of the above.
Edit:
To set a hook you need no classes. This is a basic example. Every other problem stems from your use of a class. If you want to use a class, make sure you know how to do it. If you are unsure, C++ is not Java. You don't need to use a class if it works just perfectly without.
Example:
#include "stdafx.h"
HHOOK hHook;
BOOL APIENTRY DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved)
{
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
hHook = SetWindowsHookEx(WH_CBT, HookProcedure, hModule, 0);
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
LRESULT CALLBACK HookProcedure(int nCode, WPARAM wparam, LPARAM lparam)
{
if (nCode >= 0)
{
switch (nCode)
{
case HCBT_CREATEWND:
cout << "Created!~" << endl;
break;
case HCBT_DESTROYWND:
cout << "Destroied!~" << endl;
break;
default:
cout << "sth else" << endl;
break;
}
}
else
return CallNextHookEx(hHook, nCode, wparam, lparam);
}