943,884 Members | Top Members by Rank

Ad:
Dec 24th, 2008
0

Writing to Console

Expand Post »
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
VB Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 64
Solved Threads: 7
Junior Poster
shouvik.d is offline Offline
198 posts
since Jan 2007
Dec 24th, 2008
0

Re: Writing to Console

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

VB Syntax (Toggle Plain Text)
  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.
Reputation Points: 64
Solved Threads: 7
Junior Poster
shouvik.d is offline Offline
198 posts
since Jan 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Adding Time in VB 6.0
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Reg.DATABASE and Refresh





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC