[英]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.