PowerShellでWin32APIを使う。
よく分かりませんが、こんな感じで使えるようです。
param([String] $title)
## Invoke a Win32 P/Invoke call.
function Invoke-Win32([string] $dllName, [Type] $returnType, [string] $methodName,
[Type[]] $parameterTypes, [Object[]] $parameters)
{
## Begin to build the dynamic assembly
$domain = [AppDomain]::CurrentDomain
$name = New-Object Reflection.AssemblyName 'PInvokeAssembly'
$assembly = $domain.DefineDynamicAssembly($name, 'Run')
$module = $assembly.DefineDynamicModule('PInvokeModule')
$type = $module.DefineType('PInvokeType', "Public,BeforeFieldInit")
## Define the actual P/Invoke method
$method = $type.DefineMethod($methodName, 'Public,HideBySig,Static,PinvokeImpl',
$returnType, $parameterTypes)
## Apply the P/Invoke constructor
$ctor = [Runtime.InteropServices.DllImportAttribute].GetConstructor([string])
$attr = New-Object Reflection.Emit.CustomAttributeBuilder $ctor, $dllName
$method.SetCustomAttribute($attr)
## Create the temporary type, and invoke the method.
$realType = $type.CreateType()
$realType.InvokeMember($methodName, 'Public,Static,InvokeMethod', $null, $null,
$parameters)
}
function FindWindow([String] $WindowName)
{
$parameterTypes = [Int32], [String]
$parameters = 0, $WindowName
Invoke-Win32 "user32.dll" ([Int32]) "FindWindow" $parameterTypes $parameters
}
function GetWindowThreadProcessId([Int32] $hWnd, [ref] $ProcessId)
{
$parameterTypes = [Int32], [System.Type]::GetType("System.Int32").MakeByRefType()
$parameters = $hWnd, $ProcessId.Value
Invoke-Win32 "user32.dll" ([Int32]) "GetWindowThreadProcessId" $parameterTypes $parameters
$ProcessId.Value=$parameters[1]
}
function GetWindowText([Int32] $hWnd, [Text.StringBuilder] $lpString, [Int32] $nMaxCount)
{
$parameterTypes = [Int32], [Text.StringBuilder], [Int32]
$parameters = $hWnd, $lpString, $nMaxCount
Invoke-Win32 "user32.dll" ([Int32]) "GetWindowTextA" $parameterTypes $parameters
}
$hwnd=FindWindow $title
$ProcessId=0
GetWindowThreadProcessId $hWnd ([ref] $ProcessId)
$ProcessId
$lpString=new-object System.Text.StringBuilder(128)
GetWindowText $hWnd $lpString $lpString.Capacity
$lpString.Length
$lpString.ToString()
Invoke-Win32のオリジナルソースは以下を参照。
From: Lee Holmes
http://www.leeholmes.com/blog/GetTheOwnerOfAProcessInPowerShellPInvokeAndRefOutParameters.aspx
PowerShellでWin32APIが使えるなら、Invoke-Win32をJScript.NETに焼き直して、JScript.NETでも同様にWin32APIが使えますね。
« PowerShell.exeでAppActivateする1行バッチファイル | トップページ | JScript.NETでWin32APIを使う。 »