Thread: Using WinAPI to return textbox value in other app which sometimes work and not

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    385

    Using WinAPI to return textbox value in other app which sometimes work and not

    Hello,

    I am trying to use the WinAPI to return a String that resides in a textbox in an another open application in windows.

    I use the below code which works if the string in the textbox is this one:
    "aaaaaaaaaaaaaaaaaaaaaaaaaa"

    But if I change the string to this one instead, the application stops working and needs to quit.
    "aaaaaaaaaaaaaaaaaaaaaaaaaaBBBBBBBBBBBBBBBBB"

    I wonder why that longer string is not possible to retreive and also makes my application quit?

    I have used Spy++ to find the handle of the textbox which is: "0x00020584"
    PHP Code:
            [DllImport("user32.dll")]
            static 
    extern IntPtr SendMessage(IntPtr hWnduint Msgint wParam, [MarshalAs(UnmanagedType.LPStr)] StringBuilder lParam); 
    PHP Code:
                IntPtr textboxvalue = (IntPtr)0x00020584;
                const 
    int WM_GETTEXT 0x0D;
                
    IntPtr childHandle textboxvalue;
                
    StringBuilder sb = new StringBuilder();
                
    IntPtr retVal SendMessage(childHandleWM_GETTEXT300sb);
                
    String thevalue sb.ToString();
                
    MessageBox.Show(thevalue); 
    Last edited by Coding; 12-28-2015 at 03:26 PM.
    JackpotCity - Over 500 casinos, 2800 free games.
    Free Roulette Software - Free Roulette Software & complete betting tool for roulette. Simulate any systems.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    I found a solution to first retrieve the title.Capacity with the code below and this seem to work.

    Code:
            [DllImport("user32.dll")]
            static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, [MarshalAs(UnmanagedType.LPStr)] StringBuilder lParam);
            [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = System.Runtime.InteropServices.CharSet.Auto)] //
            public static extern IntPtr SendMessage2(int hWnd, int Msg, int wparam, int lparam);
    Code:
                IntPtr textboxvalue = (IntPtr)0x00020584;
                const int WM_GETTEXT = 0x000D;
                const int WM_GETTEXTLENGTH = 0x000E;
    
                IntPtr childHandle = textboxvalue;
                StringBuilder title = new StringBuilder();
    
                // Get the size of the string required to hold the window title. 
                Int32 size = SendMessage2((int)childHandle, WM_GETTEXTLENGTH, 0, 0).ToInt32();
    
                if (size > 0)
                {
                    title = new StringBuilder(size + 1);
                    SendMessage(childHandle, (int)WM_GETTEXT, title.Capacity, title);
                    MessageBox.Show(title.ToString());
                }
    JackpotCity - Over 500 casinos, 2800 free games.
    Free Roulette Software - Free Roulette Software & complete betting tool for roulette. Simulate any systems.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Location
    Trinidad, CO (log cabin in middle of nowhere)
    Posts
    148

    Demo Apps

    I'm thinking the best way to do it is use FindWindow() to obtain the HWND of the other application, then use GetDlgItem() to obtain the HWND of the text box (edit control) whose parent is the HWND of the other application. Of course, for this to work, one needs the Control ID of the text box in the other application, because that is the 2nd parameter of GetDlgItem(). Not having that, all I could think to do would be to use one of the EnumWindow() functions to enumerate all the HWNDs of the other app.

    Anyway, here are two Win32 Api SDK style apps that demonstrate this technique. The first is the app from which we wish to extract the text. Its name and Window Class is "ExtractTextSource". It has two buttons and two text boxes on it. When you click the "Love" button it writes "The World Needs More Love!" to the text box underneath the button. When you click the "Hate" button, the text "The World Needs More Hate!" is copied to the text box underneath it.

    The second app's Window Class is "ExtractTextTarget". Its exactly like the first app, but if the first app is running and you click the top "Love" button, it extracts the text from the 1st app and writes it to the text box. Ditto for the Hate button. Naturally, you need to start the 1st app first and click the buttons to put the text in the text boxes. Then start the 2nd app and it should retrieve the text.

    Tested with x86 on GCC 4.4.1 and MS VC 9 (ver. 15).

    Code:
    //Main.cpp - ExtractTextSource
    //g++ Main.cpp -mwindows -o ExtractTextSource -s -Os
    //cl Main.cpp Kernel32.lib User32.lib /O1 /Os /MT /FeExtractTextSource.exe
    //GCC 4.4.1 compiles/links to 8,192 bytes
    //VC9 (Version 15) 38400 bytes memory, 40,960 On Disk
    #include <windows.h>
    #define BTN_LOVE  1500
    #define BTN_HATE  1505
    #define TXT_LOVE  1510
    #define TXT_HATE  1515
    
    
    LRESULT CALLBACK fnWndProc(HWND hWnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
    {
     switch(msg)
     {
       case WM_CREATE:
         {
            HINSTANCE hIns=((LPCREATESTRUCT)lParam)->hInstance;
            CreateWindowEx(0,"button","Love",WS_CHILD|WS_VISIBLE,125,15,80,30,hWnd,(HMENU)BTN_LOVE,hIns,0);
            CreateWindowEx(WS_EX_CLIENTEDGE,"edit","",WS_CHILD|WS_VISIBLE,25,60,285,22,hWnd,(HMENU)TXT_LOVE,hIns,0);
            CreateWindowEx(0,"button","Hate",WS_CHILD|WS_VISIBLE,125,105,80,30,hWnd,(HMENU)BTN_HATE,hIns,0);
            CreateWindowEx(WS_EX_CLIENTEDGE,"edit","",WS_CHILD|WS_VISIBLE,25,150,285,22,hWnd,(HMENU)TXT_HATE,hIns,0);
            return 0;
         }
       case WM_COMMAND:
         {
            switch(LOWORD(wParam))
            {
              case BTN_LOVE:
                SetWindowText(GetDlgItem(hWnd,TXT_LOVE),"The World Needs More Love!");
                break;
              case BTN_HATE:
                SetWindowText(GetDlgItem(hWnd,TXT_HATE),"The World Needs More Hate!");
                break;
            }
            return 0;
         }
       case WM_DESTROY:
         {
            PostQuitMessage(0);
            return 0;
         }
     }
    
     return (DefWindowProc(hWnd, msg, wParam, lParam));
    }
    
    
    int WINAPI WinMain(HINSTANCE hIns, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
    {
     char szClassName[]="ExtractTextSource";
     WNDCLASSEX wc;
     MSG messages;
     HWND hWnd;
    
     memset(&wc,0,sizeof(WNDCLASSEX));
     wc.lpszClassName = szClassName,                wc.lpfnWndProc = fnWndProc;
     wc.cbSize        = sizeof (WNDCLASSEX),        wc.hIcon       = LoadIcon(NULL,IDI_APPLICATION);
     wc.hInstance     = hIns,                       wc.hCursor     = LoadCursor(NULL,IDC_ARROW);
     wc.hbrBackground = (HBRUSH)COLOR_BTNSHADOW;
     RegisterClassEx(&wc);
     hWnd=CreateWindowEx(0,szClassName,szClassName,WS_OVERLAPPEDWINDOW,150,150,350,250,HWND_DESKTOP,0,hIns,0);
     ShowWindow(hWnd,iShow);
     while(GetMessage(&messages,NULL,0,0))
     {
        TranslateMessage(&messages);
        DispatchMessage(&messages);
     }
    
     return messages.wParam;
    }
    Code for "ExtractTextTarget"

    Code:
    //Main.cpp - "ExtractTextTarget"
    //g++ Main.cpp -mwindows -o ExtractTextTarget -s -Os
    //cl Main.cpp Kernel32.lib User32.lib /O1 /Os /MT /FeExtractTextTarget.exe
    //compiles/links to 7680, 8192 bytes on disk GCC 4.4.1; x86
    //compiles/links VC 15 38,400 bytes memory, 40,960 On Disk; x86  
    #include <windows.h>
    #define BTN_LOVE  1500
    #define BTN_HATE  1505
    #define TXT_LOVE  1510
    #define TXT_HATE  1515
    
    
    LRESULT CALLBACK fnWndProc(HWND hWnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
    {
     char szBuffer[64];
     HWND hSource  = NULL;
     HWND hTxt     = NULL;
    
     switch(msg)
     {
       case WM_CREATE:
         {
            HINSTANCE hIns=((LPCREATESTRUCT)lParam)->hInstance;
            CreateWindowEx(0,"button","Love",WS_CHILD|WS_VISIBLE,125,15,80,30,hWnd,(HMENU)BTN_LOVE,hIns,0);
            CreateWindowEx(WS_EX_CLIENTEDGE,"edit","",WS_CHILD|WS_VISIBLE,25,60,285,22,hWnd,(HMENU)TXT_LOVE,hIns,0);
            CreateWindowEx(0,"button","Hate",WS_CHILD|WS_VISIBLE,125,105,80,30,hWnd,(HMENU)BTN_HATE,hIns,0);
            CreateWindowEx(WS_EX_CLIENTEDGE,"edit","",WS_CHILD|WS_VISIBLE,25,150,285,22,hWnd,(HMENU)TXT_HATE,hIns,0);
            return 0;
         }
       case WM_COMMAND:
         {
            switch(LOWORD(wParam))
            {
              case BTN_LOVE:
                {
                   hSource = FindWindow("ExtractTextSource","ExtractTextSource");
                   if(hSource)
                   {
                      hTxt = GetDlgItem(hSource,TXT_LOVE);
                      if(hTxt)
                      {
                         SendMessage(hTxt,WM_GETTEXT,32,(LPARAM)szBuffer);
                         SetWindowText(GetDlgItem(hWnd,TXT_LOVE),szBuffer);
                      }
                   }
                }
                break;
              case BTN_HATE:
                {
                   hSource = FindWindow("ExtractTextSource","ExtractTextSource");
                   if(hSource)
                   {
                      hTxt = GetDlgItem(hSource,TXT_HATE);
                      if(hTxt)
                      {
                         SendMessage(hTxt,WM_GETTEXT,32,(LPARAM)szBuffer);
                         SetWindowText(GetDlgItem(hWnd,TXT_HATE),szBuffer);
                      }
                   }
                }
                break;
            }
            return 0;
         }
       case WM_DESTROY:
         {
            PostQuitMessage(0);
            return 0;
         }
     }
    
     return (DefWindowProc(hWnd, msg, wParam, lParam));
    }
    
    
    int WINAPI WinMain(HINSTANCE hIns, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
    {
     char szClassName[]="ExtractTextTarget";
     WNDCLASSEX wc;
     MSG messages;
     HWND hWnd;
    
     memset(&wc,0,sizeof(WNDCLASSEX));
     wc.lpszClassName = szClassName,                wc.lpfnWndProc = fnWndProc;
     wc.cbSize        = sizeof (WNDCLASSEX),        wc.hIcon       = LoadIcon(NULL,IDI_APPLICATION);
     wc.hInstance     = hIns,                       wc.hCursor     = LoadCursor(NULL,IDC_ARROW);
     wc.hbrBackground = (HBRUSH)COLOR_BTNSHADOW;
     RegisterClassEx(&wc);
     hWnd=CreateWindowEx(0,szClassName,szClassName,WS_OVERLAPPEDWINDOW,150,150,350,250,HWND_DESKTOP,0,hIns,0);
     ShowWindow(hWnd,iShow);
     while(GetMessage(&messages,NULL,0,0))
     {
        TranslateMessage(&messages);
        DispatchMessage(&messages);
     }
    
     return messages.wParam;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple return and it does not work
    By kotte in forum C++ Programming
    Replies: 4
    Last Post: 01-27-2006, 12:22 AM
  2. Textbox
    By maxorator in forum Windows Programming
    Replies: 20
    Last Post: 09-25-2005, 10:04 AM
  3. Textbox
    By Salibea in forum C++ Programming
    Replies: 6
    Last Post: 07-27-2005, 09:55 AM
  4. "return function" doesn't work
    By Cris987 in forum C++ Programming
    Replies: 10
    Last Post: 03-04-2004, 11:04 PM
  5. Get Int with TextBox
    By Squintz in forum C# Programming
    Replies: 1
    Last Post: 01-27-2003, 09:02 AM