简体   繁体   中英

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

Below is the code of my WH_CBT callback. I am trying to ignore any click of a hyperlink in Outlook. When I click a hyperlink in Outlook, I'm getting a message box:

图片

But when I return 1 in the callback without the if condition, it works fine.

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. 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 . A function with a non- void return type must exit with a 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM