Click here to Skip to main content
15,894,180 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: HRESULT variable returning negative value Pin
Richard MacCutchan29-Jun-19 7:23
mveRichard MacCutchan29-Jun-19 7:23 
GeneralRe: HRESULT variable returning negative value Pin
jschell30-Jun-19 7:14
jschell30-Jun-19 7:14 
GeneralRe: HRESULT variable returning negative value Pin
Richard MacCutchan30-Jun-19 10:14
mveRichard MacCutchan30-Jun-19 10:14 
AnswerRe: HRESULT variable returning negative value Pin
Stefan_Lang30-Jun-19 22:09
Stefan_Lang30-Jun-19 22:09 
PraiseRe: HRESULT variable returning negative value Pin
Member 144995631-Jul-19 4:05
Member 144995631-Jul-19 4:05 
GeneralRe: HRESULT variable returning negative value Pin
Stefan_Lang1-Jul-19 21:31
Stefan_Lang1-Jul-19 21:31 
GeneralRe: HRESULT variable returning negative value Pin
Member 144995632-Jul-19 6:29
Member 144995632-Jul-19 6:29 
QuestionHook stops application Pin
Mark_G0027-Jun-19 5:34
Mark_G0027-Jun-19 5:34 
Hello!

I'm new to the topic "Hooks" and a lot of C++ is new to me. I working on a small application to capture messages, which are send to a "log" window of another application. First I used Spy++ to analyze the messages, which showed me a bunch of WM_SETTEXT messages and that's what I want.

from Spy++:
<000001> 0000000000021E4E S WM_SETTEXT lpsz:03B50000 ("18.18s [INFO]: NC: ;Note...Rev: C00 - MAA...")

I created a DLL, which is injected into the application and a console application to create the hook. At the moment the DLL is just writing some data(Handle, Msg, ThreadID, Title) into a text file and I see, that something is getting logged, which belongs to the same thread, but the part I need is missing. When I start the application the log window and it's app freezes. Normally I see text getting added all the time. Once I close the app, it continues.

I'm using Visual Studio 2015 on Win7 64bit. Apps are all 64bit.

This is the DLL code:
HHOOK hHook = 0;

extern "C" __declspec(dllexport) LRESULT CALLBACK GetMsgProc(INT nCode, WPARAM wParam, LPARAM lParam)
{   
    
    
   // The lParam parameter contains a structure that provides information 
   // about the message being sent. 

   if (nCode < 0)
   { /* pass it on */
      CallNextHookEx(hHook, nCode, wParam, lParam);
      return 0;
   } /* pass it on */

   CWPSTRUCT* pCwp = (CWPSTRUCT*)lParam; // for WH_CALLWNDPROC

   
   // Get/Convert Window Text
   TCHAR tcWinTitle[255];
   int len = GetWindowTextLength(pCwp->hwnd) + 1;  
   GetWindowText(pCwp->hwnd, tcWinTitle, len);      
   std::wstring wStr = tcWinTitle;
   std::string strWinTitle = std::string(wStr.begin(), wStr.end());


   // Convert Window Handle
   TCHAR tcWinHandle[256];
   wsprintf(tcWinHandle, L"0x%08p", pCwp->hwnd);
   wStr = tcWinHandle;
   std::string strWinHandle = std::string(wStr.begin(), wStr.end());


   // Get Process ID
   DWORD dwThreadId = GetWindowThreadProcessId(pCwp->hwnd, NULL);


   // Write Results to file
   ofstream myfile;
   myfile.open("c:\\temp\\example.txt", ios::app);
   myfile << " Handle ::: " << strWinHandle << "  Msg ::: " << std::to_string(pCwp->message) << "  Title ::: " << strWinTitle << "  ThreadID ::: " << std::to_string(dwThreadId) << "\n";
   myfile.close();

   
   HWND hBingo = FindWindow(L"#32770", L"Bingo");   

   if (hBingo == 0) 
   {
      HWND hWndOutputParent = FindWindow(L"#32770", L"[Output Window]");
      HWND hWndOutput = FindWindowEx(hWndOutputParent, 0, L"Edit", NULL);
      
      // Check for Message and Handle
      if ((pCwp->message == WM_SETTEXT) && (pCwp->hwnd == hWndOutput))
      {         
         MessageBox(hWndOutput, L"Got it", L"Bingo", MB_OK);
         return 0;
      }
   }  

   // Call next Hook
   return CallNextHookEx(hHook, nCode, wParam, lParam);

This is my console application:
int main()
{  
   HMODULE dll = LoadLibrary(L"C:\\Projects\\ConsoleApplication1\\x64\\Debug\\WinMsgHookLib.dll");
   if (dll == NULL) {
      printf("The DLL could not be found.\n");
      getchar();
      return -1;

   }

   /*
   * Get the address of the function inside the DLL.
   */
   HOOKPROC addr = (HOOKPROC)GetProcAddress(dll, "GetMsgProc");
   //HOOKPROC addr = (HOOKPROC)GetProcAddress(dll, "_GetMsgProc@12"); // for 32bit
   if (addr == NULL) {
      printf("The function was not found.\n");
      getchar();
      return -1;
   }


   HWND hWndOutputParent = FindWindow(L"#32770", L"[Output Window]");
   HWND hWndOutput = FindWindowEx(hWndOutputParent, 0, L"Edit", NULL);

   DWORD dwThreadId = GetWindowThreadProcessId(hWndOutput, NULL);



   /*
   * Hook the function.
   */
   HHOOK handle = SetWindowsHookEx(WH_CALLWNDPROC, addr, dll, dwThreadId);
   if (handle == NULL) {
      printf("Messages could not be hooked.\n");
      return -1;
   }

   /*
   * Unhook the function.
   */
   printf("Program successfully hooked.\nPress enter to unhook the function and stop the program.\n");
   getchar();
   UnhookWindowsHookEx(handle);

   return 0;
}

Thank you very much for your help!

Mark
AnswerRe: Hook stops application Pin
leon de boer27-Jun-19 7:13
leon de boer27-Jun-19 7:13 
GeneralRe: Hook stops application Pin
Mark_G0028-Jun-19 3:23
Mark_G0028-Jun-19 3:23 
GeneralRe: Hook stops application Pin
Victor Nijegorodov28-Jun-19 4:50
Victor Nijegorodov28-Jun-19 4:50 
GeneralRe: Hook stops application Pin
Mark_G0028-Jun-19 6:41
Mark_G0028-Jun-19 6:41 
GeneralRe: Hook stops application Pin
Victor Nijegorodov28-Jun-19 7:45
Victor Nijegorodov28-Jun-19 7:45 
GeneralRe: Hook stops application Pin
Mark_G0028-Jun-19 9:03
Mark_G0028-Jun-19 9:03 
GeneralRe: Hook stops application Pin
leon de boer28-Jun-19 20:59
leon de boer28-Jun-19 20:59 
GeneralRe: Hook stops application Pin
Mark_G0029-Jun-19 3:36
Mark_G0029-Jun-19 3:36 
QuestionImporting enhanced metafile from ms word to MFC problem Pin
zakomed24-Jun-19 3:55
zakomed24-Jun-19 3:55 
QuestionCStringList on heap Pin
_Flaviu18-Jun-19 23:38
_Flaviu18-Jun-19 23:38 
QuestionRe: CStringList on heap Pin
CPallini19-Jun-19 2:19
mveCPallini19-Jun-19 2:19 
AnswerRe: CStringList on heap Pin
_Flaviu19-Jun-19 19:36
_Flaviu19-Jun-19 19:36 
GeneralRe: CStringList on heap Pin
CPallini20-Jun-19 4:49
mveCPallini20-Jun-19 4:49 
AnswerRe: CStringList on heap Pin
Maximilien19-Jun-19 3:43
Maximilien19-Jun-19 3:43 
GeneralRe: CStringList on heap Pin
_Flaviu19-Jun-19 19:38
_Flaviu19-Jun-19 19:38 
GeneralRe: CStringList on heap Pin
Richard MacCutchan19-Jun-19 21:07
mveRichard MacCutchan19-Jun-19 21:07 
GeneralRe: CStringList on heap Pin
Stefan_Lang19-Jun-19 23:20
Stefan_Lang19-Jun-19 23:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.