shell funktionen er designet sådan at ikke kører som en del af dit program dvs. at dit program ikke stopper indtil shell er færdig.
dette er pr. design da der jo er mange anvendelses muligheder for shell som ikke behøver at have noget med dit program at gøre.
hvis man er afhængig af et resultat fra shell funktionen kunne man måske lave en løkke hvor man søger efter shell opgaven i de kørende opgaver og når den ikke længere findes må shell opgaven være færdig.
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Const STILL_ACTIVE As Long = &H103 Private Const PROCESS_QUERY_INFORMATION As Long = &H400
Public Sub ShellWait(ByVal strFile As String, _ Optional emuStyle As VbAppWinStyle = vbNormalFocus) Dim lR As Long Dim hProcess As Long hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, _ Shell(strFile, emuStyle)) If (hProcess <> 0) Then Do Call GetExitCodeProcess(hProcess, lR) DoEvents: Sleep 1 Loop While lR = STILL_ACTIVE End If End Sub
Private Sub Command1_Click() Const FILE As String = "c:\winnt\system32\notepad.exe" Call ShellWait(FILE) MsgBox FILE End Sub '---------------------------------------- Form1 ----------------------------------------
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Const STILL_ACTIVE As Long = &H103 Private Const PROCESS_QUERY_INFORMATION As Long = &H400
Public Sub ShellWait(ByVal strFile As String, _ Optional emuStyle As VbAppWinStyle = vbNormalFocus) Dim lR As Long Dim hProcess As Long hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, _ Shell(strFile, emuStyle)) If (hProcess <> 0) Then Do Call GetExitCodeProcess(hProcess, lR) DoEvents: Sleep 1 Loop While lR = STILL_ACTIVE End If End Sub
Private Sub Command2_Click() Dim frasti, program, hjemdir As String Dim udpak As FILE If Text1.Text = "" Then Exit Sub frasti = Chr(34) & Text1.Text & Chr(34) program = "wzunzip -d -o" hjemdir = Chr(34) & App.Path & Chr(34) ChDrive ("C:\") ChDrive ("C:\") udpak = "wzunzip -d -o" & " " & frasti & " frasti.txt " & "C:\" Const FILE As String = udpak Label1.Caption = "VENT VENLIGST" Label1.Refresh Call ShellWait(FILE)
1 -> Dim udpak As FILE skal nok være Dim udpak As String 2 -> så gør du sådan: Call ShellWait(udpak) 3 -> Prøv om du kan få din kode til at virke ved at skrive: Shell(strFile, vbNormalFocus)
Lidt sent men: Jeg bruger denne kode uden problemer: Opret en form: 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
Private Declare Function OpenProcess Lib "kernel32" _ (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _ ByVal dwProcessId As Long) As Long
Private Sub Command1_Click() Dim iTask As Long, ret As Long, pHandle As Long iTask = Shell("notepad.exe", vbNormalFocus) pHandle = OpenProcess(SYNCHRONIZE, False, iTask) ret = WaitForSingleObject(pHandle, INFINITE) ret = CloseHandle(pHandle) MsgBox "Process Finished!" End Sub
/Søren
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.