编译成dll正常,dll的工程主要函数如下BOOL  __declspec(dllexport)__stdcall installHook()
{
   hkb=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)keyboardProc,hins,0);
   return TRUE;
}
LRESULT __declspec(dllexport)__stdcall CALLBACK keyboardProc(int nCode,WPARAM wParam,LPARAM lParam)
{
if(((DWORD)lParam&0x40000000)&&(HC_ACTION==nCode))
{
switch(wParam) {
case 'a':
sndPlaySound("1.wav",SND_ASYNC);
break;
case 'b' :
sndPlaySound("2.wav",SND_ASYNC);
break;
default:
sndPlaySound("3.wav",SND_ASYNC);
}

}
LRESULT Retval=CallNextHookEx(hkb,nCode,wParam,lParam);
return Retval;
}在对话框initialize中
CreateMutex(NULL,FALSE,"KeyHook");
if(GetLastError()==ERROR_ALREADY_EXISTS)
        return false;   
static HINSTANCE hInstancedll;
typedef BOOL(CALLBACK *inshook)();
inshook instkhook;
if(hInstancedll=LoadLibrary((LPCSTR)"hookkeysound.dll"))
{
instkhook=(inshook)GetProcAddress(hInstancedll,"installHook");
instkhook();
}
else
{
AfxMessageBox("当前目录找不到hookkeysound.dll文件,程序初始化失败");
 
}把dll和1.wav,2.wav,3.wav考入生成的exe目录
按各个健都没有声音,怎么检测毛病在哪里呀???
谢谢

解决方案 »

  1.   

    you can consult vckbase example 
    http://www.vckbase.com/document/viewdoc.asp?id=271
      

  2.   

    do you notice these code?
    GetModuleFileName(AfxGetInstanceHandle( ),szBuf,sizeof(szBuf));
        p = szBuf;
        while(strchr(p,'\\')) 

       p = strchr(p,'\\'); 
       p++; 
    }
        *p = '\0'; 
        msg=szBuf;
    //then you should change your code to this
    if(((DWORD)lParam&0x40000000)&&(HC_ACTION==nCode))
    {
    switch(wParam) {
    case 'a':
    sndPlaySound(msg+"1.wav",SND_ASYNC);
    break;
    case 'b' :
    sndPlaySound(msg+"2.wav",SND_ASYNC);
    break;
    default:
    sndPlaySound("3.wav",SND_ASYNC);
    }

    }
      

  3.   

    这段没有,不是用来相对路径用得吗?
    我的已经把*.wav放在exe的目录,不用这个吧??
    GetModuleFileName(AfxGetInstanceHandle( ),szBuf,sizeof(szBuf));
        p = szBuf;
        while(strchr(p,'\\')) 

       p = strchr(p,'\\'); 
       p++; 
    }
        *p = '\0'; 
        msg=szBuf;
      

  4.   

    yes ,you should get opposite directory,otherwise it will not find the resouce
      

  5.   

    按作者说:将编译生成后的KeySound.exe和Hook.dll放在同一目录下,定义好声音文件,运行KeySound.exe后打开记事本或写字板,体验一下系统为您即时快速地朗读您按下的每一个键的快感吧^-^
    然后
    他再说GetModuleFileName(AfxGetInstanceHandle( ),szBuf,sizeof(szBuf));
    的,所以,在我的程序,还没必要用GetModuleFileName 把??
      

  6.   

    但我用绝对路径也不行啊,我把"1.wav",改成"c:\\1.wav"了,还是老样子啊
      

  7.   

    你可以不用相对路径,但你也要使用绝对路径.sndPlaySound("1.wav",SND_ASYNC);你这样写系统sndPlaySound是根本不会知道资源路径的.
      

  8.   

    我已经改成sndPlaySound("c:\\1.wav",SND_ASYNC)了,可是还是老样子啊
    ,我只是想式一下钩子的威力,结果搞了一晚
      

  9.   

    i have tested ,and you can use absolute directory.you check it carefully.
      

  10.   

    you can debug it by this way:
    1.replace sndPlaySound("c:\\1.wav",SND_ASYNC) with AfxMessgageBox("test");...
    2.write a test program (such as MFC Dialog program)
    then write :
    sndPlaySound("c:\\1.wav",SND_ASYNC); check whether it have sound.
      

  11.   

    我就是写了个diolog测试她的阿,let me see第一种方法
      

  12.   

    根本弹不出AfxMessgageBox("test")阿,难道没有引用到函数?
    可是核心代码一样的阿
      

  13.   

    do you hear sound when you run its Keysound.exe?
      

  14.   

    没有啊,我把核心代码和例子改成一样
    static HINSTANCE hinstDLL; 
    typedef BOOL (CALLBACK *inshook)(); 
    inshook instkbhook;
    if(hinstDLL=LoadLibrary((LPCTSTR)"hookkeyboard.dll"))
    {
    instkbhook=(inshook)GetProcAddress(hinstDLL, "installhook"); 
    instkbhook();
    }
    结果还是一样
      

  15.   

    I have change it ,and it runs well, please check your e-mail now.
      

  16.   

    ok,thx,let me see, and ,what 's the problem?
      

  17.   

    oh,my god太神了,怎么弄得啊?
      

  18.   

    1.hins have not been initialized?
    so add 
    hins=AfxGetInstanceHandle();
    2.in exe,your code have error.so i changed
    if(hinstDLL=LoadLibrary((LPCTSTR)"hookkeysound.dll"))
    {
    instkbhook=(inshook)GetProcAddress(hinstDLL, "installHook"); 
    instkbhook();
    }
      

  19.   

    hins=AfxGetInstanceHandle();              用来实例化
    hins把?
      

  20.   

    hins=AfxGetInstanceHandle();              用来实例化
    hins把?想不到还有这个函数的,太谢谢了,给分,
    咱交个朋友吧:)
      

  21.   

    不知道你的dll中有没有
    BOOL CHookApp::InitInstance() 
    {
    hins = AfxGetInstanceHandle();

    }你先确认钩子是否已安装。
    获得module路径函数是一定要使用的。
      

  22.   

    oh, mostneed(往↓看) 晚点了,刚结贴,下次给分给你把,谢谢:)