c# Windows API finding the object name and the data in the object

I am trying to recover the data from an application, i can see the form cycle through the form and see the object in question, can come back with the handle but cannot seem to retrieve the object name and the data housed in the object itself, have been using Managed Spy and Windows detector, for learning can recommend both for learning especially managed spy which has the source code for viewing how it is done in c#
except where i need to find the value or change the value of the object
TMistakeEdit is the name that i need to return and the data in side the object
as well as being able to send data to this object using c#
any help woudl be appreciated. its that close can do it with windows detector so i know that it can be done
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;


namespace Win32Application
{
    public partial class Form1 : Form
    {
           

        public Form1()
        {
            InitializeComponent();
           
         
        }
        
        int hWnd = 0;
        public delegate int Callback(int hWnd,int lParam);
        
        private void button1_Click(object sender, EventArgs e)
        {
                
            
              Callback myCallBack = new Callback(EnumChildGetValue);

              hWnd = Win32.FindWindow("TMainFrm", "Copernic Agent Personal");
                if(hWnd == 0)
                {
                        MessageBox.Show("Please Start Calling Window Application");
                }
                else
                {
                        Win32.EnumChildWindows(hWnd,myCallBack,0);
                }
           
        }

        public int EnumChildGetValue(int hWnd, int lParam)
        {
            StringBuilder formDetails = new StringBuilder(256);
            int txtValue;
            string editText = "";
            txtValue = Win32.GetWindowText(hWnd, formDetails, 256);
            if (hWnd == 132476)
            {
                MessageBox.Show("  ");
            }
            editText = formDetails.ToString().Trim();

       //     MessageBox.Show("Contains text of contro:" + editText + txtValue);
            return 1;
        }

         
    }
}

Open in new window

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace Win32Application
{
        /// <author>Shrijeet Nair</author>
        public class Win32
        {
                [DllImport("User32.dll")]
                public static extern Int32 FindWindow(String lpClassName,String lpWindowName);
                [DllImport("User32.dll")]
                public static extern Int32 SetForegroundWindow(int hWnd);
                [DllImport("User32.dll")]
                public static extern Boolean EnumChildWindows(int hWndParent,Delegate lpEnumFunc,int lParam);
                [DllImport("User32.dll")]
                public static extern Int32 GetWindowText(int hWnd,StringBuilder s,int nMaxCount);
                [DllImport("User32.dll")]
                public static extern Int32 GetWindowTextLength(int hwnd);
                [DllImport("user32.dll", EntryPoint="GetDesktopWindow")]
                public static extern int GetDesktopWindow();
        }
}

Open in new window

TMistakeEdit.png
sydneyguyAsked:
Who is Participating?
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Try something like this:

    string contents = MyGetWindowText(hWnd);
    MessageBox.Show(contents);

With:
public const int WM_GETTEXT = 0xD;
        public const int WM_GETTEXTLENGTH = 0x000E;

        [DllImport("user32.dll")]
        public static extern int SendMessage(int handle, int msg, int Param1, int Param2);

        [DllImport("user32.dll")]
        public static extern int SendMessage(int handle, int msg, int Param, System.Text.StringBuilder text);

        public static string MyGetWindowText(int handle)
        {
            StringBuilder buffer = new StringBuilder(SendMessage(handle, WM_GETTEXTLENGTH,0,0) + 1);
            SendMessage(handle, WM_GETTEXT, buffer.Capacity, buffer);
            return buffer.ToString();
        }

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
See GetWindowText():
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633520(v=vs.85).aspx

   "To retrieve the text of a control in another process, send a WM_GETTEXT message directly instead of calling GetWindowText."

So try using SendMessage() with WM_GETTEXT:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms632627(v=vs.85).aspx
0
 
sydneyguyAuthor Commented:
i knew that SendMessage()  is the one to use i just cannot find a good example to get all my pointers in the right direction to get it running corectly this is my problem at the moment any example that work
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
sydneyguyAuthor Commented:
it comes down to understanding how to pass the handle of the child that is need as a pointer to the function, this is what i am having trouble with as you will

            // this is the coding are here were i have the handle required but getting it converted to the
            // correct pointer is what i am lacking so any direction would be useful
public int EnumChildGetValue(int hWndval, int lParam)
        {
            StringBuilder formDetails = new StringBuilder(256);
            int txtValue;
            string editText = "";
            txtValue = Win32.GetWindowText(hWndval, formDetails, 256);
            if (hWndval == 132476)
            {
                StringBuilder sb = new StringBuilder(65535);
          //      Win32.GetWindowText("handle to window", sb, Win32.GetWindowTextLength("handle to window"));
               
             //   *********************************************
            // this is the coding are here were i have the handle required but getting it converted to the
            // correct pointer is what i am lacking so any direction would be uese full
       
                Win32.SendMessage(hWnd , WM_GETTEXT, sb.Length, sb);

            GetControlText( hWnd);

            }
            editText = formDetails.ToString().Trim();

       //     MessageBox.Show("Contains text of contro:" + editText + txtValue);
            return 1;
        }

Open in new window

0
 
sydneyguyAuthor Commented:
sorry missed our last message crossed over will try that now
0
 
sydneyguyAuthor Commented:
 public static string MyGetWindowText(int handle)
        {

            const int WM_GETTEXTLENGTH = 0x000D;
            StringBuilder buffer = new StringBuilder(Win32.SendMessage(handle, WM_GETTEXTLENGTH, 0, 0) + 1);
       //     Win32.SendMessage(handle, WM_GETTEXTLENGTH, buffer.Capacity, buffer);
            return buffer.ToString();
        }

throws up errors
also why call the  Win32.SendMessage(handle, WM_GETTEXTLENGTH, buffer.Capacity, buffer);
twice in the same function??

The best overloaded method match for 'Win32Application.Win32.SendMessage(System.IntPtr, uint, System.IntPtr, System.Text.StringBuilder)' has some invalid arguments      

is the error that i am trying to correct any ideas




0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
If you look closely...
the first call gets the length of the text with WM_GETTEXTLENGTH,
while the second actually grabs the text with WM_GETTEXT.

The error is because you need to copy my declarationS for SendMessage().  Yours is using IntPtr as the first param while mine are using int.
0
 
sydneyguyAuthor Commented:
thats fine understand that and have included my code have set up the handle
 using the int value handle which is hWndval the handle number but the systems still throws up and error looking for a IntPtr, not sure whats happening here?


Error      1      The best overloaded method match for 'Win32Application.Win32.SendMessage(System.IntPtr, uint, System.IntPtr, System.Text.StringBuilder)' has some invalid arguments      

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace Win32Application
{
        /// <author>Shrijeet Nair</author>
        public class Win32
        {

                        
            [DllImport("User32.dll")]
                public static extern Int32 FindWindow(String lpClassName,String lpWindowName);
                [DllImport("User32.dll")]
                public static extern Int32 SetForegroundWindow(int hWnd);
                [DllImport("User32.dll")]
                public static extern Boolean EnumChildWindows(int hWndParent,Delegate lpEnumFunc,int lParam);
                [DllImport("User32.dll")]
                public static extern Int32 GetWindowText(int hWnd,StringBuilder s,int nMaxCount);
                [DllImport("User32.dll")]
                public static extern Int32 GetWindowTextLength(int hwnd);
                [DllImport("user32.dll", EntryPoint="GetDesktopWindow")]
                public static extern int GetDesktopWindow();
              
                [DllImport("user32.dll", CharSet = CharSet.Auto)]
                public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, [Out] StringBuilder lParam);

                [DllImport("User32.DLL")]
                public static extern int SendMessage(IntPtr hWnd, UInt32 Msg, Int32 wParam, Int32 lParam);

                public const Int32 WM_SYSCOMMAND = 0x112;
                public const Int32 SC_SCREENSAVE = 0xF140;
        
        }
}

Open in new window

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;


namespace Win32Application
{
    public partial class Form1 : Form
    {
        public const int WM_GETTEXT = 0xD;
        public const int WM_GETTEXTLENGTH = 0x000E;

        public Form1()
        {
            InitializeComponent();
        
        }
        
        int hWndval = 0;
        public delegate int Callback(int hWndval,int lParam);
        
        private void button1_Click(object sender, EventArgs e)
        {

            // Start the screen saver.
           // Win32.SendMessage(this.Handle, Win32.WM_SYSCOMMAND, Win32.SC_SCREENSAVE, 0);
            
            Callback myCallBack = new Callback(EnumChildGetValue);
               hWndval = Win32.FindWindow("TMainFrm", "Copernic Agent Personal");
                if(hWndval == 0)
                {
                        MessageBox.Show("Please Start Calling Window Application");
                }
                else
                {
                        Win32.EnumChildWindows(hWndval,myCallBack,0);
                }
           
        }

        public int EnumChildGetValue(int hWndval, int lParam)
        {
            StringBuilder formDetails = new StringBuilder(256);
            int txtValue;
            string editText = "";
            txtValue = Win32.GetWindowText(hWndval, formDetails, 256);
            if (hWndval == 132476)
            {
                int handle = hWndval;
                StringBuilder buffer = new StringBuilder(Win32.SendMessage(handle, WM_GETTEXTLENGTH, 0, 0) + 1);
                Win32.SendMessage(handle, WM_GETTEXT, buffer.Capacity, buffer);
                return buffer.ToString();
              
      
            }
            editText = formDetails.ToString().Trim();

     
            return 1;
        }
               
    }
}

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You still didn't copy MY declarations...

Here they are:

        [DllImport("user32.dll")]
        public static extern int SendMessage(int handle, int msg, int Param1, int Param2);

        [DllImport("user32.dll")]
        public static extern int SendMessage(int handle, int msg, int Param, System.Text.StringBuilder text);

Notice that the first param is of type "int".   *Not IntPtr...
0
 
sydneyguyAuthor Commented:
sorry about that have been looking at code for hours and copied your code across a had a conflict and then deleted yours thinking it was the old code instead of the new code, but it does compile now and returns the value in this field,
but how do i send it a string of data to replace whats already there please
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Typically, instead of WM_GETTEXT, you use WM_SETTEXT and SendMessage():
http://msdn.microsoft.com/en-us/library/windows/desktop/ms632644(v=vs.85).aspx

    public const int WM_SETTEXT = 0xC;
0
 
sydneyguyAuthor Commented:
thanks for that got that section going appreciate the help lots of code to work through but at least i am moving foward again thanks for the help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.