Return the content of DOS Prompt?


Here is a simple example that Compile the Java file (*.java) to Class file (*.class) using the Java Compiler (javac.exe).


Option Explicit
Dim temp As String, CommandLine As String, Result As Long, i As Long

Private Sub cmdClose_Click()
    Unload Me
End Sub

Private Sub cmdCompileNow_Click()
    CommandLine = txtCompiler & " -classpath " & txtServlet & " " & txtJava
    Shell CommandLine, vbNormalFocus

    If chkClose.Value = 1 Then Unload Me
End Sub

Private Sub cmdCompiler_Click()
    temp = "" & OpenDialogBox(Me, "Exe" + Chr$(0) + "*.exe", "Select the Javac.exe file", dlgOpen)
    If LCase$(Dir$(temp, vbNormal)) = "javac.exe" Then txtCompiler = temp
End Sub

Private Sub cmdJava_Click()
    txtJava = "" & OpenDialogBox(Me, "Java File" + Chr$(0) + "*.java", "Select a Java file", dlgOpen)
End Sub

Private Sub cmdServlet_Click()
    temp = "" & OpenDialogBox(Me, "Jar" + Chr$(0) + "*.jar", "Select the Servlet.jar file", dlgOpen)
    If LCase$(Dir$(temp, vbNormal)) = "servlet.jar" Then txtServlet = temp
End Sub

Private Sub Form_Load()
    chkClose = Val(QueryValue(HKEY_LOCAL_MACHINE, "Software\cys\MyJavaCompiler\Setting", "chkClose"))
    txtCompiler = "" & QueryValue(HKEY_LOCAL_MACHINE, "Software\cys\MyJavaCompiler\Setting", "txtCompiler")
    txtServlet = "" & QueryValue(HKEY_LOCAL_MACHINE, "Software\cys\MyJavaCompiler\Setting", "txtServlet")
    txtJava = "" & QueryValue(HKEY_LOCAL_MACHINE, "Software\cys\MyJavaCompiler\Setting", "txtJava")
End Sub

Private Sub Form_Unload(Cancel As Integer)
    i = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\cys\MyJavaCompiler\Setting", 0, KEY_ALL_ACCESS, Result)
    If Result = 0 Then i = CreateNewKey(HKEY_LOCAL_MACHINE, "Software\cys\MyJavaCompiler\Setting"): Result = 0
    i = SetKeyValue(HKEY_LOCAL_MACHINE, "Software\cys\MyJavaCompiler\Setting", "chkClose", chkClose, REG_SZ)
    i = SetKeyValue(HKEY_LOCAL_MACHINE, "Software\cys\MyJavaCompiler\Setting", "txtCompiler", txtCompiler, REG_SZ)
    i = SetKeyValue(HKEY_LOCAL_MACHINE, "Software\cys\MyJavaCompiler\Setting", "txtServlet", txtServlet, REG_SZ)
    i = SetKeyValue(HKEY_LOCAL_MACHINE, "Software\cys\MyJavaCompiler\Setting", "txtJava", txtJava, REG_SZ)
    Set frmCompileJava = Nothing
End Sub

Private Sub txtCompiler_Change()
    txtCompiler.ToolTipText = "" & txtCompiler
End Sub

Private Sub txtJava_Change()
    txtJava.ToolTipText = "" & txtJava
End Sub

Private Sub txtServlet_Change()
    txtServlet.ToolTipText = "" & txtServlet
End Sub


Nothing wrong with the program, it runs smoothly, but the Dos Prompt close itself after the Shell function is executed:

CommandLine = txtCompiler & " -classpath " & txtServlet & " " & txtJava
Shell CommandLine, vbNormalFocus

(The CommandLine can be:
C:\jdk1.3.1_02\bin\javac.exe -classpath C:\jakarta-tomcat-4.0.3\common\lib\servlet.jar C:\jakarta-tomcat-4.0.3\webapps\test\WEB-INF\classes\

For the example above, if the shell function executed successful, a new class file (HelloWorld.class) will be compiled in folder C:\jakarta-tomcat-4.0.3\webapps\test\WEB-INF\classes.

But if there is already a HelloWorld.class in the directory, the Dos Prompt will show the errors, then it close itself too.

So, is there a method that i can get what is return from the Dos Prompt? What may be difficult is the Dos Prompt CLOSE automatically regardless whether the java file successfully compile to class file or not. Is this Dos Prompt behave like this (Close when process finished) ? or is there a method i can get the message in Dos Prompt? Thanks.

* Note: Current here is a solution, that's if the class file exist in the directory, kill it if it's exist, then compile the java file. But this method is not a good solution as the latest java file may contain errors and no class file is compiled/generated.
Who is Participating?

Improve company productivity with a Business Account.Sign Up

Richie_SimonettiConnect With a Mentor IT OperationsCommented:
You could edit pif file to belongs to dospropmpt and uncheck Close on exit check box.

If that's not enougth:

HOWTO: Create a Process for Reading and Writing to a Pipe
Last reviewed: August 26, 1997
Article ID: Q173085  
The information in this article applies to:
Microsoft Visual Basic Control Creation and Enterprise Editions for Windows, version 5.0
Microsoft Visual Basic Professional and Enterprise Editions, 32-bit only, for Windows, version 4.0

This example illustrates a Visual Basic application starting another process with the purpose of redirecting that process's standard IO handles. The Visual Basic application redirects the created process's standard output handle to an anonymous pipe, then proceeds to read the output through the pipe. This sample just redirects STDOUT of the new process. To redirect other handles (STDIN and STDERR), create a pipe for each handle for which redirection is desired. The Visual Basic application would read from the read ends of the pipes for the redirected STDOUT and STDERR. If STDIN redirection was desired, the Visual Basic application would write to the write end of the appropriate pipe. An example follows:

   'A pipe for redirection of STDOUT
   CreatePipe(hReadPipe1, hWritePipe1, sa, 0)
   'A pipe for redirection of STDERR
   CreatePipe(hReadPipe2, hWritePipe2, sa, 0)
   'A pipe for redirection of STDIN
   CreatePipe(hReadPipe3, hWritePipe3, sa, 0)
   'Preparing to start the process with redirected handles
   start.hStdOutput = hWritePipe1
   start.hStdError = hWritePipe2
   start.hStdInput = hReadPipe3
   'Reading output from the started process's STDOUT
   ReadFile(hReadPipe1, mybuff1, 100, bytesread, ByVal 0&)
   'Reading output from the started process's STDERR
   ReadFile(hReadPipe2, mybuff2, 100, bytesread, ByVal 0&)
   'Writing to the started process's STDIN
   WriteFile(hWritePipe3, mybuff3, 100, byteswritten, ByVal 0&)


Start a new project in Visual Basic. Form1 is created by default.

Add the following code to the General Declarations section of Form1:

      Option Explicit
      Private Declare Function CreatePipe Lib "kernel32" ( _
          phReadPipe As Long, _
          phWritePipe As Long, _
          lpPipeAttributes As Any, _
          ByVal nSize As Long) As Long
      Private Declare Function ReadFile Lib "kernel32" ( _
          ByVal hFile As Long, _
          ByVal lpBuffer As String, _
          ByVal nNumberOfBytesToRead As Long, _
          lpNumberOfBytesRead As Long, _
          byval lpOverlapped As Any) As Long
          nLength As Long
          lpSecurityDescriptor As Long
          bInheritHandle As Long
      End Type

      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 Integer
         cbReserved2 As Integer
         lpReserved2 As Long
         hStdInput As Long
         hStdOutput As Long
         hStdError As Long
      End Type
         hProcess As Long
         hThread As Long
         dwProcessID As Long
         dwThreadID As Long
      End Type

      Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
         lpApplicationName As Long, ByVal lpCommandLine As String, _
         lpProcessAttributes As Any, lpThreadAttributes As Any, _
         ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
         ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
         lpStartupInfo As Any, lpProcessInformation As Any) As Long
      Private Declare Function CloseHandle Lib "kernel32" (ByVal _
         hObject As Long) As Long

      Private Const NORMAL_PRIORITY_CLASS = &H20&
      Private Const STARTF_USESTDHANDLES = &H100&
      Private Sub ExecCmd(cmdline$)
          Dim proc As PROCESS_INFORMATION, ret As Long, bSuccess As Long
          Dim start As STARTUPINFO
          Dim sa As SECURITY_ATTRIBUTES, hReadPipe As Long, hWritePipe _
          As Long
          Dim bytesread As Long, mybuff As String
          Dim i As Integer
          mybuff = String(256, Chr$(65))
          sa.nLength = Len(sa)
          sa.bInheritHandle = 1&
          sa.lpSecurityDescriptor = 0&
          ret = CreatePipe(hReadPipe, hWritePipe, sa, 0)
          If ret = 0 Then
              MsgBox "CreatePipe failed. Error: " & Err.LastDllError
              Exit Sub
          End If

       start.cb = Len(start)
       start.dwFlags = STARTF_USESTDHANDLES
       start.hStdOutput = hWritePipe
       ' Start the shelled application:
       ret& = CreateProcessA(0&, cmdline$, sa, sa, 1&, _
       NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
       If ret <> 1 Then
           MsgBox "CreateProcess failed. Error: " & Err.LastDllError
       End If
       bSuccess = ReadFile(hReadPipe, mybuff, 100, bytesread, 0&)
       If bSuccess = 1 Then
           List1.AddItem Left(mybuff, bytesread)
           MsgBox "ReadFile failed. Error: " & Err.LastDllError
       End If

          ret& = CloseHandle(proc.hProcess)
          ret& = CloseHandle(proc.hThread)
          ret& = CloseHandle(hReadPipe)
          ret& = CloseHandle(hWritePipe)
      End Sub

Add a CommandButton (Command1) to Form1.

Add the following code to the Command1_Click event:

      Private Sub Command1_Click()
              ExecCmd ("C:\Console\Debug\console.exe")
      End Sub

Richie_SimonettiIT OperationsCommented:
I meant:
You could edit pif file called "dosprompt" located in c:\windows and uncheck "Close on exit" check box.

trowaAuthor Commented:
Thanks Richie, the codes look complicated! Anyway will try it later, thanks :)
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Richie_SimonettiIT OperationsCommented:
It appears so, but try and you will see.
trowaAuthor Commented:
I had tried with the example above, tried ExecCmd ("") just fine, but when i tried ExecCmd ("C:\jdk1.3.1_02\bin\javac.exe -classpath C:\jakarta-tomcat-4.0.3\common\lib\servlet.jar C:\jakarta-tomcat-4.0.3\webapps\test\WEB-INF\classes\") , the problem hang! So, what is the possibility to cause this problem?

Richie_SimonettiIT OperationsCommented:
try to debug with F8 key and post where did you get the hang.
trowaAuthor Commented:
Hi Richie,

Me again, i had debug the code using F8, i found that the Process is created by using

ret& = CreateProcessA(0&, cmdline$, sa, sa, 1&, _
      NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)

But the process close itself after the process is running regardless whether the java compiled successfully or not.

So this cause the program hang on this line

      bSuccess = ReadFile(hReadPipe, mybuff, 100, bytesread, 0&)

because i think the process is closed, so cannot read the file? Right?

You also mentioned about the "dosprompt" located in c:\windows, but what if i'm using Windows 2000 Advanced Server? I cannot find any files that similar to "dosprompt". Can you help me how can i resolve this?

Points will be added if you do solve my problem, i'm frustrated now, thanks for your help.
points lowered as per user request, problem was not solved tho comments where helpful

JGould-EE Moderator
Richie_SimonettiIT OperationsCommented:
i missed something?
Richie_SimonettiIT OperationsCommented:
I didn't see your last comment trowa, could create a pif file for javac.exe?
If so, it is easily to change "Close on exit" property.
I cannot test it in Advanced server since i don't use it, sorry.
trowaAuthor Commented:
Nvm Richie Simonetti. This is for research only. Anyway, what important is prompt a message to the user to let him know whether the java file is compiled successfully or vice versa. So, i got to compare the file created date of the compiled class file, to see whether this is the latest one or not (if the compilation is not succeed, the create file of class file will not be updated). So, finally i solve this problem in another way, but will definitely don't know what is the dos errors returned when compile error occured.

Anyway, thanks all for help.

Think twice, so i think the accepted comments worth at least 50pts bcos i also learn something new.

Sorry to bring so many problem to all :-) thank all very much.
Richie_SimonettiIT OperationsCommented:
Don't worry, thanks for "A" grade.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.