En gros vb6 lance un processus mais le problème est de le fermer quand il se termine. Lorsque le programme externe affiche msgbox en disant «terminé», il peut être fermé lorsque le programme externe affiche msgbox. Cependant, quand il affiche le msgbox, le processus est toujours en cours d'exécution dans taskmgr. Comment détecter le msgbox et tuer le programme?vb6: comment exécuter un programme à partir de vb6 et le fermer une fois qu'il a terminé?
3
A
Répondre
1
Essayez cette
Option Explicit
'--- for CreateProcess
Private Const NORMAL_PRIORITY_CLASS As Long = &H20&
Private Const STARTF_USESHOWWINDOW As Long = 1
Private Const SW_HIDE As Long = 0
Private Const SW_SHOWDEFAULT As Long = 10
Private Const ERROR_ELEVATION_REQUIRED As Long = 740
'--- for WaitForXxx
Private Const INFINITE As Long = &HFFFFFFFF
'--- for ShellExecuteEx
Private Const SEE_MASK_NOCLOSEPROCESS As Long = &H40
Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (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 ShellExecuteEx Lib "shell32.dll" Alias "ShellExecuteExA" (lpExecInfo As SHELLEXECUTEINFO) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
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 Type SHELLEXECUTEINFO
cbSize As Long
fMask As Long
hWnd As Long
lpVerb As String
lpFile As String
lpParameters As String
lpDirectory As Long
nShow As Long
hInstApp As Long
' Optional fields
lpIDList As Long
lpClass As Long
hkeyClass As Long
dwHotKey As Long
hIcon As Long
hProcess As Long
End Type
Private Const MSG_ELEVATION_REQUIRED As String = "To run %1 as administrator please confirm next elevation of rights"
Public Function ShellWait(_
ByVal sFile As String, _
Optional sParams As String, _
Optional ByVal bStartHidden As Boolean, _
Optional oOwnerForm As VB.Form) As Long
Const FUNC_NAME As String = "ShellWait"
Dim sCommandLine As String
Dim uInfo As PROCESS_INFORMATION
Dim uStart As STARTUPINFO
Dim lExitCode As Long
Dim uShell As SHELLEXECUTEINFO
Dim sFileName As String
On Error GoTo EH
'--- win9x: fix spaces or not working on 9x
If InStr(sFile, " ") > 0 Then
sCommandLine = """" & sFile & """" & " " & sParams
Else
sCommandLine = sFile & " " & sParams
End If
uStart.cb = Len(uStart)
If bStartHidden Then
uStart.dwFlags = STARTF_USESHOWWINDOW
uStart.wShowWindow = SW_HIDE
End If
If CreateProcess(vbNullString, sCommandLine, 0, 0, 1, NORMAL_PRIORITY_CLASS, 0, vbNullString, uStart, uInfo) <> 0 Then
Call WaitForSingleObject(uInfo.hProcess, INFINITE)
If GetExitCodeProcess(uInfo.hProcess, lExitCode) <> 0 Then
ShellWait = lExitCode
End If
Call CloseHandle(uInfo.hThread)
Call CloseHandle(uInfo.hProcess)
Else
If Err.LastDllError = ERROR_ELEVATION_REQUIRED Then
If Not oOwnerForm Is Nothing Then
If InStrRev(sFile, "\") > 0 Then
sFileName = Mid(sFile, InStrRev(sFile, "\") + 1)
Else
sFileName = sFile
End If
MsgBox Replace(MSG_ELEVATION_REQUIRED, "%1", sFileName), vbExclamation
uShell.hWnd = oOwnerForm.hWnd
End If
With uShell
.cbSize = Len(uShell)
.fMask = SEE_MASK_NOCLOSEPROCESS
.lpVerb = "runas"
.lpFile = sFile
.lpParameters = sParams
.nShow = IIf(bStartHidden, SW_HIDE, SW_SHOWDEFAULT)
End With
If ShellExecuteEx(uShell) Then
Call WaitForSingleObject(uShell.hProcess, INFINITE)
If GetExitCodeProcess(uShell.hProcess, lExitCode) <> 0 Then
ShellWait = lExitCode
End If
Call CloseHandle(uShell.hProcess)
End If
End If
End If
Exit Function
EH:
Debug.Print FUNC_NAME; ": "; Error
Resume Next
End Function
Private Sub Command1_Click()
MsgBox "Exit code = " & ShellWait("cmd"), vbExclamation
End Sub
0
Si vous connaissez le titre ou le nom de classe du programme, vous pouvez utiliser les appels API FindWindow et PostMessage pour le fermer.
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const WM_CLOSE = &H10
Dim hwnd As Long
hwnd = FindWindow(vbNullString, "WINDOW CAPTION HERE")
PostMessage hwnd, WM_CLOSE, CLng(0), CLng(0)
Questions connexes
- 1. Comment exécuter un projet VB6 dans Hudson?
- 2. Comment fermer un processus shell dans VB6
- 3. Exécuter le package SSIS à partir de VB6
- 4. Comment exécuter google voice à partir de vb6
- 5. Ajouter du son à un programme VB6
- 6. Comment contrôler firefox à partir de vb6
- 7. transcription de VB.NET à VB6
- 8. Comment exécuter un module Access 2007 dans VB6?
- 9. formatage dans Excel à partir de VB6
- 10. Comment renommer un exécutable VB6?
- 11. Le programme VB6 plante pendant DoEvents, pourquoi?
- 12. VB6: créer IHTMLElementCollection à partir de zéro
- 13. Consommez un service web Coldfusion à partir de VB6
- 14. Appel d'une méthode asynchrone C# à partir d'une application VB6
- 15. Lire le fichier XLS à partir de VB6
- 16. VB6 Empaquetage et organisation
- 17. Télécharger le fichier - VB6
- 18. VB6 Implémentations et événements
- 19. Débogage de VB6 dans VS2008 avec des objets non-VB6
- 20. VB6 OpenRecordSet a trop peu de paramètres?
- 21. vb6 affichant le problème
- 22. Comment tracer une ligne dynamiquement dans vb6?
- 23. SetParent() sur une application VB6 à partir d'une application C#
- 24. Copier une seule ligne à partir d'un RecordSet (VB6)
- 25. Comment lire une ligne de texte à la fois et réécrire dans un autre fichier dans VB6
- 26. VB6 à C# que signifie ce code VB6?
- 27. Désactiver HotKeys lors de la frappe - VB6
- 28. Intégration de VB6 à TFS
- 29. Exécuter un programme à partir de MemoryStream
- 30. Comment appeler une DLL Delphi de VB6