Thread: Intercep Messages to Windows, or View Them

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    54

    Intercep Messages to Windows, or View Them

    Hello. I am using C and Win32 API. I'm trying to find a way to view the message queue of a certain window that receives text and would like to see the text before it shows up on the window. My idea is to look at all the WM_CHAR messages a certain window receives, and then convert those to strings.
    Any suggestions on how to start? I know the handle of the window, and can get the thread that it belongs to.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    You need to be more specific.
    • Is this your application?
    • If it's not your application (and you don't have the source code) then you should explain why you want to do this so that we know you are not in breach of good conduct (forum guidelines, rule 6).
    • Is the window in question a system window class (eg. an 'edit' control)?
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    54
    it is my application. I'm trying to stream the contents of an Edit Control box.

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Sounds like what you want to do is a bit of sub-classing, have a play with this. NewEditProc() receives all the messages and either processes them, or passes them on unmodified to the original procedure.
    Code:
    #include <windows.h>
    
    LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);
    LRESULT CALLBACK NewEditProc(HWND, UINT, WPARAM, LPARAM);
    
    HINSTANCE hInst;
    WNDPROC OriginalEditProc;
    
    int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, 
                       LPSTR Args, int WinMode)
    {
        HWND hWnd;
        MSG Message;
        WNDCLASSEX Wcl;
    
        hInst = hThisInst;
        
        Wcl.cbSize = sizeof(WNDCLASSEX);    
        Wcl.hInstance = hThisInst;
        Wcl.lpszClassName = "Window";
        Wcl.lpfnWndProc = WindowFunc; 
        Wcl.style = 0;  
        Wcl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        Wcl.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
        Wcl.hCursor = LoadCursor(NULL, IDC_ARROW);
        Wcl.lpszMenuName = NULL; 
        Wcl.cbClsExtra = 0; 
        Wcl.cbWndExtra = 0;
        Wcl.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH); 
        
        if(!RegisterClassEx(&Wcl)) return 0;
        
        hWnd = CreateWindow(
            "Window", 
            "Window",
            WS_OVERLAPPEDWINDOW, 
            CW_USEDEFAULT, 
            CW_USEDEFAULT, 
            CW_USEDEFAULT, 
            CW_USEDEFAULT, 
            HWND_DESKTOP, 
            NULL, 
            hThisInst, 
            NULL 
            );
        
        ShowWindow(hWnd, WinMode);
        UpdateWindow(hWnd);
    
        while(GetMessage(&Message, NULL, 0, 0))
        {
            TranslateMessage(&Message); 
            DispatchMessage(&Message);
        }
        return Message.wParam;
    }
    
    LRESULT CALLBACK WindowFunc(HWND hWnd,UINT Message,WPARAM wParam,LPARAM lParam)
    {
        static HWND hEditBox;
        
        switch(Message)
        {
        case WM_CREATE:
            hEditBox = CreateWindowEx(0,
                                      "EDIT",
                                      NULL,
                                      WS_CHILD | WS_VISIBLE | WS_BORDER,
                                      10,
                                      10,
                                      300,
                                      20,
                                      hWnd,
                                      (HMENU) 500,
                                      hInst,
                                      NULL);
            OriginalEditProc = (WNDPROC) SetWindowLong(hEditBox,
                                                       GWL_WNDPROC,
                                                       (LONG) NewEditProc);
    
            SetFocus(hEditBox);
            break;
        case WM_DESTROY:
            SetWindowLong(hEditBox,
                          GWL_WNDPROC,
                          (LONG) OriginalEditProc);
            DestroyWindow(hEditBox);
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd, Message, wParam, lParam);
        }
        return 0;
    }
    
    LRESULT CALLBACK NewEditProc(HWND hWnd,UINT Message,WPARAM wParam,LPARAM lParam)
    {
        const int TAB = '\x09';
        const int CR = '\x0d';
        const int ESC = '\x1b';
        
        if (Message == WM_CHAR)
        {
            if (wParam == TAB)
            {
                CallWindowProc(OriginalEditProc,
                               hWnd,
                               Message,
                               (WPARAM) '<',
                               lParam);
                CallWindowProc(OriginalEditProc,
                               hWnd,
                               Message,
                               (WPARAM) 'T',
                               lParam);
                CallWindowProc(OriginalEditProc,
                               hWnd,
                               Message,
                               (WPARAM) 'A',
                               lParam);
                CallWindowProc(OriginalEditProc,
                               hWnd,
                               Message,
                               (WPARAM) 'B',
                               lParam);
                CallWindowProc(OriginalEditProc,
                               hWnd,
                               Message,
                               (WPARAM) '>',
                               lParam);
                return 0;
            }
            else if (wParam == CR)
            {
                CallWindowProc(OriginalEditProc,
                               hWnd,
                               Message,
                               (WPARAM) '<',
                               lParam);
                CallWindowProc(OriginalEditProc,
                               hWnd,
                               Message,
                               (WPARAM) 'C',
                               lParam);
                CallWindowProc(OriginalEditProc,
                               hWnd,
                               Message,
                               (WPARAM) 'R',
                               lParam);
                CallWindowProc(OriginalEditProc,
                               hWnd,
                               Message,
                               (WPARAM) '>',
                               lParam);
                return 0;
            }
            else if (wParam == ESC)
            {
                CallWindowProc(OriginalEditProc,
                               hWnd,
                               Message,
                               (WPARAM) '<',
                               lParam);
                CallWindowProc(OriginalEditProc,
                               hWnd,
                               Message,
                               (WPARAM) 'E',
                               lParam);
                CallWindowProc(OriginalEditProc,
                               hWnd,
                               Message,
                               (WPARAM) 'S',
                               lParam);
                CallWindowProc(OriginalEditProc,
                               hWnd,
                               Message,
                               (WPARAM) 'C',
                               lParam);
                CallWindowProc(OriginalEditProc,
                               hWnd,
                               Message,
                               (WPARAM) '>',
                               lParam);
                return 0;
            }
        }
    
        return CallWindowProc(OriginalEditProc,
                              hWnd,
                              Message,
                              wParam,
                              lParam);
    }
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    54
    Thanks for all the suggestions. What I did, to read the Edit Control Contents line-by-line, is the following. The Edit Control is to a chat program of mine, so the data streams in at a certain rate. Since my bot doesnt need any of the statements before the current one, I just delete the line, and stay on the top line.:

    Code:
     
     
    // hwnd = edit control window
    // len = length of top line
    
    len = SendMessage(hwnd, EM_LINELENGTH, 0, 0 );
    
    SendMessage(hwnd, WM_GETTEXT, len + 1, (LPARAM) &data);
     
    //delete the line
    SendMessage(hwnd, WM_SETTEXT, 0, (LPARAM)(LPCTSTR)text);
    
    //all chat stays on the top line, which is okay for the bot's sake
     data[len + 1] = '\0';

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Codec Bitrates?
    By gvector1 in forum C# Programming
    Replies: 2
    Last Post: 06-16-2003, 08:39 AM
  2. FlashWindowEx not declared?
    By Aidman in forum Windows Programming
    Replies: 3
    Last Post: 05-17-2003, 02:58 AM
  3. Child Windows and Messages
    By Terrell in forum Windows Programming
    Replies: 10
    Last Post: 09-05-2002, 06:39 AM
  4. How come this only works in Windows nt/2000?
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 08-30-2002, 06:54 PM
  5. Determining Active View :: MFC
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 04-25-2002, 07:34 PM