将 lpcs->lpszName 与 WH_CBT 回调中的 "OLEChannelWnd" 等其他值进行比较时出现问题

Issue when comparing lpcs->lpszName with other value like "OLEChannelWnd" in a WH_CBT callback

下面是我的 WH_CBT 回调的代码。我试图忽略对 Outlook 中超链接的任何点击。当我在 Outlook 中单击超链接时,出现一个消息框:

但是当我在没有if条件的回调中return1时,它工作正常。

LRESULT __declspec(dllexport)__stdcall  CALLBACK GetCBTProc(int nCode, WPARAM wParam, LPARAM lParam)
{

    HRESULT hResult;
    

    if (nCode < 0)
        CallNextHookEx(hkb, nCode, wParam, lParam);

    HWND parentWin;
    DWORD ChiledThreadID;
    DWORD parentProcessID = 0;
    std::ostringstream streamcb;
    DWORD xx = (DWORD)15028;
    HWND hCurrWnd;

    TCHAR  clsName_v[22];
    TCHAR  className[22]="OleMainThreadWndClass";
    LPCSTR oleName2 = _T("OLEChannelWnd");
    int number;
    f1 = fopen("C:\Log\report.txt", "a+");
    if (nCode == HCBT_CREATEWND)
    {
        hCurrWnd = (HWND)wParam;
        parentWin = GetAncestor(hCurrWnd, GA_ROOT);
        ChiledThreadID = GetWindowThreadProcessId(parentWin, &parentProcessID);
        if (parentProcessID == xx)
        {
            writetofile("HCBT_CREATEWND", 15, f1);

            CBT_CREATEWNDA* cw = (CBT_CREATEWNDA*)lParam;
            CREATESTRUCTA* lpcs = (CREATESTRUCTA*)cw->lpcs;

            number = GetClassName(hCurrWnd, clsName_v, 22);
            writetofile(clsName_v, number, f1);

            if (lpcs->lpszName)
            {
                if (CompareString(LOCALE_SYSTEM_DEFAULT, 0, lpcs->lpszName, -1, _T("OLEChannelWnd"), 13) == CSTR_EQUAL)
                {
                    return 1;
                }
            }
        }
    }
    fclose(f1);

    LRESULT RetVal = CallNextHookEx(hkb, nCode, wParam, lParam);

    return  RetVal;

}

您的代码缺少两个 return 语句,一个在 CallNextHookEx() 上,一个在回调结束时。因此,回调的 return 值为 indeterminate 除非输入字符串符合您的条件。你的编译器应该警告你第二个缺失 return。具有非 void return 类型的函数必须以 return <value>; 语句退出。

试试这个:

LRESULT __declspec(dllexport) CALLBACK GetCBTProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    if (nCode == HCBT_CREATEWND)
    {
        HWND hCurrWnd = (HWND) wParam;
        HWND parentWin = GetAncestor(hCurrWnd, GA_ROOT);
        DWORD parentProcessID = 0;
        DWORD ChildThreadID = GetWindowThreadProcessId(parentWin, &parentProcessID);
        const DWORD xx = 8108;

        if (parentProcessID == xx)
        {
            FILE *f1 = fopen("C:\Log\report.txt", "a+");

            writetofile("HCBT_CREATEWND", 14, f1);

            CBT_CREATEWND* cw = (CBT_CREATEWND*) lParam;
            CREATESTRUCT* lpcs = (CREATESTRUCT*) cw->lpcs;

            TCHAR clsName_v[22] = {};
            int number = GetClassName(hCurrWnd, clsName_v, 22);
            writetofile(clsName_v, number, f1);

            fclose(f1);

            if (lpcs->lpszName)
            {
                if (CompareString(LOCALE_SYSTEM_DEFAULT, 0, lpcs->lpszName, -1, _T("OLEChannelWnd"), 13) == CSTR_EQUAL)
                {
                    return 1;
                }
            }
        }
    }

    return CallNextHookEx(hkb, nCode, wParam, lParam);
}