|
$source = @"
Imports System.Runtime.InteropServices
Imports System.Text
Public Class API_EnumWindows
' ******************************************************
' Callback 関数の定義( 通常 )
' ******************************************************
Public Delegate Function EnumWindowsCallbackNormal( _
ByVal hWnd As Integer, _
ByVal lParam As Integer) As Boolean
' ******************************************************
' Callback 呼び出し 関数の定義
' ******************************************************
<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Public Shared Function EnumWindows( _
ByVal callback As EnumWindowsCallbackNormal, _
ByVal lParam As Integer) As Integer
End Function
' ******************************************************
' Callback 関数の定義( 文字列引渡し用 )
' ******************************************************
Public Delegate Function EnumWindowsCallbackString( _
ByVal hWnd As Integer, _
ByVal lParam As String) As Boolean
' ******************************************************
' Callback 呼び出し 関数の定義
' ※ 要 Ansi 文字変換 => CharSet:=CharSet.Ansi
' ******************************************************
<DllImport("user32.dll", EntryPoint:="EnumWindows", CharSet:=CharSet.Ansi)> _
Public Shared Function EnumWindowsString( _
ByVal callback As EnumWindowsCallbackString, _
ByVal lParam As String) As Integer
End Function
' ******************************************************
' 情報取得用 API
' ******************************************************
<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Public Shared Function GetWindowText(ByVal hWnd As Integer, _
ByVal lpString As StringBuilder, _
ByVal nMaxCount As Integer) As Integer
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Public Shared Function GetClassName(ByVal hwnd As Integer, _
ByVal lpClassName As StringBuilder, _
ByVal cch As Integer) As Integer
End Function
Const STRING_BUFFER_LENGTH As Integer = 255
' ******************************************************
' Callback 関数( 文字列用 )
' ******************************************************
Private Function ActiveProcessListString(ByVal hWnd As Integer, _
ByVal lParam As String) As Boolean
Dim windowText As New StringBuilder(STRING_BUFFER_LENGTH)
Dim className As New StringBuilder(STRING_BUFFER_LENGTH)
GetWindowText(hWnd, windowText, STRING_BUFFER_LENGTH)
GetClassName(hWnd, className, STRING_BUFFER_LENGTH)
' lParam は、String
' System.Console.WriteLine(lParam)
System.Console.WriteLine( _
String.Format("{0}|{1}|{2}", _
windowText.ToString(), _
className.ToString(), _
hWnd.ToString()) _
)
Return True
End Function
' ******************************************************
' Callback 関数( 通常 )
' ******************************************************
Private Function ActiveProcessListNormal(ByVal hWnd As Integer, _
ByVal lParam As Integer) As Boolean
Dim windowText As New StringBuilder(STRING_BUFFER_LENGTH)
Dim className As New StringBuilder(STRING_BUFFER_LENGTH)
GetWindowText(hWnd, windowText, STRING_BUFFER_LENGTH)
GetClassName(hWnd, className, STRING_BUFFER_LENGTH)
' lParam は、 Integer
' System.Console.WriteLine(lParam)
System.Console.WriteLine( _
String.Format("{0}|{1}|{2}", _
windowText.ToString(), _
className.ToString(), _
hWnd.ToString()) _
)
Return True
End Function
' ******************************************************
' 処理開始( 文字列を渡す )
' ******************************************************
Public Sub Call1()
' Callback 関数のアドレスの渡し方
EnumWindowsString( _
New EnumWindowsCallbackString(AddressOf ActiveProcessListString), _
"文字列を渡す")
End Sub
' ******************************************************
' 処理開始( 通常 )
' ******************************************************
Public Sub Call2()
' Callback 関数のアドレスの渡し方
Dim func As EnumWindowsCallbackNormal = _
AddressOf ActiveProcessListNormal
EnumWindows(func, 0)
End Sub
End Class
"@
Add-Type -TypeDefinition $source -Language VisualBasic
$obj = New-Object API_EnumWindows
$ret = $obj.Call1()
Read-Host
| |