Hi folks,

When I try to invoke MS-DOS programm from VB, the exit code return is aways
0.
My code is:

Option Explicit

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 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 GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, lpExitCode As Long) As Long


Public Function ExecCmd(cmdline$)
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim procId As Long

start.cb = Len(start)

procId = Shell(cmdline$, vbNormalFocus)


proc.hProcess = OpenProcess(ACCESS_TYPE, False, procId)

Call WaitForSingleObject(proc.hProcess, INFINITE)
Call GetExitCodeProcess(proc.hProcess, procId)

ExecCmd = procId
End Function

Private Sub CommandButton1_Click()
Dim retval As Long
retval = ExecCmd("java -jar Test.jar abc")
MsgBox "Process Finished, Exit Code " & retval
End Sub

When I run this command (java -jar Test.jar abc) the exit codes are between
3 and -6.

Any help and/or ideas?

Plamen.

Re: Problem getting exit code returned by DOS command by Jonathan

Jonathan
Mon Oct 30 07:45:18 CST 2006

Hi Plamen,

If you are trying to code a "Shall and Wait" process, there's no need to go
in for wheel-reinvention. Use the code here instead

http://vb.mvps.org/samples/project.asp?id=Shell32


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org

"pkirow" <pkirow@discussions.microsoft.com> wrote in message
news:1FE9BFDA-88BE-4EA0-9B56-B9DC529D1AA9@microsoft.com...
> Hi folks,
>
> When I try to invoke MS-DOS programm from VB, the exit code return is
> aways
> 0.
> My code is:
>
> Option Explicit
>
> 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 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 GetExitCodeProcess Lib "kernel32" _
> (ByVal hProcess As Long, lpExitCode As Long) As Long
>
>
> Public Function ExecCmd(cmdline$)
> Dim proc As PROCESS_INFORMATION
> Dim start As STARTUPINFO
> Dim procId As Long
>
> start.cb = Len(start)
>
> procId = Shell(cmdline$, vbNormalFocus)
>
>
> proc.hProcess = OpenProcess(ACCESS_TYPE, False, procId)
>
> Call WaitForSingleObject(proc.hProcess, INFINITE)
> Call GetExitCodeProcess(proc.hProcess, procId)
>
> ExecCmd = procId
> End Function
>
> Private Sub CommandButton1_Click()
> Dim retval As Long
> retval = ExecCmd("java -jar Test.jar abc")
> MsgBox "Process Finished, Exit Code " & retval
> End Sub
>
> When I run this command (java -jar Test.jar abc) the exit codes are
> between
> 3 and -6.
>
> Any help and/or ideas?
>
> Plamen.