vb| Solution | Source code | perform |shell| asynchronous | sync
Arisisi Web development team in the development of an ASP system, the need for Synchronous call VB (DLL) to perform shell operations, ASP->VB (DLL)->shell->rar.exe synchronous execution exe file.
Because the shell is asynchronous processing, this problem let us headache for a long time, and finally through Baidu found a few good functions, debugging, the effect is good, suggest you can use the VB build EXE to debug well, and then put the DLL compiled to ASP call.
The code is as follows:
The following is the use of VB to make an EXE file for debugging, open Notepad and calculator examples: 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 waitforsingleobject Lib "kernel32" (ByVal _ Hhandle as Long, ByVal dwmilliseconds as Long
Private Declare Function createprocessa Lib "kernel32" (ByVal _ Lpapplicationname As String, 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 String, _ Lpstartupinfo as Startupinfo, lpprocessinformation as _ process_information) as Long
Private Declare Function closehandle Lib "Kernel32" _ (ByVal hobject as Long) As Long
Private Declare Function getexitcodeprocess Lib "Kernel32" _ (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
' Initialize The STARTUPINFO structure: START.CB = Len (start)
' Start the shelled application: ret& = CreateProcessA (vbNullString, cmdline$, 0&, 0&, 1&, _ Normal_priority_class, 0&, vbNullString, start, proc)
' Shelled application to finish: ret& = WaitForSingleObject (proc.hprocess, INFINITE) Call GetExitCodeProcess (proc.hprocess, ret&) Call CloseHandle (Proc.hthread) Call CloseHandle (proc.hprocess) ExecCmd = ret& End Function
Sub Form_Click () Dim RetVal as Long retval = ExecCmd ("notepad.exe") MsgBox "Notepad Process finished, Exit Code" & retval retval = ExecCmd ("Calc.exe") MsgBox "Calc Process finished, Exit Code" & retval
End Sub |
Method Two:
The following is a reference fragment: Private Declare Function openprocess Lib "kernel32" (ByVal dwdesiredaccess as Long, ByVal binherithandle as Long, ByVal DW ProcessID as long) as long Private Declare Function closehandle Lib "kernel32" (ByVal Hobject as long) as long Private Declare Function waitforsingleobject Lib "kernel32" (ByVal hhandle as Long, ByVal dwmilliseconds as long) as long
Private Const SYNCHRONIZE = &h100000 Private Const INFINITE = &hffff ' INFINITE timeout Private Const wait_timeout = &H102&
Public Function shellforwait (sappname as String, Optional ByVal lshowwindow as VbAppWinStyle = Vbminimizedfocus, Optional ByVal lwaittime as Long = 0) as Boolean Dim LID as Long, lhnd as long, lret as Long On Error Resume Next LID = Shell (Sappname, Lshowwindow) If LID > 0 Then LHND = openprocess (SYNCHRONIZE, 0, LID) If lhnd <> 0 Then Todo Lret = WaitForSingleObject (lhnd, Lwaittime) DoEvents Loop while Lret = Wait_timeout CloseHandle LHND shellforwait = True Else shellforwait = False End If Else shellforwait = False End If End Function
Shellforwait ("notepad.exe", &HFFFF) |
Method Three:
The following is a reference fragment: Private Declare Function openprocess Lib "kernel32" (ByVal dwdesiredaccess as Long, ByVal binherithandle as Long, ByVal DW ProcessID as long) as long Private Const SYNCHRONIZE = &h100000 ' Process sync Private Declare Function closehandle Lib "kernel32" (ByVal Hobject as long) as long Private Declare Function waitforsingleobject Lib "kernel32" (ByVal hhandle as Long, ByVal dwmilliseconds as long) as long Private Const INFINITE = &hffffffff
Private Sub Cmdopen_click () Openfilewait "C;\windows\hh.exe", "Help.chm" End Sub
Private Sub openfilewait (tkshellfile As String, Tkfilename as String) Wndid = Shell (Tkfilename, Vbnormalfocus) WND = openprocess (SYNCHRONIZE, 0, Wndid) WaitForSingleObject Wnd, INFINITE CloseHandle WND End Sub |
If you feel good, Arisisi (alixixi.com) site There are more wonderful resources, do not miss, remember our website!