HWND g_hWnd;
HHOOK g_hCBTProc = NULL;
LRESULT CALLBACK CBTProc(int nCode,WPARAM wParam,LPARAM lParam)
{
     if (HCBT_CREATEWND == nCode)
{
g_hWnd = (HWND)wParam;
} return CallNextHookEx(g_hCBTProc,nCode,wParam,lParam); //传递钩子信息 

}
HWND SetHook()
{
g_hCBTProc = SetWindowsHookEx(WH_CBT, CBTProc, GetModuleHandle("Hook"), 0);
return g_hWnd;
}
我这里的g_hWnd是为了获得WM_CREATE消息创建的桌面窗口,怎么每次还是返回NULL的呢,,

解决方案 »

  1.   

    你在SetHook的时候g_hWnd就是NULL啊,CBTProc也不可能在SetHook之前执行啊
      

  2.   

    SetWindowsHookEx(WH_CBT...
    create....
      

  3.   

    对啊,我安装钩子之后在钩子函数里面不是把g_hWnd赋值了吗
    g_hWnd不就是新创建窗口的句柄吗
      

  4.   

    CBT_CREATEWND
    The CBT_CREATEWND structure contains information passed to a WH_CBT hook procedure, CBTProc, before a window is created. typedef struct tagCBT_CREATEWND { 
        LPCREATESTRUCT lpcs; 
        HWND           hwndInsertAfter; 
    } CBT_CREATEWND, *LPCBT_CREATEWND; 
    Members
    lpcs 
    Pointer to a CREATESTRUCT structure that contains initialization parameters for the window about to be created. 
    hwndInsertAfter 
    Handle to the window whose position in the Z order precedes that of the window being created. 
      

  5.   

    if (HCBT_CREATEWND == nCode)
    {
    LPCBT_CREATEWND p = (LPCBT_CREATEWND)lParam;
    g_hWnd = p->hwndInsertAfter; //g_hWnd = (HWND)wParam; char buff[30];
    LPCSTR lpstr;
    lpstr = buff; ::SetWindowText(g_hWnd, lpstr);
    } 我这样修改了还是没用,现在的测试目的就是截获新创建的消息,并把新创建窗口的标题栏的text设置为空
      

  6.   

    看错,不好意思才明白你想干什么,你是想在监视到窗口创建以后修改窗口的标题吧?我记得在这个钩子里这样改是不行的,因为这时候这个窗口还没有真正创建,你SetWindowText是没用的怎么解决这个问题我忘记了,以前肯定有过这样的帖子,就是时间有点久了
      

  7.   

    hook 窗口过程,而不是 CBT
    bbs.aisnote.com