I am new to windows programming, I was trying to compile the following code and it gives me the following errors:
C:\Program Files\Microsoft Visual Studio\MyProjects\hook\hook.cpp(14) : error C2065: 'KeyboardProc' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\hook\hook.cpp(22) : error C2373: 'KeyboardProc' : redefinition; different type modifiers
C:\Program Files\Microsoft Visual Studio\MyProjects\hook\hook.cpp(30) : error C2065: 'GetkeyState' : undeclared identifier
Error executing cl.exe.

/* CODE */
#include<windows.h>

static HHOOK hkb=NULL;
HANDLE h;

BOOL __stdcall DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
h=hModule;
return TRUE;
}

BOOL __declspec(dllexport)installhook()
{
hkb=SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)KeyboardProc, (HINSTANCE)h,0);
if(hkb==NULL)
return FALSE;

return TRUE;
}

LRESULT __declspec(dllexport)__stdcall KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
short int state;

if(nCode<0)
return CallNextHookEx(hkb, nCode, wParam, lParam);

if((nCode==HC_ACTION)&&((DWORD)lParam&0*40000000))
{
state=GetkeyState(VK_CAPITAL);
if((state&1)==0)
{
keybd_event(VK_CAPITAL,0,KEYEVENTF_EXTENDEDKEY,0);
keybd_event(VK_CAPITAL,0,KEYEVENTF_EXTENDEDKEY|KEYEVENTF_KEYUP,0);
}
}
return CallNextHookEx(hkb, nCode, wParam, lParam);
}

BOOL __declspec(dllexport) removehook()
{
return UnhookWindowsHookEx(hkb);
}

Can anyone please help me...

KeyboardProc was used before it was declared (C++ parses from the top down). You can either add a prototype before installhook(), or move the definition of KeyboardProc to be before installhook().

As for GetKeyState, you misspelled it:

#include<windows.h>

static HHOOK hkb=NULL;
HANDLE h;

BOOL __stdcall DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) 
{
    h=hModule;
    return TRUE;
}

LRESULT __declspec(dllexport)__stdcall KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) 
{
    short int state;

    if(nCode<0)
        return CallNextHookEx(hkb, nCode, wParam, lParam);

    if((nCode==HC_ACTION)&&((DWORD)lParam&0*40000000)) {
        state=GetKeyState(VK_CAPITAL);
        if((state&1)==0) {
            keybd_event(VK_CAPITAL,0,KEYEVENTF_EXTENDEDKEY,0);
            keybd_event(VK_CAPITAL,0,KEYEVENTF_EXTENDEDKEY|KEYEVENTF_KEYUP,0);
        }
    }
    return CallNextHookEx(hkb, nCode, wParam, lParam);
}

BOOL __declspec(dllexport)installhook() 
{
    hkb=SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)KeyboardProc, (HINSTANCE)h,0);
    if(hkb==NULL)
        return FALSE;

    return TRUE;
}

BOOL __declspec(dllexport) removehook() 
{
    return UnhookWindowsHookEx(hkb);
}

Thanx again..

did you look in either the Debug or Release folder, depending on which one you compiled? It should have generated by *.dll and *.lib files.

Yes I successfully generated the dll file. This is my first serious code with windows programming and I was trying to write a code which temporarily turns on the caps lock key. I have successfully compiled the code with zero errors, but it does not seem to be doing the job.

The code for dll file is same as that given above by Narue.
And the code for the executable goes like this:

#include<windows.h>
#include"helper.h"

HINSTANCE h;

void OnDestroy(HWND);
void OnCreate(HWND);

int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdline, int nCmdShow)
{
	MSG m;

	InitInstance(hInstance, nCmdShow,"Hook");

	while(GetMessage(&m,0,0,0))
		DispatchMessage(&m);

	return 0;
}


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
	{
	case WM_DESTROY:
		OnDestroy(hWnd);
		break;

	case WM_CREATE:
		OnCreate(hWnd);
		break;

	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}


void OnCreate(HWND hWnd)
{
	BOOL (CALLBACK *p)();

	h=LoadLibrary("hook.dll");
	if(h!=NULL)
	{
		p=GetProcAddress(h,"installhook");
		(*p)();
	}
}

void OnDestroy(HWND hWnd)
{
	BOOL(CALLBACK *p)();

	p=GetProcAddress(h,"removehook");
	(*p)();

	FreeLibrary(h);
	PostQuitMessage(0);
}

Can anyone please correct my code.
(helper.h could be copied from the following link:
http://www.kicit.com/sites/default/files/helper.h)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.