Trouble loading from .dll file

Hi. I'm fairly new to the Win32API with several years of C and C++ experience. I'm currently trying to make a key hook (just for amusement/learning purposes) and I'm having some trouble. Since I want to make the hook global, it needs to be built into a separate .dll file. The main program loads the .dll fine, but when I call GetProcAddress(), the function addresses are not returned. Here's the code for the .dll

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <windows.h>
#include <tchar.h>

HHOOK hHook;

__declspec(dllexport) LRESULT CALLBACK KeyHookProc(int code, WPARAM wParam, LPARAM lParam)
{
	//Return things we shouldn't handle
	if(code < 0)
		CallNextHookEx(hHook, code, wParam, lParam);

	return CallNextHookEx(hHook, code, wParam,lParam);
}

__declspec(dllexport) BOOL CALLBACK KeyHookSetup(HINSTANCE hDLL, HOOKPROC procPtr)
{
	hHook = SetWindowsHookEx(WH_KEYBOARD, procPtr, hDLL, 0);
	if(hHook == NULL)
	{
		MessageBox(NULL, _T("Could not start hook"), _T("Error"), MB_OK | MB_ICONERROR);
		return FALSE;
	}

	return TRUE;
}

__declspec(dllexport) BOOL CALLBACK KeyHookFree(void)
{
	if(UnhookWindowsHookEx(hHook) != 0)
	{
		MessageBox(NULL, _T("Could not free hook"), _T("Error"), MB_OK | MB_ICONERROR);
	}

	return TRUE;
}


and here's the code for getting function pointers from the .dll

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
hDLL = LoadLibrary(_T("Hook_DLL.dll"));

if(hDLL == NULL)
{
	MessageBox(NULL, _T("DLL could not be loaded."), _T("Error"),
	MB_OK|MB_ICONERROR);
}

keyHookProc = (HOOKPROC)GetProcAddress((HMODULE)hDLL, "KeyHookProc");
keyHookSetup = (SETHOOKPROC)GetProcAddress((HMODULE)hDLL, "KeyHookSetup");
keyHookFree = (FREEHOOKPROC)GetProcAddress((HMODULE)hDLL, "KeyHookFree");

if(keyHookProc == NULL)
{
	MessageBox(NULL, _T("keyHookProc could not be loaded."), _T("Error"),
		MB_OK|MB_ICONERROR);
}
if(keyHookSetup == NULL)
{
	MessageBox(NULL, _T("keyHookSetup could not be loaded."), _T("Error"),
		MB_OK|MB_ICONERROR);
}
if(keyHookFree == NULL)
{
	MessageBox(NULL, _T("keyHookFree could not be loaded."), _T("Error"),
		MB_OK|MB_ICONERROR);
}


I've tried everything I can possibly think of. The application is compiling in C++. I originally had extern "C" {} around the .dll functions to avoid name mangling, but am now compiling the .dll in C instead to avoid any possible issues. I'm using Visual C++ 2008 as a build environment.

Yes, I do realize that HookKeyProc() is not finished, but I want to get the functions loading first. Thanks for all help in advance.
Last edited on
iirc for DLLs with VS you need to make a .def file which outlines the functions you want exposed.

Somewere in the project settings there's an option for a .def file to use.

The def file I used for some other lib I worked on looked like this. I copied this from elsewhere so I don't really know the significant of the library name.

1
2
3
4
LIBRARY   in_NotSoFatso

EXPORTS
winampGetInModule2


I guess under 'EXPORTS' you just list all the functions you want externally linked (winampGetInModule2 was a function in my dll)
Last edited on
The problem has been solved due to help on another forum. However, I appear to have a new one.

My hook is only working on the thread level, and not the global level. According to MSDN, to make the thread run on a global level you call SetWindowsHookEx() with a final parameter of 0. Despite my call being:

 
SetWindowsHookEx(WH_KEYBOARD, procPtr, hDLL, 0)


The hook procedure is only called when a keyboard message is passed to the window (I have verified this with breakpoints. The only time the function is called is when my window has focus and a key is pressed.) procPtr points to a function in a dll, and that dll's handle is hDLL. I don't see what the problem is. Would it be a problem that I'm calling SetWindowsHookEx() from inside of a dll function?
Last edited on
> Since I want to make the hook global, it needs to be built into a separate .dll file

No, you don't need DLL
According to http://msdn.microsoft.com/en-us/library/ms644960(VS.85).aspx :

You must place a global hook procedure in a DLL separate from the application installing the hook procedure.


Regardless, that's not answering my question. Does anybody have any idea why it's still only working on the thread level?
Last edited on
slavik262:

you want the code for global hooks, tell me you email.
i created it some time back.



george135:

to create windows hooks, they should be in dll only. because windows injects the dll code in the applications and which cant be done using a .exe file.
Disch:


he has already exported the functions so he doesnt need a .def file. we need to use either a .def file to export the functions or export the functions in .h/.cpp files.
Check your DLL using depends. You can confirm that you're DLL is exporting its functions.
slavik262


I don't know why your hook procedure cannot get keyboard events that handled by other threads, it's so strange..

but you can Replace
SetWindowsHookEx(WH_KEYBOARD, procPtr, hDLL, 0)

with
SetWindowsHookEx(WH_KEYBOARD_LL, procPtr, hDLL, 0)

you can get all low-level keyboard events...

look at this:
http://msdn.microsoft.com/en-us/library/ms644985(VS.85).aspx
Topic archived. No new replies allowed.