如何用VB调用外部程序(非Shell)
我现在要用VB调用C盘下的“1.exe”文件,要等“1.exe”运行结束后然后再运行下一行语句,而非像Shell那样还没等到外部程序运行结束就继续执行下一行语句。希望各位...
我现在要用VB调用C盘下的“1.exe”文件,要等“1.exe”运行结束后然后再运行下一行语句,而非像Shell那样还没等到外部程序运行结束就继续执行下一行语句。
希望各位朋友帮帮忙,能结出这段完整的代码……
谢谢(^-^)
“yuki365”这位朋友,你回答的内容我运行过了,可以成功,但我看完后晕晕的,能不能再给我写个简单点的,最好是每行都有注解(^*^) 请你帮帮忙,谢啦……
如果写的好我会适当给你追加分的…… 展开
希望各位朋友帮帮忙,能结出这段完整的代码……
谢谢(^-^)
“yuki365”这位朋友,你回答的内容我运行过了,可以成功,但我看完后晕晕的,能不能再给我写个简单点的,最好是每行都有注解(^*^) 请你帮帮忙,谢啦……
如果写的好我会适当给你追加分的…… 展开
展开全部
'定义常量 以后见到 SYNCHRONIZE 就相当于填写了 &H100000
'即 pHnd = OpenProcess(SYNCHRONIZE, 0, pId) 等于 pHnd = OpenProcess(&H100000, 0, pId)
Const SYNCHRONIZE = &H100000
Const INFINITE = &HFFFFFFFF
'引用系统 API “OpenProcess” 用以获得指定程序进程句柄
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
'引用系统 API “CloseHandle” 关闭句柄
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
'引用系统 API “WaitForSingleObject” 用以等待指定句柄的进程执行
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
' 程序范例:(以执行 Notepad 程序为例)
Private Sub Command1_Click()
Dim pId As Long, pHnd As Long ' 分别声明 Process Id 及 Process Handle
pId = Shell("Notepad", vbNormalFocus) ' Shell 传回 Process Id 如 notepad 的 进程ID
pHnd = OpenProcess(SYNCHRONIZE, 0, pId) ' 取得 Process Handle 得到 notepad 的进程句柄
If pHnd <> 0 Then '句柄获得成功 则执行if内语句
Call WaitForSingleObject(pHnd, INFINITE) ' 无限等待,直到程序结束
Call CloseHandle(pHnd) '释放句柄
End If
MsgBox "执行结束"
End Sub
我也是菜鸟 只能解释成这样了
'即 pHnd = OpenProcess(SYNCHRONIZE, 0, pId) 等于 pHnd = OpenProcess(&H100000, 0, pId)
Const SYNCHRONIZE = &H100000
Const INFINITE = &HFFFFFFFF
'引用系统 API “OpenProcess” 用以获得指定程序进程句柄
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
'引用系统 API “CloseHandle” 关闭句柄
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
'引用系统 API “WaitForSingleObject” 用以等待指定句柄的进程执行
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
' 程序范例:(以执行 Notepad 程序为例)
Private Sub Command1_Click()
Dim pId As Long, pHnd As Long ' 分别声明 Process Id 及 Process Handle
pId = Shell("Notepad", vbNormalFocus) ' Shell 传回 Process Id 如 notepad 的 进程ID
pHnd = OpenProcess(SYNCHRONIZE, 0, pId) ' 取得 Process Handle 得到 notepad 的进程句柄
If pHnd <> 0 Then '句柄获得成功 则执行if内语句
Call WaitForSingleObject(pHnd, INFINITE) ' 无限等待,直到程序结束
Call CloseHandle(pHnd) '释放句柄
End If
MsgBox "执行结束"
End Sub
我也是菜鸟 只能解释成这样了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询