Hi All,

I have a VB6 application which can work in UI as well as in CMD mode. Now when I am running it in CMD mode I need to write few line into the console after completion of the program before quitting the application. The rest of the application is working perfectly fine but I am not able to write something into the console

I tried the following but it does not work

Option Explicit
Declare Function AllocConsole Lib "kernel32" () As Long
Declare Function FreeConsole Lib "kernel32" () As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Declare Function GetStdHandle Lib "kernel32" (ByVal nStdHandle As Long) As Long
Declare Function WriteConsole Lib "kernel32" Alias "WriteConsoleA" (ByVal hConsoleOutput As Long, lpBuffer As Any, ByVal nNumberOfCharsToWrite As Long, lpNumberOfCharsWritten As Long, lpReserved As Any) As Long
Declare Function SetConsoleCtrlHandler Lib "kernel32" (ByVal HandlerRoutine As Long, ByVal Add As Long) As Long
Public Const STD_OUTPUT_HANDLE = -11&
Public hndl As Long

Sub Main()
   Dim strWord As String
   Dim cWritten As Long
   Dim successStat As Boolean
   
   On Error GoTo Hndlr
   strWord = "Hello!" & vbCrLf
    hndl = GetStdHandle(STD_OUTPUT_HANDLE)
    successStat = WriteConsole(hndl, ByVal strWord, Len(strWord), cWritten, ByVal 0&)
Hndlr:
   Debug.Print Err.Description
   Debug.Print Err.Number
   Resume Next
   
End Sub

Kindly help. I need to write the output in the same console window from where the application is invoked, not in a new window that the application would open.

Hi All,

I could do the required by doing the following "<drive>:\Program Files\Microsoft Visual Studio\vb98\LINK.EXE" /EDIT /SUBSYSTEM:CONSOLE <my_app_name.exe>" .

But then whenever we open the VB Application in UI mode it shows a CMD window at the background along with the Form. So to hide it I had to do the following as well

ConsoleHandle = FindWindow("ConsoleWindowClass", App.Path & "\<my_app_name.exe>")
ShowWindow ConsoleHandle, SW_HIDE

in the Sub Main()

Now it works fine both for Console mode as well as UI mode.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.