0

I have some application that i want to read it's datagridview, Is that possible ? (it is not my app so i don't have the source code of it). for example assume that i want to read the textbox data of windows caluculator app..

I heard about 'Autoit' software which deals with these kind of issues, is this possible programmatically in c#?

Niklas
  • 955
  • 15
  • 29
tomerJK
  • 39
  • 7
  • I doubt that's possible. – rory.ap Sep 16 '16 at 11:53
  • If the program you are trying to access has no sort of interface for you to access, then I see no way to get the information – Markai Sep 16 '16 at 11:56
  • ok maybe it's not available in c# but i will try it in 'autoit' – tomerJK Sep 16 '16 at 12:01
  • You can create a Win Forms project and add that exe file's reference in that project, from there you can try to access `froms` in that exe file and open them. In this way you might be able to access the `DataGridView` control on that particular form. – sallushan Sep 16 '16 at 12:09

1 Answers1

1

AutoIt is available with c#, libs can be found here :

https://www.autoitscript.com/site/autoit/downloads/

for example to get the value of the calc app you can do this :

 AutoItX.WinWait("[CLASS:CalcFrame]", "", 10);
 var win = AutoItX.WinGetHandle("[CLASS:CalcFrame]");
 string strReturnText = AutoItX.WinGetText(win).Trim();

If you don't want to use autoit you can use PInvoke.

you can get a handle of the calc window with Pinvoke like this :

 [DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
 public static extern IntPtr FindWindow(string lpClassName,
 string lpWindowName);

 IntPtr handle = FindWindow("CalcFrame", "Calculator");

etc...

To see a more complex example you can read my answer here to get tabs inside internet explorer.

https://stackoverflow.com/a/37595421/5703316

More info about Pinvoke :

http://pinvoke.net/

EDIT

After some thinking I think the best way for you is to use System.Windows.Automation;

I did a little example to show you how to use the Automation framework.

It's a console application that gets the grid headers of this little windowsForm app :

enter image description here

And the code :

    static void Main(string[] args)
    {
        int id = System.Diagnostics.Process.GetProcessesByName("WindowsFormsApplication1")[0].Id;
        AutomationElement desktop = AutomationElement.RootElement;
        AutomationElement bw = desktop.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ProcessIdProperty, id));

        AutomationElement datagrid = bw.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "dataGridView1"));

        AutomationElementCollection headers = datagrid.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Header));

        var headerCol1 = headers[1].Current.Name; ;
        var headerCol2 = headers[2].Current.Name;

        Console.WriteLine(headerCol1 + " " + headerCol2);

    }

You can find many Q/A on SO to manipulate DataGridView with UI Automation :

Getting full contents of a Datagrid using UIAutomation

UI Automation not working for DataGridView

Community
  • 1
  • 1
Quentin Roger
  • 6,410
  • 2
  • 23
  • 36