2010/11/23

office2010 Win32API WaitForSingleObject/OpenProcess/CloseHandle

Option Compare Database
Option Explicit

Const PROCESS_ALL_ACCESS = &H1F0FFF
Const SYNCHRONIZE = &H100000

Const INFINITE = &HFFFFFFFF      '  Infinite timeout

#If VBA7 Then
'http://msdn.microsoft.com/ja-jp/library/cc429427.aspx
Declare PtrSafe Function WaitForSingleObject Lib "kernel32" ( _
                                ByVal hHandle As LongPtr, _
                                ByVal dwMilliseconds As Long _
                                ) As Long

'http://msdn.microsoft.com/ja-jp/library/cc429278.aspx
Declare PtrSafe Function OpenProcess Lib "kernel32" ( _
                                ByVal dwDesiredAccess As Long, _
                                ByVal bInheritHandle As Long, _
                                ByVal dwProcessId As Long _
                                ) As LongPtr

'http://msdn.microsoft.com/ja-jp/library/cc429605.aspx
Declare PtrSafe Function CloseHandle Lib "kernel32" ( _
                                ByVal hObject As LongPtr _
                                ) As Long

#Else
Declare Function WaitForSingleObject Lib "kernel32" ( _
                                ByVal hHandle As Long, _
                                ByVal dwMilliseconds As Long _
                                ) As Long

Declare Function OpenProcess Lib "kernel32" ( _
                                ByVal dwDesiredAccess As Long, _
                                ByVal bInheritHandle As Long, _
                                ByVal dwProcessId As Long _
                                ) As Long

Declare Function CloseHandle Lib "kernel32" ( _
                                ByVal hObject As Long _
                                ) As Long
#End If

Sub test()
    Dim taskID
#If vba7 Then
    Dim pHwnd As LongPtr
#Else
    Dim pHwnd As Long
#End If
    taskID = Shell("calc.exe")
    pHwnd = OpenProcess(SYNCHRONIZE, False, taskID)
    WaitForSingleObject pHwnd, INFINITE
    CloseHandle pHwnd
    Debug.Print "Process End"
End Sub

0 件のコメント: