使用句柄从外部应用程序中提取数据

时间:2017-03-14 13:25:10

标签: c# handle hwnd

我需要使用句柄从外部应用程序中提取数据。我的问题是,如果应用程序关闭并再次打开,每次都会更改HWND,所以我想连接到它的windowClass名称,一切都很好,直到找到那个;那个特定的外部应用程序有我需要读取数据的标签在这种情况下具有相同的windowClass名称WindowsForms10.STATIC.app.0.378734a。我需要找到一种方法从外部应用程序标签中提取正确的数据方式,我不知道该怎么做。

这就是我的尝试:

通过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;

        }

    }
}

通过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;
        }


    }
}

这是我使用Windows Detective从2个不同标签中检索到的数据。

的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>

我该如何处理这个问题?我怎样才能以正确的方式提取必要的数据?

P.S。我可以找到的唯一区别是尺寸不同,但即使你打开和关闭应用程序它们保持不变,但我不知道如何使用句柄挂钩它们。

0 个答案:

没有答案