Click here to Skip to main content
15,903,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a DLL to inject a system hook into all processes. After a great deal of work I finally got it to compile and run without error. But when a key is pressed it doesn't seem to call the function in the DLL. My script for the DLL is as follows:
C++
#include "stdafx.h"
#include "HookDLL.h"
#include <windows.h>
#include <iostream>
#include <stdio.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

HINSTANCE hinst;
HHOOK hhk;


LRESULT CALLBACK wireKeyboardProc(int code,WPARAM wParam,LPARAM lParam) {
	printf("2");
	CallNextHookEx(hhk,code,wParam,lParam);
	return 0;
}

extern "C" __declspec(dllexport) void install() { 
	hhk = SetWindowsHookEx(WH_KEYBOARD_LL, wireKeyboardProc, hinst, 0);
	printf("3");
}
extern "C" __declspec(dllexport) void uninstall() {
	UnhookWindowsHookEx(hhk); 
}

/*BOOL WINAPI DllMain(  __in  HINSTANCE hinstDLL,
  __in  DWORD fdwReason,
  __in  LPVOID lpvReserved
  ) {

 hinst = hinstDLL;
 return TRUE;
};*/

BEGIN_MESSAGE_MAP(CHookDLLApp, CWinApp)
END_MESSAGE_MAP()

CHookDLLApp::CHookDLLApp()
{

}

CHookDLLApp theApp;

BOOL CHookDLLApp::InitInstance()
{
	CWinApp::InitInstance();

	return TRUE;
}

The DllMain was commented out because it generated an error saying it was a duplicate.
The contents of the cpp file for the main file:
C++
#include <windows.h>
#include <stdio.h>
#include <cstdlib>
#include <string>
int main(int argv[])
{
    HINSTANCE hinst = LoadLibrary("HookDLL.dll");
    if (hinst == NULL)
    {
        printf("null hinst");
    }
    typedef void (*Install)();
    typedef void (*Uninstall)();
    Install install = (Install) GetProcAddress(hinst, "install");
    Uninstall uninstall = (Uninstall) GetProcAddress(hinst, "uninstall");
    char ch;
    install();
    scanf(&ch);
    uninstall();
    return 0;
}

I placed a break point in the HOOKPROC and the debugger will not stop there. Any ideas or suggestions would be great.
Posted
Updated 23-Nov-10 21:56pm
v3

 
Share this answer
 
<code>
HINSTANCE hinst;//

hhk = SetWindowsHookEx(WH_KEYBOARD_LL, wireKeyboardProc, hinst, 0);



//Todo::you can't Distribution of instances!!! Plase  Distribution of instances

</code>
 
Share this answer
 
Comments
LordXandor 26-Nov-10 9:24am    
hinst is supposed to be defined in the DllMain function, but when I put it back into the code it gives the following error on compile:

Error 1 error LNK2005: _DllMain@12 already defined in HookDLL.obj uafxcwd.lib HookDLL
Error 2 fatal error LNK1169: one or more multiply defined symbols found C:\Projects\VS\Text\Debug\HookDLL.dll 1 HookDLL

I've looked on Google quite a bit and found nothing very helpful, most things said to set the project to ignore uafxcwd.lib or to change the MFC shared library options, but no matter which options I pick it does nothing, and when I ignore uafxcwd.lib it does fix the current error but gives me a whole lot more instead. One possible thought I have is that this really doesn't need to use MFC at all, but in VS2008 I don't see an option for a DLL project that doesn't use MFC. One other thing, if I remove #include stdafx.h this also fixes this error but then gives the following two:

Warning 1 warning C4627: '#include "HookDLL.h"': skipped when looking for precompiled header use c:\projects\vs\hookdll\hookdll.cpp 1 HookDLL
Error 2 fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source? c:\projects\vs\hookdll\hookdll.cpp 44 HookDLL

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