VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



end the program with ExitCode for use in .bat files (errorlevel), Execute others programs from VB a

by Serge Lachapelle (10 Submissions)
Category: Windows System Services
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Fri 12th July 2002
Date Added: Mon 8th February 2021
Rating: (1 Votes)

end the program with ExitCode for use in .bat files (errorlevel), Execute others programs from VB and return the ExitCode.

Rate end the program with ExitCode for use in .bat files (errorlevel), Execute others programs from VB a




Public Enum PRIORITY_CLASS
  REALTIME_PRIORITY = &H100
  HIGH_PRIORITY = &H80
  NORMAL_PRIORITY = &H20
  IDLE_PRIORITY = &H40
End Enum

Private Type STARTUPINFO
    cb As Long
    lpReserved As Long
    lpDesktop As Long
    lpTitle As Long
    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 VbAppWinStyle
    cbReserved2 As Integer
    lpReserved2 As Byte
    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 Sub ExitProcess Lib "Kernel32.dll" (ByVal uExitCode As Long)
Private Declare Function WinExec Lib "Kernel32.dll" (ByVal lpCmdLine As String, ByVal nCmdShow As Long) As Long
Private Declare Function CreateProcess Lib "Kernel32.dll" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As Any, lpThreadAttributes As Any, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function WaitForSingleObject Lib "Kernel32.dll" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function GetExitCodeProcess Lib "Kernel32.dll" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function CloseHandle Lib "Kernel32.dll" (ByVal hObject As Long) As Long

Public Sub End2(ByVal uExitCode As Long)
  ExitProcess uExitCode
End Sub

Public Function Exec(ByVal CmdLine As String, Optional WindowStyle As VbAppWinStyle = vbNormalFocus) As Boolean
  Dim var1 As Long
  var1 = WinExec(CmdLine, WindowStyle)
  If var1 > 31 Then
    Exec = True
  Else
    Exec = False
  End If
End Function

Public Function Exec2(ByVal CmdLine As String, Optional ByVal WindowStyle As VbAppWinStyle = vbNormalFocus, Optional zWait As Boolean = False, Optional ByVal pclass As PRIORITY_CLASS = NORMAL_PRIORITY) As Variant
  Const Infinite As Long = &HFFFFFFFF
  Const STILL_ACTIVE As Long = &H103
  Dim sinfo As STARTUPINFO, pinfo As PROCESS_INFORMATION
  Dim CmdLine As String, ExitCode As Long
  sinfo.cb = Len(sinfo)
  sinfo.dwFlags = &H1
  sinfo.wShowWindow = WindowStyle
  If CreateProcess(vbNullString, CmdLine, ByVal 0&, ByVal 0&, 1&, pclass, ByVal 0&, vbNullString, sinfo, pinfo) <> 0 Then
    If zWait = True Then WaitForSingleObject pinfo.hProcess, Infinite
    Do
      GetExitCodeProcess pinfo.hProcess, ExitCode
      DoEvents
    Loop While ExitCode = STILL_ACTIVE
    CloseHandle pinfo.hThread
    CloseHandle pinfo.hProcess
    Exec2 = ExitCode
  Else
    Exec2 = "ERROR"
  End If
End Function


Download this snippet    Add to My Saved Code

end the program with ExitCode for use in .bat files (errorlevel), Execute others programs from VB a Comments

No comments have been posted about end the program with ExitCode for use in .bat files (errorlevel), Execute others programs from VB a. Why not be the first to post a comment about end the program with ExitCode for use in .bat files (errorlevel), Execute others programs from VB a.

Post your comment

Subject:
Message:
0/1000 characters