Hey All,
I'm writing some dll injection code using windows hooks. I'm understanding the code pretty well, Its just more of the theory that I don't quite get. First I will post my code for the dll injecting:
Now this is what I understand should be happening with the DllMain:Code:BOOL InjectDll ( LPCWSTR dllName );
int _tmain(int argc, _TCHAR* argv[])
{
// Inject our dll
InjectDll ( L"C:\\Code\\APIHookingRevisited_src\\ThreadSpy.dll" );
while ( true ) Sleep ( 1 );
return 0;
}
BOOL InjectDll ( LPCWSTR dllName )
{
HMODULE hDll;
FARPROC cbtProcAddr;
// Load our library
hDll = LoadLibrary ( dllName );
// Get our process
cbtProcAddr = GetProcAddress ( hDll, "CBTProc" );
HWND hWnd = FindWindow ( 0, L"Form1" );
// Set our hooks
SetWindowsHookEx(WH_CBT, (HOOKPROC)cbtProcAddr, hDll, (DWORD) hWnd);
return TRUE;
}
It should get called once from being loaded (vis LoadLibrary), which happens.
Because I'm setting a callback dll, the process that im hooking should also have to load the dll into it's memory. This is not happening. So am I understanding wrong? Here is my dll code:
So again... From my understanding, this should be working fine. I'm obviously misunderstanding something. Could someone point me in the right direction?Code:__declspec( dllexport ) LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam)
{
return CallNextHookEx(0, nCode, wParam, lParam);
};
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
MessageBox(NULL, "DLL attached", "None", 0);
break;
case DLL_PROCESS_DETACH:
MessageBox(NULL, "DLL detached", "None", 0);
break;
}
return TRUE;
}