I need to extract data from an external application using handles.My problem is that if the application is closed and opened again the HWND is changed every time,so I was thinking of hooking to its windowClass name,all good until I found out that;that specific external app has the labels that I need to read the data from have the same windowClass name in this case WindowsForms10.STATIC.app.0.378734a
.I need to find a way to extract the data from the external apps labels in a correct way and I am not sure how to do this.
This is what I tried:
To retrieve the data via HWND:
public partial class Form1 : Form
{
private const int WM_GETTEXT = 13;
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string lclassName, string windowTitle);
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
internal delegate int WindowEnumProc(IntPtr hwnd, IntPtr lparam);
[DllImport("user32.dll")]
internal static extern bool EnumChildWindows(IntPtr hwnd, WindowEnumProc func, IntPtr lParam);
public delegate bool Win32Callback(IntPtr hwnd, IntPtr lParam);
public Form1()
{
InitializeComponent();
}
private async void button1_Click_1(object sender, EventArgs e)
{
for (;;)
{
await Task.Delay(1000);
DateTime date = DateTime.Now;
label3.Text = date.ToString();
IntPtr firstHandle = new IntPtr(0x00020232);
IntPtr Hwnd = FindWindow(null, "Form1");
IntPtr Handle = Marshal.AllocHGlobal(100);
int NumText = (int)SendMessage(firstHandle, WM_GETTEXT, (IntPtr)50, Handle);
string Text = Marshal.PtrToStringUni(Handle);
label1.Text = Text;
}
}
}
To retrieve data via windowClass Name
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, StringBuilder lParam);
[DllImport("user32.dll")]
public static extern IntPtr FindWindowEx(IntPtr Parent, IntPtr child, string classname, string WindowTitle);
const int WM_GETTEXT = 0x00D;
const int WM_GETTEXTLENGTH = 0x00E;
private void Form1_Load_1(object sender, EventArgs e)
{
for (;;)
{
Process procc = GetProcByName("Form1");
if (procc != null)
{
IntPtr child = FindWindowEx(procc.MainWindowHandle, IntPtr.Zero, "WindowsForms10.STATIC.app.0.378734a", null);
int length = SendMessage(child, WM_GETTEXTLENGTH, 0, 0);
StringBuilder text = new StringBuilder();
text = new StringBuilder(length + 1);
int retr2 = SendMessage(child, WM_GETTEXT, length + 1, text);
label1.Text = text.ToString();
}
}
}
public Process GetProcByName(string Name)
{
Process proc = null;
Process[] processes = Process.GetProcesses();
for (int i = 0; i < processes.Length; i++)
{ if (processes[i].ProcessName == Name) proc = processes[i]; }
return proc;
}
}
}
And this is the data that I retrieved from 2 different labels using Windows Detective.
Label1:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Properties for window WindowsForms10.STATIC.app.0.378734a (0x002E04AA)
Created by Window Detective
-->
<window>
<windowClass>
<name>WindowsForms10.STATIC.app.0.378734a</name>
<classExtraBytes>0</classExtraBytes>
<windowExtraBytes>4</windowExtraBytes>
<brush>none</brush>
</windowClass>
<handle>0x002E04AA</handle>
<parentHandle>0x003404EE</parentHandle>
<windowText>Front Pressure</windowText>
<dimensions>
<rect x="146" y="177" width="198" height="27"/>
</dimensions>
<clientDimensions>
<rect x="146" y="177" width="198" height="27"/>
</clientDimensions>
<styleBits>1442840589</styleBits>
<extendedStyleBits>0</extendedStyleBits>
<font>none</font>
<windowPropsList/>
<ownerProcessId>2960</ownerProcessId>
<ownerThreadId>2964</ownerThreadId>
</window>
Label2:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Properties for window WindowsForms10.STATIC.app.0.378734a (0x00020232)
Created by Window Detective
-->
<window>
<windowClass>
<name>WindowsForms10.STATIC.app.0.378734a</name>
<classExtraBytes>0</classExtraBytes>
<windowExtraBytes>4</windowExtraBytes>
<brush>none</brush>
</windowClass>
<handle>0x00020232</handle>
<parentHandle>0x0002022E</parentHandle>
<windowText>12.0 kg</windowText>
<dimensions>
<rect x="146" y="205" width="198" height="24"/>
</dimensions>
<clientDimensions>
<rect x="146" y="205" width="198" height="24"/>
</clientDimensions>
<styleBits>1442840717</styleBits>
<extendedStyleBits>0</extendedStyleBits>
<font>none</font>
<windowPropsList/>
<ownerProcessId>2960</ownerProcessId>
<ownerThreadId>2964</ownerThreadId>
</window>
How should I approach this problem?How could I extract the necessary data in a correct way?
P.S. The only difference that I could find to hook on are the dimensions that are different but even if you open and close the app they remain the same,but I don't know how could I hook to them using handles.