HOWTO: Attach a Console Window to Your eMbedded Visual Basic Program
The information in this article applies to:
- Microsoft eMbedded Visual Basic Version 3.0
- SymbolicTools PocketConsole
SUMMARY
This article demonstrates attaching a PocketConsole console window
to your eMbedded Visual Basic application, writing to it, and running another
application in the console window.
MORE INFORMATION
This article does demonstrate creating a console window that your
application can interact with. It also demonstrates running a console
application (batch file, in this case) from Visual Basic, which utilizes the
created console.
WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS
ARTICLE IS AT YOUR OWN RISK. SymbolicTools provides this code "as is" without
warranty of any kind, either express or implied, including but not limited to
the implied warranties of merchantability and/or fitness for a particular
purpose.
Step-by-Step Example
- Download/install
PocketConsole, the console device driver.
- Download/install a command prompt, either CMD from Microsoft,
or the Open Source product PocketCMD from ReactOS/SymbolicTools.
- Use Notepad to create the following batch file:
DIR /W
and save it as \TEST.BAT on your Pocket PC
- In eMbedded Visual Basic, create a new project with a form and a module.
- Type the following API declarations in the module:
Option Explicit
Declare Function AllocConsole Lib "port" () As Long
Declare Function FreeConsole Lib "port" () As Long
Declare Function Shell Lib "port" Alias "_wsystem" (ByVal cmd As String) As Long
Declare Function CloseHandle Lib "coredll" (ByVal hObject As Long) _
As Long
Declare Function GetStdHandle Lib "port" (ByVal _
nStdHandle As Long) As Long
Declare Function WriteConsole Lib "port" Alias "WriteConsoleW" _
(ByVal hConsoleOutput As Long, ByVal lpBuffer As String, ByVal _
nNumberOfCharsToWrite As Long, lpNumberOfCharsWritten As Long, _
lpReserved As Long) As Long
Public Const STD_OUTPUT_HANDLE = -11
- Add a CommandButton to the form and enter the following code:
Dim hConsole As Long
Private Sub Command1_Click()
Dim Result As Long, sOut As String, cWritten As Long
sOut = "Hi There" & vbCrLf
Result = WriteConsole(hConsole, sOut, Len(sOut), cWritten, 0)
Shell "test.bat"
End Sub
Private Sub Form_Load()
If AllocConsole() Then
hConsole = GetStdHandle(STD_OUTPUT_HANDLE)
If hConsole = 0 Then MsgBox "Couldn't allocate STDOUT"
Else
MsgBox "Couldn't allocate console"
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
CloseHandle hConsole
FreeConsole
End Sub
- Run the application. A form with a CommandButton will appear
- Click the CommandButton. Than go to the task manager and select "command
prompt". Both the text in sOut and the output from the batch file will
appear in the console.
- Close the form. The console window will terminate.