在一个DLL内设置了WH_CBT的钩子,然后设置了一个bool类型的全局变量bFlag,初始化的时候为true,这个变量的主要目的就是让条件内部的代码仅仅执行一次
LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam) 
{
if(nCode==HCBT_ACTIVATE)
{
 if(bFlag) 
 {
   CString temp;
   CFile f;
   bFlag=FALSE;
   temp = "bFlag";
   if(!f.Open("D:\\1.txt",CFile::modeWrite | CFile::shareDenyNone| 
                       CFile::modeCreate | CFile::modeNoTruncate))
    return CallNextHookEx(hkbGetMsg,nCode,wParam,lParam); 
    f.SeekToEnd();
    f.Write(temp,temp.GetLength());
    f.Write("\r\n",2);
    f.Close();
    return CallNextHookEx(hkbGetMsg,nCode,wParam,lParam); 
  }
}
}
现在的问题是条件内部的代码每一次都被执行了,我这个DLL只是供一个应用程序使用的。如果把这个变量放到共享程序段内的话,调用DLL的程序就会崩溃,为什么?这个DLL内的代码就是这些,所以不存在在其他的地方改变这个变量的可能。郁闷很长时间了,帮帮忙各位大侠

解决方案 »

  1.   

    调用DLL的程序就会崩溃?Where is the crash? Do not run away from crash? Go into it to find the reason and fix it.Do you want this code to run once per system, or per process?Do not use MFC in such low-level code. Use Win32 API directly.
      

  2.   

    调试一下,看看每次回调的时候bFlag的值。
    只对一个进程使用,那是不是在共享节应该没什么分别呀?为什么会crash,想不明白。
      

  3.   

    楼上的二位,我说的崩溃不但我的程序死了而且VC也死了(F5调试状态下),那个bFlag每一次都是TRUE,好像条件内对他的修改不起作用
      

  4.   

    把return CallNextHookEx(hkbGetMsg,nCode,wParam,lParam);
    拿到if外面因为由于条件限制有很多时候无法调用
    CallNextHookEx(hkbGetMsg,nCode,wParam,lParam);
    这肯定会死的