diff --git a/native/MsgHook/MsgHook.cpp b/native/MsgHook/MsgHook.cpp index 2999281..20149a7 100644 --- a/native/MsgHook/MsgHook.cpp +++ b/native/MsgHook/MsgHook.cpp @@ -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 } diff --git a/native/MsgHook/MsgHookTest.aps b/native/MsgHook/MsgHookTest.aps index 1bcb0db..ab709a6 100644 Binary files a/native/MsgHook/MsgHookTest.aps and b/native/MsgHook/MsgHookTest.aps differ diff --git a/native/MsgHook/MsgHookTest.rc b/native/MsgHook/MsgHookTest.rc index 45f3912..8aaa3ec 100644 Binary files a/native/MsgHook/MsgHookTest.rc and b/native/MsgHook/MsgHookTest.rc differ diff --git a/native/MsgHook/MsgHookWindow.cpp b/native/MsgHook/MsgHookWindow.cpp index cd7ed7b..7d975b9 100644 --- a/native/MsgHook/MsgHookWindow.cpp +++ b/native/MsgHook/MsgHookWindow.cpp @@ -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(); } diff --git a/native/MsgHook/MsgLookup.h b/native/MsgHook/MsgLookup.h index f8413b3..e432123 100644 --- a/native/MsgHook/MsgLookup.h +++ b/native/MsgHook/MsgLookup.h @@ -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; diff --git a/native/MsgHook/bin/MsgHook32.dll b/native/MsgHook/bin/MsgHook32.dll index e08c44d..c5b0807 100644 Binary files a/native/MsgHook/bin/MsgHook32.dll and b/native/MsgHook/bin/MsgHook32.dll differ diff --git a/native/MsgHook/bin/MsgHook64.dll b/native/MsgHook/bin/MsgHook64.dll index 11297ae..780332f 100644 Binary files a/native/MsgHook/bin/MsgHook64.dll and b/native/MsgHook/bin/MsgHook64.dll differ diff --git a/native/MsgHook/resource.h b/native/MsgHook/resource.h index 8f5c0a2..efdecfc 100644 Binary files a/native/MsgHook/resource.h and b/native/MsgHook/resource.h differ