【发布时间】:2012-04-25 02:53:15
【问题描述】:
我想从与我的程序无关的窗口中“读取”信息。 如果我有进程 ID 和窗口句柄:
Process Proc = Process.GetProcessById(ProcID);
IntPtr hdl = Proc.MainWindowHandle;
我从 spy++ 得到的信息告诉我,我感兴趣的元素的 control-ID 是 00000003EA,我如何使用 C# 访问它?
感谢您的帮助!
编辑_____________________________________
如果有人感兴趣,这就是我的工作方式:
Process p = Process.GetProcessById(ProcID);
IntPtr hdl = p.MainWindowHandle;
byte[] buffer = new byte[1024]; //Assume that 1024 bytes are enough! Better would be to get the text length..
UTF8Encoding enc = new UTF8Encoding();
uint Test = GetDlgItemText((int)hdl, Convert.ToInt32("0x000003EA", 16), buffer, 1024);
string TextFromOtherWindow = enc.GetString(buffer);
[DllImport("user32.dll")]
public static extern uint GetDlgItemText(
int hDlg, //A handle to the dialog box that contains the control.
int nIDDlgItem, //The identifier of the control whose title or text is to be retrieved.
byte[] lpString, //The buffer to receive the title or text.
int nMaxCount //The maximum length, in characters, of the string to be copied to the
//buffer pointed to by lpString. If the length of the string, including
//the null character, exceeds the limit, the string is truncated.
);
byte[] buffer 是从另一个窗口写回文本的缓冲区。我假设文本长度不超过 1024 字节,但最好获得实际大小……
就编码而言,不同的编码可能更适合您的需求。
十六进制句柄需要转换为整数:Convert.ToInt32("0x000003EA", 16)
GetDlgItemText 最适合(我认为)我获取静态文本的要求,而不是“SendMessage”和“WM_GETTEXT” .
感谢所有帮助我指明正确方向的人!
GetDlgItemText 的来源:MSDN
编辑_________________________________
嗯。我说得太早了......每次启动程序时都会更改元素ID。我在Persistent Element Identification 提出了一个新问题。
【问题讨论】:
-
也许这会有所帮助:stackoverflow.com/questions/352236/…
-
请问是什么流程?它是 .net 进程吗?
-
该链接用于 C++,可以解决问题,但我不知道它是什么意思 :-( 我要包装的是 AutoCAD 的安装程序。
-
@Daro:问题是在“纯 C#”中无法做到这一点,您必须使用 pinvoke(参见 pinvoke.net)来调用一些 C++ 代码。
标签: c# interprocess