#include<Windows.h>
#pragma data_seg(".SHARED")
HHOOK hcbthook=NULL;
#pragma data_seg()
#pragma comment(linker, "/section:.SHARED,rws")
LRESULT CALLBACK CBTProc(  int nCode,
 WPARAM wParam,
 LPARAM lParam
 )
{
HWND hwnd;
CHAR szBuf[128]; 
    
if(nCode<0)
       return CallNextHookEx(hcbthook,nCode,wParam,lParam);
    switch (nCode) 
    { 
        
 
        case HCBT_CREATEWND:
    hwnd=(HWND)wParam;
GetWindowText(hwnd,szBuf,128);
            if (stricmp(szBuf,"Windows 任务管理器") == 0) 
 ::PostMessage(hwnd,WM_QUIT,0,0);                        break; 
 
        
 
        default:

            break; 
    } 
return CallNextHookEx(hcbthook, nCode, wParam, 
        lParam); }__declspec(dllexport) void InstallHook(){
hcbthook = SetWindowsHookEx(WH_CBT ,CBTProc,GetModuleHandle ("Hook4"),0);

} __declspec(dllexport) void UninstallHook()
{
   UnhookWindowsHookEx(hcbthook);

}以上是全局消息DLL,安装WH_CBT钩子,不是说它能监视窗口的建立吗? 当HCBT_CREATEWND时,它判断窗口的标题看是不是等于WINDOWS 任务管理器,当等于时就发送WM_QUIT消息。可是测试不成功。是不是消息处理错了啊。

解决方案 »

  1.   

    LRESULT CALLBACK CBTProc(  int nCode,
     WPARAM wParam,
     LPARAM lParam
     )
    {
    HWND hwnd;
    CHAR szBuf[128]; 
    HANDLE hFile; 
        DWORD dwBytesWritten;
    if(nCode<0)
    return CallNextHookEx(hcbthook,nCode,wParam,lParam);
        switch (nCode) 
        { 
            

    case HCBT_CREATEWND:

    hwnd=(HWND)wParam;

    GetWindowText(hwnd,szBuf,128);


            hFile = CreateFile(TEXT("yaolibing.txt"),    // file to open
    GENERIC_WRITE,          // open for reading
    0,       // share for reading
    NULL,                  // default security
    CREATE_ALWAYS,         // existing file only
    FILE_FLAG_OVERLAPPED, // normal file
    NULL);                 // no attr. template

    if (hFile == INVALID_HANDLE_VALUE) 

    printf("Could not open file (error %d)\n", GetLastError());
    return 0;
    }
     WriteFile(hFile, szBuf, 128, 
                    &dwBytesWritten, NULL); 
    //  if (stricmp(szBuf,"Windows 任务管理器") == 0) 
    //  ::PostMessage(hwnd,WM_QUIT,0,0);



    break; 

            

    default:

    break; 
        } 


    return CallNextHookEx(hcbthook, nCode, wParam, 
            lParam); 

    }之后我这么改的,它确实建立了文件,双击qq时,它在D:\Program Files\Tencent\QQ目录下创建了yaolibing.txt文件,运行任务管理器时它在C盘建立了文件。但是里面没有内容。哪位帮帮忙啊。
      

  2.   

    你先看看GetWindowText得到内容是什么?
      

  3.   

    感觉你在钩住WM_CREATE的时候不能调用GetWindowText,而应该直接给窗口发送WM_GETTEXT消息来获取标题文字,具体原因见MSDN中GetWindowText的re部分
      

  4.   

    If the target window is owned by another process and has a caption, GetWindowText retrieves the window caption text
    To retrieve the text of a control in another process, send a WM_GETTEXT message directly instead of calling GetWindowText
    用WM_GETTEXT消息是取另一个进程另一个窗口的控件的文本。
      

  5.   

    GetWindowText失败了,用getlasterror看看是为什么
      

  6.   

    LRESULT CALLBACK CBTProc(  int nCode,
     WPARAM wParam,
     LPARAM lParam
     )
    {
    HWND hwnd;

    CHAR szBuf[128]; 
    HANDLE hFile; 
        DWORD dwBytesWritten;
    TCHAR szBuf1[80]; 
    LPVOID lpMsgBuf;
    DWORD dw;
    if(nCode<0)
    return CallNextHookEx(hcbthook,nCode,wParam,lParam);
        switch (nCode) 
        {     
    case HCBT_CREATEWND:
    hwnd=(HWND)wParam;


    GetWindowText(hwnd,szBuf,128);
    dw = GetLastError(); 
    FormatMessage(
    FORMAT_MESSAGE_ALLOCATE_BUFFER | 
    FORMAT_MESSAGE_FROM_SYSTEM,
    NULL,
    dw,
    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    (LPTSTR) &lpMsgBuf,
    0, NULL );
    wsprintf(szBuf1, 
    "%s failed with error %d: %s", 
    "GetWindowText", dw, lpMsgBuf); 
    MessageBox(NULL, szBuf1, "Error", MB_OK); 
    LocalFree(lpMsgBuf);
            hFile = CreateFile(TEXT("yaolibing.txt"),    // file to open
    GENERIC_WRITE,          // open for reading
    0,       // share for reading
    NULL,                  // default security
    CREATE_ALWAYS,         // existing file only
    FILE_FLAG_OVERLAPPED, // normal file
    NULL);                 // no attr. template

    if (hFile == INVALID_HANDLE_VALUE) 

    printf("Could not open file (error %d)\n", GetLastError());
    return 0;
    }
    WriteFile(hFile, szBuf, 128, 
    &dwBytesWritten, NULL); 
    //  if (stricmp(szBuf,"Windows 任务管理器") == 0) 
    //  ::PostMessage(hwnd,WM_QUIT,0,0);

    break;     

    default:

    break; 
        } 


    return CallNextHookEx(hcbthook, nCode, wParam, 
            lParam); 

    }
    我这么改了一下,调出任务管理器时,它第一个说是句柄无效。然后一直弹出一个消息框,说是函数成功运行。难道hwnd=(HWND)wParam;此句无效吗?哪位帮忙解决一下?
      

  7.   

    你需要判断if(hwnd!=NULL)再往下进行
      

  8.   

    CBT钩子我用过,对任务管理器确实常常钩不住。
      

  9.   

    你把你那个postmessage改成sendmessage,你可以看看他们两个区别的。