Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending exit code to calling program

Status
Not open for further replies.

GAG

Programmer
Mar 20, 2002
1
US
I am having a problem getting a vb program to send an exit code back to a calling system on another machine. The program is relatively simple it reads a sql 7 db to get a file number and the creates a lan directory using that number and moves files from a lan directory that recieves the files from a mainframe job to the newly created directory. this job is started remotely by a vendor system and runs by itself with no interaction. if it abends I need to set an exit code or return code to let the calling system know it has abended.
 
Hello;

I think I'm looking for the same answer except in my case I'm trying to get a command-line utility (console app?) to return exit codes so that I can use it in a batch file.

Unforturnately, the outlook is not looking too good for VB 5.0 since I ran across this from microsoft: "KB171654: If a Visual Basic application is started from a console application, the operating system automatically detaches it from the console, preventing the Visual Basic application from interacting with it." I'm still hoping to come up with something....

Michelle
 
You are limited in what you can return, but if you take a look at the ExitProcess API, you can get the program to return a long value to its calling process. Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Here is an example of how to obtain an exit code. It uses Notepad.exe in this sample. Both Notepad and the vb app are running on the same PC.

Copy the code below into a form module with a command button named Command1


Option Explicit
Const NORMAL_PRIORITY_CLASS As Long = &H20

Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End Type

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 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, lpEnvironment As Any, ByVal lpCurrentDirectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) '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)



Private Sub Command1_Click()
Dim lProcess As Long
Dim StartInfo As STARTUPINFO
Dim ProcessInfo As PROCESS_INFORMATION
Dim lExitCode As Long
Dim iRetrievedProcessCode As Long

Const INFINITE = &HFFFF

lProcess = CreateProcess("C:\WINNT\Notepad.exe", vbNullString, 0, 0, True, NORMAL_PRIORITY_CLASS, ByVal 0&, vbNullString, StartInfo, ProcessInfo)
WaitForSingleObject ProcessInfo.hProcess, INFINITE ' INFINITE = wait for in milliseconds, waits forever inthis case
iRetrievedProcessCode = GetExitCodeProcess(ProcessInfo.hProcess, lExitCode)
If iRetrievedProcessCode <> 0 Then

MsgBox &quot;Notepade was terminated&quot; & vbCrLf & vbCrLf & &quot;Exit Code = &quot; & Format$(lExitCode)
Else
MsgBox &quot;Could not obtain exit code. . . &quot;
End If

End Sub
 
Hello;

Interesting discussion but I'm concerned about using ExitProcess() as Microsoft says that &quot;If a Visual Basic application makes a direct call to the ExitProcess() API, the process may not properly exit.&quot; While the complete text is available at MS is basically saying that a VB application is actually runned using the run-time engine and that it allocates resources that might not be released when ExitProcess() is called.

CajunCenturian, can you tell me if this is an issue if the VB program is compiled to native code instead of P-code? If it is an issue, how can I test my program to see if it is leaving a mess behind?

Unfortunately, the idea posted by RonVaught won't work for me since I'm not trying to get an exit code from a program that I've called but return an exit code to some unknown program that called my program. On the other hand, it could be useful in another program that I'm considering.

Michelle
 
There are some issues with ExitProcess, but if your program has properly shut down and released all of its resources before calling the ExitProcess API, you should be ok. Its not that the ExitProcess function allocates resources that may not be cleaned up, the point is that if your program allocates some resources, and then calls ExitProcess before releasing those resources, then those resources become orphaned.

That being said, I'm not sure that it will work in your case anyway, because I'm not sure how to get the exit process value to the correct program, unless thru some very tricky API calls you were to manipulate the processes and threads to connect the various processes together. But if there is one thing that I've learned from this forum, its never say that something cannot be done.

A couple of other options might be to post a message to your other process, but that could be problematic if the other process is not running, or is running on a different machine.

You might want to consider have your program write out to a file the desired results in a shared location, where the those programs who might be interested in the results can read that and proceed accordingly. You could use the Events of the FileSystemObject to trigger when a response file exists. Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top