For example, can I get the edit (character input) part of Notepad or the string displayed on a specific form of any application from Powershell?
Environment:
Powershell 5.1
Windows 10
*Spy++ is not available.
何 You cannot download anything, only the features that come with the Windows standard are available.
I can do it.
However, the sample code below requires you to know the class name of any application and the control name of a specific form in advance.
sample code
This is an example of reading a string from an edit in the starting Notepad.
If you want to read specific controls for any app, rewrite the "notepad"
and "Edit"
at the end of the code to the appropriate values.
$Win32=&{
Add-Type-Assembly "System.Runtime"
# ========= Win32 Api function definition in C# language (from here) =========
$cscode=@"
DllImport("user32.dll")]
static external IntPtr FindWindow (string lpClassName, string lpWindowName);
DllImport("user32.dll", SetLastError=true)]
public static external IntPtr FindWindowEx (IntPtr hwndParent, IntPtr hwndChildafter, string lpszClass, string lpszWindow);
DllImport("user32.dll", CharSet=CharSet.Unicode)
public static external int SendMessage (IntPtr hwnd, int msg, IntPtr wparam, IntPtr param);
DllImport("user32.dll", CharSet=CharSet.Unicode)
public static external int SendMessage (IntPtr hwnd, int msg, IntPtr wparam, System.Text.StringBuilder lparam);
public static IntPtr FindWindowByClass(string lpClassName){
return FindWindow (lpClassName, null);
}
public static string GetText(IntPtr hwndParent, string lpszClass) {
int WM_GETTEXT = 0x000D;
intWM_GETTEXTLENGTH=0x000E;
var hwnd=FindWindowEx(hwndParent, IntPtr.Zero, lpszClass, ''); // Control Handle
Console.WriteLine(hwnd);
var length = SendMessage (hwnd, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero); // Number of characters in the control
Console.WriteLine(length);
varsb = new System.Text.StringBuilder(length); /* Ensure buffer size for length*/
SendMessage(hwnd, WM_GETTEXT, new IntPtr(length+1), sb);
return sb.ToString();
}
"@
# ========= Win32 Api function definition in C# language (so far) =========
return(add-type-memberDefinition$cscode-name "Win32ApiFunctions"-pasthru)
}
# Get Notepad Handle from Class Name
$notepad=$Win32::FindWindowByClass("notepad")
# Get Edit Control String
$s=$Win32::GetText($notepad, "Edit")
# display
$s
References
© 2024 OneMinuteCode. All rights reserved.