Writing to Console

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jan 2007
Posts: 200
Reputation: shouvik.d is an unknown quantity at this point 
Solved Threads: 6
shouvik.d's Avatar
shouvik.d shouvik.d is offline Offline
Posting Whiz in Training

Writing to Console

 
0
  #1
Dec 24th, 2008
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
  1. Option Explicit
  2. Declare Function AllocConsole Lib "kernel32" () As Long
  3. Declare Function FreeConsole Lib "kernel32" () As Long
  4. Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  5. Declare Function GetStdHandle Lib "kernel32" (ByVal nStdHandle As Long) As Long
  6. 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
  7. Declare Function SetConsoleCtrlHandler Lib "kernel32" (ByVal HandlerRoutine As Long, ByVal Add As Long) As Long
  8. Public Const STD_OUTPUT_HANDLE = -11&
  9. Public hndl As Long
  10.  
  11. Sub Main()
  12. Dim strWord As String
  13. Dim cWritten As Long
  14. Dim successStat As Boolean
  15.  
  16. On Error GoTo Hndlr
  17. strWord = "Hello!" & vbCrLf
  18. hndl = GetStdHandle(STD_OUTPUT_HANDLE)
  19. successStat = WriteConsole(hndl, ByVal strWord, Len(strWord), cWritten, ByVal 0&)
  20. Hndlr:
  21. Debug.Print Err.Description
  22. Debug.Print Err.Number
  23. Resume Next
  24.  
  25. 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.
Regards
Shouvik
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 200
Reputation: shouvik.d is an unknown quantity at this point 
Solved Threads: 6
shouvik.d's Avatar
shouvik.d shouvik.d is offline Offline
Posting Whiz in Training

Re: Writing to Console

 
0
  #2
Dec 24th, 2008
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

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

in the Sub Main()

Now it works fine both for Console mode as well as UI mode.
Last edited by shouvik.d; Dec 24th, 2008 at 9:21 am.
Regards
Shouvik
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC