Re: universal command line parser
- From: "Tony Proctor" <tony_proctor@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 23 Jul 2008 14:36:06 +0100
OK, here's a skeleton program with just one Module and one Form. You should
be able to extend that to do what you really need
============Form1===============
Option Explicit
Private Sub Form_Load()
' Hide the console window
ShowWindow GetConsoleWindow(), SW_HIDE
End Sub
Private Sub Form_Unload(Cancel As Integer)
' Restore the console window
ShowWindow GetConsoleWindow(), SW_SHOWNORMAL
End Sub
================================
============Module1==============
Option Explicit
Public bFormMode As Boolean 'Whether in forms mode of console mode
Private hConsoleOut As Long 'Handle for console output stream
Private hConsoleErr As Long 'Handle for console error stream
'
' WIN API support
'
Private Declare Function GetStdHandle Lib "kernel32" ( _
ByVal nStdHandle As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" ( _
ByVal hObject As Long) As Long
Private Declare Function WriteFile Lib "kernel32" ( _
ByVal hFile As Long, ByVal lpBuffer As String, ByVal
nNumberOfBytesToWrite As Long, _
lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
' NB: ExitProcess is technically not support for VB6 applications. See
Q288216
Private Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)
Public Declare Function GetConsoleWindow Lib "kernel32" () As Long
Public Declare Function ShowWindow Lib "user32" ( _
ByVal hWnd As Long, _
ByVal nCmdShow As Long) As Long
Public Const SW_SHOWNORMAL = 1
Public Const SW_HIDE = 0
Private Const INVALID_HANDLE_VALUE = -1
Private Const STD_OUTPUT_HANDLE = -11&
Private Const STD_ERROR_HANDLE = -12&
Private Sub CloseDown()
If (hConsoleOut <> INVALID_HANDLE_VALUE) Then CloseHandle hConsoleOut
If (hConsoleErr <> INVALID_HANDLE_VALUE) Then CloseHandle hConsoleErr
End Sub
Public Sub Output(sText As String, Optional bError As Boolean = False)
' Outputs a simple text line to the appropriate console stream. If in forms
mode then the lines are
' written to the txtOutput TextBox instead.
Dim hConsole As Long, lWritten As Long
If bFormMode Then
MsgBox sText, IIf(bError, vbExclamation, vbInformation)
Else
If bError Then
hConsole = hConsoleErr
Else
hConsole = hConsoleOut
End If
WriteFile hConsole, sText & vbCrLf, Len(sText) + 2, lWritten, 0&
End If
Debug.Print sText
End Sub
Public Sub ReportError()
' Report error in mode appropriate to whether using forms interface
Dim sMsg As String
Dim lErr As Long
lErr = Err.Number
sMsg = "Error " & Hex(lErr) & ": " & Err.Description
Output sMsg, True
If Not bFormMode Then
' If in console mode, make it fatal
CloseDown
If Not bRunningIDE() Then
' Make sure we don't accidentally terminate the IDE
ExitProcess lErr
End If
End If
End Sub
Private Function bTestIDE(ByRef bTest As Boolean) As Boolean
bTest = True
End Function
Public Function bRunningIDE() As Boolean
' Return TRUE if program is running in IDE, else FALSE
Debug.Assert Not bTestIDE(bRunningIDE)
End Function
Private Sub Main()
Dim sCmd As String
hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE)
hConsoleErr = GetStdHandle(STD_ERROR_HANDLE)
' Get any command-line specified
sCmd = Trim$(Command$())
' If command-line not present, assume forms interface, else assume
console-mode.
bFormMode = (sCmd = "")
If sCmd <> "" Then
' Process command-line args, including /help and /? options
Else
' NB: If we launch this form as modal then the ShowInTaskbar
property is ignored, and it doesn't
' look like a real application window. If we launch it as modeless
then we get the button
' but processing would continue headlong into the CloseDown call. We
therefore simulate a
' modal call with a modeless form1
Form1.Show vbModeless
Do While Forms.Count > 0
Sleep 100: DoEvents
Loop
End If
CloseDown
End Sub
================================
Once the EXE has been generated, you need to post-process it with the
following command in order top make it work properly when in console-mode
"%ProgramFiles%\Microsoft Visual Studio\VC98\Bin\editbin" /subsystem:CONSOLE
MyProgram.exe
Tony Proctor
"Peter" <peter_l@xxxxxxxxxxxxx> wrote in message
news:g679i6$uej$1@xxxxxxxxxxxxxxx
YESS!!! Please let me know how you did it.
Regards
Peter
"Tony Proctor" <tony_proctor@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx> schrieb im
Newsbeitrag news:eBZaBWK7IHA.3736@xxxxxxxxxxxxxxxxxxxxxxx
I have at least one app here with a "split personality". If invoked with a
command-line then it runs in console mode. Otherwise it runs in windowed
mode.
Interested?
Tony Proctor
.
- References:
- universal command line parser
- From: Peter
- Re: universal command line parser
- From: Tony Proctor
- Re: universal command line parser
- From: Karl E. Peterson
- Re: universal command line parser
- From: Karl E. Peterson
- Re: universal command line parser
- From: Karl E. Peterson
- Re: universal command line parser
- From: Bob O`Bob
- Re: universal command line parser
- From: Tony Proctor
- universal command line parser
- Prev by Date: Re: WebBrowser Control (VB5, VB6) & window.external. Possible?
- Next by Date: Re: controlling HTMLHelp display monitor
- Previous by thread: Re: universal command line parser
- Next by thread: Re: universal command line parser
- Index(es):