saf*_*oli 1 powershell powershell-2.0 powershell-3.0 powershell-4.0
如何在PowerShell中使用GetWindowText API函数?
我试过这样的方式:
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class UserWindowss {
[DllImport("user32.dll")]
public static extern IntPtr GetWindowText(IntPtr hWnd, System.Text.StringBuilder text, int count);
}
"@
Run Code Online (Sandbox Code Playgroud)
我做这样的字符串生成器:
$stringbuilder = New-Object System.Text.StringBuilder
$stringbuilder.Capacity =256
Run Code Online (Sandbox Code Playgroud)
我使用的功能如下:
$WindowTitless = $ImportDll::GetWindowText($TopWindow, $stringbuilder, 256)
Run Code Online (Sandbox Code Playgroud)
但是我得到了错误:
ERROR: test: Failed to get active Window details. More Info: Exception calling "GetWindowText" with "3" argument(s): "Unable to find an entry point named
ERROR: 'GetWindowText' in DLL 'user32.dll'."
Run Code Online (Sandbox Code Playgroud)
有人可以帮我使用这个功能吗?如果你能请代码.TNX.
大致这个:
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class UserWindows {
[DllImport("user32.dll")]
public static extern IntPtr GetWindowText(IntPtr hWnd, System.Text.StringBuilder text, int count);
}
"@
$stringbuilder = New-Object System.Text.StringBuilder 256
Get-Process | ForEach-Object {
$count = [UserWindows]::GetWindowText($_.MainWindowHandle, $stringbuilder, 256)
if (0 -lt $count) {
"$($_.ProcessName) $($stringbuilder.ToString())"
}
}
Run Code Online (Sandbox Code Playgroud)
您没有说明$ImportDll代码中的位置,但您需要将它们作为您定义的类的静态方法调用.
我的密切投票中的链接中有更完整的例子.
帮助链接(如果有):
Add-Type (在模块Microsoft.PowerShell.Utility中)New-Object (在模块Microsoft.PowerShell.Utility中)Get-Process (在模块Microsoft.PowerShell.Management中)ForEach-Object| 归档时间: |
|
| 查看次数: |
1230 次 |
| 最近记录: |