Click to See Complete Forum and Search --> : VB app returning user defined ExitCode
Dunebuggy
Mar 8th, 2001, 06:48 AM
Hi all,
Perhaps there is someone who has knowledge about how the following is done. I want to terminate my program with an exit code I chose.
This is needed for my program because my program is called by another which should exit itself when a specific exit code is returned by my app.
I thought it would be possible by using the ExitProcess Api call. But it crashes the program with an illegal operation. (Though this happens at the end of Sub Main)
What should i do in my application to sucessfully return my own specified exit code ?
Greetings,
Dunebuggy
Try this:
Private Declare Sub ExitProcess Lib "kernel32" _
(ByVal uExitCode As Long)
Private Declare Function GetExitCodeProcess _
Lib "kernel32" (ByVal hProcess As Long, lpExitCode As _
Long) As Long
Private Declare Function GetCurrentProcess _
Lib "kernel32" () As Long
Private Sub Form_Load()
'KPD-Team 1999
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
'end this process
ExitProcess GetExitCodeProcess(GetCurrentProcess, 0)
End Sub
The PostQuitMessgage function is used to exit an application. You can specify and exit code as well.
Private Declare Sub PostQuitMessage Lib "user32" Alias "PostQuitMessage" (ByVal nExitCode As Long)
Usage:
PostQuitMessage(0)
Dunebuggy
Mar 9th, 2001, 05:05 AM
Thank you both for replying.
I forgot to tell you that this illegal exception only occurred when i ran the application from the vb6 ide.
PostQuitMessage with an argument worked better as it did not trow an exception. But it also stopped my VB6 IDE.
However i do understand why this is happening. The actual process being quit is not the application but the vb6 IDE. Both your solutions terminate the vb6.exe process :o)
I guess the vb6 IDE does not run the application as a seperate process but as a thread of some sort from the vb6 process. Don't pin me on this assumtion as i have not yet verified this.
After building the application and running it the code worked using PostQuitMessage.
Matthew: Sorry to tell you but you solution didn't work.
PostQuitMessage worked like a charm.
Question : How can this be made working when running from the IDE as to running as an .exe
Thanks,
Dunebuggy
Dunebuggy
Mar 9th, 2001, 06:19 AM
Sorry.. I spoke too early as that PostQuitMessage doesn't set the return value correct.
<code>
PostQuitMessage 1
</code>
Still ends the process with 0
Perhaps because PostQuitMessage only works on the thread it is called upon.
<code>
ExitProcess 1
</code>
Works fine. Only when i do NOT run the program from the IDE. Starting the program from the IDE runs the program from a thread in the process of the vb6.exe
greetings,
Dunebuggy
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.