简体   繁体   English

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

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

Below is the code of my WH_CBT callback.下面是我的WH_CBT回调的代码。 I am trying to ignore any click of a hyperlink in Outlook.我试图忽略 Outlook 中超链接的任何点击。 When I click a hyperlink in Outlook, I'm getting a message box:当我单击 Outlook 中的超链接时,我收到一个消息框:

图片

But when I return 1 in the callback without the if condition, it works fine.但是当我在没有if条件的回调中返回1时,它工作正常。

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;

}

Your code is missing two return statements, one on the CallNextHookEx() , and one at the end of the callback.您的代码缺少两个return语句,一个在CallNextHookEx()上,一个在回调结束时。 So, the return value of the callback is indeterminate unless the input string matches your criteria.因此,除非输入字符串符合您的条件,否则回调的返回值是不确定的。 Your compiler should have warned you about the second missing return .您的编译器应该警告您第二次丢失的return A function with a non- void return type must exit with a return <value>;具有非void返回类型的 function 必须以return <value>; statement.陈述。

Try this instead:试试这个:

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WH_CBT钩另一个线程在XP中工作,但Win7 - WH_CBT hook another thread works in XP but Win7 SetWindowsHookEx 不适用于另一个进程线程 ID 上的 WH_CBT(但适用于自己的线程 ID) - SetWindowsHookEx not working with WH_CBT on another process thread ID (but works on own thread ID) SetWindowsHookEx + WH_CBT不起作用? 或者至少不是我认为应该的方式? - SetWindowsHookEx + WH_CBT doesn't work? Or at least not the way I think it should? 使用 WinAPI WH_CALLWNDPROC 挂钩时不调用回调方法 - Callback method is not called when using WinAPI WH_CALLWNDPROC hook 使用STL排序时的比较方法问题 - Comparing method issue when using STL sort Windows(C ++)中的WH_JOURNALRECORD挂钩-从未调用过。 - WH_JOURNALRECORD hook in Windows (C++) - Callback never called. 当程序失去焦点时,WH_FOREGROUNDIDLE停止 - WH_FOREGROUNDIDLE stops when program loses focus 比较字符串时,OR 语句的组合不会产生正确的 boolean 值 - Composition of OR statements does not yield the correct boolean value when comparing strings 当1个字符串包含另一个字符串的一部分时比较字符串 - comparing strings when 1 string contains part of the other string 当窗口焦点丢失时,SetWindowsHookEx 停止接收 WH_MOUSE_LL 事件 - SetWindowsHookEx stops receiving WH_MOUSE_LL events when window focus lost
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM