标题:在Access 95-2002中,如何使代码的执行时中断,并启动一个外部程序直至完成后,恢复中断。
作者: Attac顾问组织
以下是如何从access启动另一个外部程序,使原来执行代码中断,直至这个外部程序完成后,恢复中断。完成这个功能我们可以"WaitforSingleObject"和"OpenProcess" API函数来启动这个外部程序直至完成。请参考下面列出代码。
1.在您模块的声明页中,加上以下声明和过程:
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal _
dwAccess As Long, ByVal fInherit As Integer, ByVal hObject _
As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal _
hObject As Long) As Long
2.请试验下面这个测试过程,它可是实现任何您想启动的一个外部应用程序直至完成,才弹出一个提示框。(“_”表示续行。)
Function LaunchApp32 (MYAppname As String) As Integer
On Error Resume Next
Const SYNCHRONIZE = 1048576
Const INFINITE = -1&
Dim ProcessID&
Dim ProcessHandle&
Dim Ret&
LaunchApp32=-1
ProcessID = Shell(MyAppName, vbNormalFocus)
If ProcessID<>0 then
ProcessHandle = OpenProcess(SYNCHRONIZE, True, ProcessID&)
Ret = WaitForSingleObject(ProcessHandle, INFINITE)
Ret = CloseHandle(ProcessHandle)
MsgBox "This code waited to execute until " _
& MyAppName & " Finished",64
Else
MsgBox "ERROR : Unable to start " & MyAppname
LaunchApp32=0
End If
End Function
值得注意的一点是,当这个外部应用程序完成后,在你的过程中必需包含一段代码用以关闭这个进程的句柄。否则内存将会溢出(即:内存死区)直到你关闭窗口为止。[em01][em01][em01]
[此贴子已经被作者于2005-8-18 15:42:12编辑过]
|