关于如何控制vb程序,与外部程序协调同步的问题

goodname 2003-02-25 10:24:13
我在vb的程序的一个处理段中
用shell函数调用了外部的arj.exe
对一个压缩包进行解压

程序中后面的段,要用到解压后的新文件。
例如:
'arj解压
Dim FileString As String
Dim Result As Long
FileString = GetAPPPath() & "arj.exe e -y " & usr_tempfile & " " & usr_tempdir
Result = Shell(FileString, vbHide)
'If Result = 0 Then '解压失败
' 'end
'End If

'....
'下面的这里的程序要用到解压后的文件
'....



可是,这个shell函数似乎是异步调用的,往往文件还没有解出来,程序已经执行到了下面了
结果得不到解出来的文件,从而导致程序失败。


我想问一下有没有方法解决这个问题?谢谢!



p.s.我目前采用的方法是在中间用
sleep(1000)

睡眠了1秒钟


可是,如果要解压的压缩包如果很大,势必还要延长睡眠时间,可是太长,影响程序速度




请各位大虾出手!



...全文
29 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
lxcc 2003-02-27
  • 打赏
  • 举报
回复

x=shellexecute(...)
'看返回值状态,然后再执行下一步
if x=... then
'do next
else
'msgbox "ERROR"
end if
goodname 2003-02-27
  • 打赏
  • 举报
回复
下午测试一下,如果ok,立刻结贴
分给地少了点,请多包涵。
Weiguo 2003-02-26
  • 打赏
  • 举报
回复
调用API函数,先获得原有进程,然后中断进程,再进行解压,解压完成后再继续当前进程。
好象程序员大本营里有源代码。
soec 2003-02-26
  • 打赏
  • 举报
回复
'''
soec 2003-02-26
  • 打赏
  • 举报
回复
我系来运吉噶,吾使理我.
用户 昵称 2003-02-26
  • 打赏
  • 举报
回复
Option Explicit

Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type

Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type

Private Declare Function dcWaitForSingleObject Lib "kernel32" Alias "WaitForSingleObject" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long

Private Declare Function dcCreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal _
lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _
lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
lpStartupInfo As STARTUPINFO, lpProcessInformation As _
PROCESS_INFORMATION) As Long

Private Declare Function dcCloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject As Long) As Long

Private Declare Function dcGetExitCodeProcess Lib "kernel32" Alias "GetExitCodeProcess" (ByVal hProcess As Long, lpExitCode As Long) As Long

Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&


Public Function ExecCmd(cmdline$)
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim ret As Long


On Error GoTo errExit

' Initialize the STARTUPINFO structure:
start.cb = Len(start)

' Start the shelled application:
ret = dcCreateProcess(0&, cmdline$, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)

' Wait for the shelled application to finish:
ret = dcWaitForSingleObject(proc.hProcess, 900000)
If ret = WAIT_TIMEOUT Then
'After 15 min program may be hung?
Call dcTerminateProcess(proc.hProcess, enAllFail)
End If
Call dcGetExitCodeProcess(proc.hProcess, ret&)
Call dcCloseHandle(proc.hProcess)
ExecCmd = ret&
Exit Function
errExit:
'Error handler here

End Function
goodname 2003-02-26
  • 打赏
  • 举报
回复
呵呵,还是有点问题。

这个arj是解压一个bmp的压缩包,解出若干个bmp来

下面还有一个bmp2jpg.exe的外部程序负责转化为jpg格式
所以得有个循环,将所有的bmp都化为对应的jpg文件

例如:

for i=1 to total

'....

hProc = OpenProcess(PROCESS_QUERY_INFORMATION, False, Result)
Do
GetExitCodeProcess hProc, lExit
DoEvents
Loop While lExit <> STILL_ACTIVE

'....

next


似乎这段检测有时候会不起作用?

youchenglong 2003-02-26
  • 打赏
  • 举报
回复
掉了几个变量的说明,自己加上
youchenglong 2003-02-26
  • 打赏
  • 举报
回复
模块:
Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredaccess&, ByVal bInherithandle&, ByVal dwProcessid&) As Long

Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpexitcode As Long) As Long

Const STILL_ACTIVE = &H103
Const PROCESS_QUERY_INFORMATION = &H400
程序:
Dim FileString As String
Dim Result As Long
FileString = GetAPPPath() & "arj.exe e -y " & usr_tempfile & " " & usr_tempdir
Result = Shell(FileString, vbHide)
'If Result = 0 Then '解压失败
' 'end
'End If

hProc = OpenProcess(PROCESS_QUERY_INFORMATION, False, Result)
Do
GetExitCodeProcess hProc, lExit
DoEvents
Loop While lExit <> STILL_ACTIVE
;下面的这里的程序要用到解压后的文件
......



要给我加分啊,我分太少了,老没人给我加

7,759

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧