Updates to Message hooking to fix changes in filtering and added new custom filters

This commit is contained in:
Edward Jakubowski
2014-05-28 23:23:15 -04:00
parent da3326a5e4
commit 05748b4582
8 changed files with 56 additions and 21 deletions

View File

@@ -32,8 +32,28 @@ LRESULT CALLBACK CwpHookProc(int nCode, WPARAM wParam, LPARAM lParam)
Event.dwHookType = WH_CALLWNDPROC;
memset((void *)&Event.wParamStr, '\0', sizeof(TCHAR) * 25);
memset((void *)&Event.lParamStr, '\0', sizeof(TCHAR) * 25);
//if (cwps->message == WM_SETTEXT && cwps->lParam != 0)
// _tcscpy_s(Event.lParamStr, 25, (const wchar_t*)Event.lParam);
bool errorFlg = false;
if (cwps->message == WM_SETTEXT && cwps->lParam != 0 && cwps->wParam == 0)
{
if (IsWindowUnicode(Event.hWnd))
{
_tcsncpy_s(Event.lParamStr, 25, (const wchar_t*)Event.lParam, _TRUNCATE);
}
else
{
int asciiSize = (int)strlen((const char*)Event.lParam);
int unicodeSize = (int)_tcslen((const wchar_t*)Event.lParam);
if (unicodeSize > asciiSize)
_tcsncpy_s(Event.lParamStr, 25, (const wchar_t*)Event.lParam, _TRUNCATE);
else
{
int tstrLen = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)cwps->lParam, (int)strlen((LPCSTR)cwps->lParam), NULL, 0); //get t len
if (tstrLen > 24)
tstrLen = 24;
MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)cwps->lParam, (int)strlen((LPCSTR)cwps->lParam), Event.lParamStr, tstrLen); // convert char to tchar
}
}
}
BOOL bRes = (BOOL)SendMessage(pData->g_hWnd, WM_COPYDATA, 0, (LPARAM)(VOID*)&CDS); // ask the controlling program if the hook should be passed
}

Binary file not shown.

Binary file not shown.

View File

@@ -29,18 +29,20 @@ const int txtboxSpacing = 2;
bool filterWmCommand = true;
bool filterWmNotify = false;
bool filterCustom = false;
bool filterAbove = false;
//#define MAX_TEST_SIZE 100
//TCHAR targetClassname[MAX_TEST_SIZE] = _T("Notepad");
TCHAR targetClassname[MAX_TEST_SIZE] = _T("WordPadClass");
TCHAR targetHwndStr[MAX_TEST_SIZE] = _T("");
TCHAR testWmSettextL[MAX_TEST_SIZE] = _T("This is a test");
TCHAR testWmSettextW[MAX_TEST_SIZE] = _T("0");
TCHAR testWmCommandL[MAX_TEST_SIZE] = _T("0");
TCHAR testWmCommandW[MAX_TEST_SIZE] = _T("1");
TCHAR targetHwndStr[MAX_TEST_SIZE] = _T("");
TCHAR customMsgStr[MAX_TEST_SIZE] = _T("WM_SETTEXT");
const int hotkeyIdOffset = 0;
const int pauseHotKey = 'P'; //P
@@ -75,12 +77,12 @@ void AppendText(HWND txtHwnd, LPCTSTR newText)
void InitMsgFiltersAndLookup()
{
if (!filterWmCommand && !filterAbove && !filterWmNotify)
if (!filterWmCommand && !filterAbove && !filterWmNotify && !filterCustom)
InitializeMsgLookup();
else
{
int allowList[3];
for (int i = 0; i < 3; i ++)
int allowList[4];
for (int i = 0; i < 4; i ++)
allowList[i] = -1;
if (filterWmCommand) {
@@ -95,7 +97,20 @@ void InitMsgFiltersAndLookup()
}
//if (filterAbove)
// allowList[0] = WM_COMMAND;
InitializeMsgLookup(allowList, 3);
if (filterCustom && _tcslen(customMsgStr) > 0)
{
InitializeMsgLookup(); //initialize full msg list and do reverse lookup based on custom filter string
for (int x = 0; x < MAX_MSG_LOOKUP; x++)
{
if (_tcscmp(customMsgStr, MSG_LOOKUP[x]) == 0) {
TCHAR tmp[100];
_stprintf_s(tmp, _T("filtering on %s (%d)\r\n"), customMsgStr, x);
AppendText(txtbox, tmp);
allowList[3] = x;
}
}
}
InitializeMsgLookup(allowList, 4);
}
}
@@ -178,8 +193,10 @@ bool OnCopyData(COPYDATASTRUCT* pCopyDataStruct) // WM_COPYDATA lParam will have
}
if (_tcscmp(msgName, _T("")) != 0)
{
TCHAR msgHwndClassname[20];
GetClassName(Event.hWnd, msgHwndClassname, 20);
TCHAR tmp[200];
_stprintf_s(tmp, _T("hwnd: %ld, msg: %s (%ld), wparam: '%s'[%ld], lparam: '%s'{%ld}\r\n"), Event.hWnd, msgName, Event.nCode, Event.wParamStr, Event.wParam, Event.lParamStr,Event.lParam);
_stprintf_s(tmp, _T("hwnd: %ld (%s), msg: %s (%ld), wparam: '%s'[%ld], lparam: '%s'{%ld}\r\n"), Event.hWnd, msgHwndClassname, msgName, Event.nCode, Event.wParamStr, Event.wParam, Event.lParamStr,Event.lParam);
AppendText(txtbox, tmp);
}
}
@@ -458,12 +475,14 @@ INT_PTR CALLBACK DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
SendDlgItemMessage(hDlg, IDC_CHECK_NOT, BM_SETCHECK, BST_CHECKED, 0);
if (filterAbove)
SendDlgItemMessage(hDlg, IDC_CHECK_ABO, BM_SETCHECK, BST_CHECKED, 0);
if (filterCustom)
SendDlgItemMessage(hDlg, IDC_CUSTOMCHK, BM_SETCHECK, BST_CHECKED, 0);
SendDlgItemMessage(hDlg, IDC_WMCOMW, WM_SETTEXT, 0 , (LPARAM)testWmCommandW);
SendDlgItemMessage(hDlg, IDC_WMCOML, WM_SETTEXT, 0 , (LPARAM)testWmCommandL);
SendDlgItemMessage(hDlg, IDC_WMSETW, WM_SETTEXT, 0 , (LPARAM)testWmSettextW);
SendDlgItemMessage(hDlg, IDC_WMSETL, WM_SETTEXT, 0 , (LPARAM)testWmSettextL);
SendDlgItemMessage(hDlg, IDC_HWND, WM_SETTEXT, 0 , (LPARAM)targetHwndStr);
SendDlgItemMessage(hDlg, IDC_CUSTOMMSG, WM_SETTEXT, 0 , (LPARAM)customMsgStr);
}
return (INT_PTR)TRUE;
@@ -476,19 +495,12 @@ INT_PTR CALLBACK DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
GetDlgItemText(hDlg, IDC_WMSETW, testWmSettextW, MAX_TEST_SIZE);
GetDlgItemText(hDlg, IDC_WMSETL, testWmSettextL, MAX_TEST_SIZE);
GetDlgItemText(hDlg, IDC_HWND, targetHwndStr, MAX_TEST_SIZE);
GetDlgItemText(hDlg, IDC_CUSTOMMSG, customMsgStr, MAX_TEST_SIZE);
// check filter options
if (SendDlgItemMessage(hDlg, IDC_CHECK_CMD, BM_GETCHECK, 0, 0) == BST_CHECKED) // the hard way
filterWmCommand = true;
else
filterWmCommand = false;
if (IsDlgButtonChecked(hDlg, IDC_CHECK_NOT) == BST_CHECKED) // the easy way
filterWmNotify = true;
else
filterWmNotify = false;
if (IsDlgButtonChecked(hDlg, IDC_CHECK_ABO) == BST_CHECKED)
filterAbove = true;
else
filterAbove = false;
filterWmCommand = (SendDlgItemMessage(hDlg, IDC_CHECK_CMD, BM_GETCHECK, 0, 0) == BST_CHECKED); // the hard way
filterWmNotify = (IsDlgButtonChecked(hDlg, IDC_CHECK_NOT) == BST_CHECKED);// the easy way
filterAbove = (IsDlgButtonChecked(hDlg, IDC_CHECK_ABO) == BST_CHECKED);
filterCustom = (IsDlgButtonChecked(hDlg, IDC_CUSTOMCHK) == BST_CHECKED);
InitMsgFiltersAndLookup();
}

View File

@@ -20,7 +20,10 @@ void InitializeMsgLookup(int allowList[], int allowSize)
if (allowList[a] == i)
allowFlg = true;
if (!allowFlg)
{
memset((void *)&MSG_LOOKUP[i], '\0', sizeof(TCHAR) * MAX_MSG_NAME); //blank it
continue;
}
switch (i)
{
case WM_NULL: _tcscpy_s(MSG_LOOKUP[i], _T("WM_NULL")); break;

Binary file not shown.

Binary file not shown.

Binary file not shown.