Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Visual Basic Programming Code Examples

Visual Basic > Applications VBA Code Examples

Returning an ExitCode parameter from an out of process application

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
Returning an ExitCode parameter from an out of process application When creating out of process applications (ie. using DOS applications or Shelling VB apps), it is often extremely useful to be able to return a parameter from the other application, eg. returning error numbers. The following code creates two projects, one called "Return Param.exe" and the other called "Demo.exe". The Demo program will shell the Return Param application and when the user clicks close a numeric parameter from a textbox will be returned to the Demo application. Note, in order for an ExitProcess value to be returned at least one form must have been loaded then unloaded. Code for Demo.exe: ------------------ 'Requires a single standard module Option Explicit Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds 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 Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Long) As Long 'This Sub Main() Dim lTaskID As Long, lPID As Long, lExitCode As Long, sAppDir As String Const INFINITE = &HFFFFFFFF ' Infinite timeout Const SYNCHRONIZE = &H100000 Const STILL_ACTIVE = 0 Const STANDARD_RIGHTS_REQUIRED = &HF0000 Const PROCESS_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF) sAppDir = App.Path If Right$(sAppDir, 1) <> "\" Then sAppDir = sAppDir & "\" End If MsgBox "This application will now start another application (out of process)." & vbNewLine & _ "Please enter a return value and click Close" & vbNewLine & _ "This display the value entered in the other application...", vbInformation 'Shell Application lTaskID = Shell(sAppDir & "Return Param.exe", vbNormalFocus) 'Get process handle lPID = OpenProcess(PROCESS_ALL_ACCESS, True, lTaskID) If lPID Then 'Wait for process to finish 'Note, you must now enter a value in the form and click close. Call WaitForSingleObject(lPID, INFINITE) 'Get Exit Process If GetExitCodeProcess(lPID, lExitCode) Then 'Received value MsgBox "Successfully returned " & lExitCode, vbInformation Else MsgBox "Failed: " & DLLErrorText(Err.LastDllError), vbCritical End If Else MsgBox "Failed: " & DLLErrorText(Err.LastDllError), vbCritical End If lTaskID = CloseHandle(lPID) End Sub 'Purpose : Return the error message associated with LastDLLError 'Inputs : lLastDLLError The error number of the last DLL error (from Err.LastDllError) 'Outputs : Returns the error message associated with the DLL error number Public Function DLLErrorText(ByVal lLastDLLError As Long) As String Dim sBuff As String * 256 Dim lCount As Long Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100, FORMAT_MESSAGE_ARGUMENT_ARRAY = &H2000 Const FORMAT_MESSAGE_FROM_HMODULE = &H800, FORMAT_MESSAGE_FROM_STRING = &H400 Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000, FORMAT_MESSAGE_IGNORE_INSERTS = &H200 Const FORMAT_MESSAGE_MAX_WIDTH_MASK = &HFF lCount = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM Or FORMAT_MESSAGE_IGNORE_INSERTS, 0, lLastDLLError, 0&, sBuff, Len(sBuff), ByVal 0) If lCount Then DLLErrorText = Left$(sBuff, lCount - 2) 'Remove line feeds End If End Function Code for Return Param.exe: -------------------------- 'Requires a form, which contains: '1. A command button called cmdClose '2. A text box called txtRetVal Option Explicit Private Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long) Private Sub cmdClose_Click() 'Must unload the application before returning the ExitCode Unload Me End Sub Private Sub Form_Unload(Cancel As Integer) Dim lExitCode as Long On Error Resume Next lExitCode = CLng(Me.txtRetVal) 'Return exit code ExitProcess lExitCode End Sub