flynny
asked on
help outputting to console
can anyone tell me why this method is not working. its running through it however i'm seeing no message been sent to the console. i basically use this method to output all my errors to the console window.
Public Const STD_OUTPUT_HANDLE = -11&
Public Function WriteMessage(strMsg As String) As Long
Dim Buffer As String
Dim hStdout As Long
Dim cBytes As Long
Dim nRet As Long
Dim mybyte1 As Byte
Buffer = strMsg
hStdout = GetStdHandle(STD_OUTPUT_HA NDLE)
nRet = WriteConsole(hStdout, Buffer, Len(Buffer), cBytes, 0&)
WriteMessage = nRet
End Function
Public Const STD_OUTPUT_HANDLE = -11&
Public Function WriteMessage(strMsg As String) As Long
Dim Buffer As String
Dim hStdout As Long
Dim cBytes As Long
Dim nRet As Long
Dim mybyte1 As Byte
Buffer = strMsg
hStdout = GetStdHandle(STD_OUTPUT_HA
nRet = WriteConsole(hStdout, Buffer, Len(Buffer), cBytes, 0&)
WriteMessage = nRet
End Function
Try using System.Console.WriteLine(s trMsg);
Sorry, got confused between two of my IE windows. This isn't the .NET section. Ignore my post. :)
Ok, here's the non-.NET version. :) I assume you're using WriteConsole exported by kernel32? This should work...
====== In the Global Declarations==========
Private Declare Function WriteConsole Lib "kernel32" Alias _
"WriteConsoleA" (ByVal hConsoleOutput As Long, _
ByVal lpBuffer As Any, ByVal nNumberOfCharsToWrite As Long, _
lpNumberOfCharsWritten As Long, lpReserved As Any) As Long
Private Declare Function GetStdHandle Lib "kernel32" _
(ByVal nStdHandle As Long) As Long
Private Const STD_OUTPUT_HANDLE = -11&
Private hConsoleOut As Long 'The console's output handle
==== within Sub Main() ====
hConsoleOut = GetStdHandle(STD_OUTPUT_HA NDLE)
ConsolePrint "This is a test"
Private Sub ConsolePrint(szOut As String)
WriteConsole hConsoleOut, szOut, Len(szOut), vbNull, vbNull
End Sub
====== In the Global Declarations==========
Private Declare Function WriteConsole Lib "kernel32" Alias _
"WriteConsoleA" (ByVal hConsoleOutput As Long, _
ByVal lpBuffer As Any, ByVal nNumberOfCharsToWrite As Long, _
lpNumberOfCharsWritten As Long, lpReserved As Any) As Long
Private Declare Function GetStdHandle Lib "kernel32" _
(ByVal nStdHandle As Long) As Long
Private Const STD_OUTPUT_HANDLE = -11&
Private hConsoleOut As Long 'The console's output handle
==== within Sub Main() ====
hConsoleOut = GetStdHandle(STD_OUTPUT_HA
ConsolePrint "This is a test"
Private Sub ConsolePrint(szOut As String)
WriteConsole hConsoleOut, szOut, Len(szOut), vbNull, vbNull
End Sub
ASKER
nope that didn't work. this is what i tried.
sorry should have mentioned yes i have exported the twofunctions you mentioned
Private Const STD_OUTPUT_HANDLE = -11&
Private hConsole As Long
Private Declare Function WriteConsole Lib "kernel32" Alias "WriteConsoleA" (ByVal hConsoleOutput As Long, ByVal lpBuffer As Any, ByVal nNumberOfCharsToWrite As Long, lpNumberOfCharsWritten As Long, lpReserved As Any) As Long
Public Declare Function GetStdHandle Lib "kernel32" (ByVal nStdHandle As Long) As Long
Public Function WriteMessage(strMsg As String) As Long
Dim Buffer As String
Buffer = strMsg
hConsoleOut = GetStdHandle(STD_OUTPUT_HA NDLE)
WriteConsole hConsoleOut, strMsg, Len(strMsg), vbNull, vbNull
WriteMessage = nRet
End Function
sorry should have mentioned yes i have exported the twofunctions you mentioned
Private Const STD_OUTPUT_HANDLE = -11&
Private hConsole As Long
Private Declare Function WriteConsole Lib "kernel32" Alias "WriteConsoleA" (ByVal hConsoleOutput As Long, ByVal lpBuffer As Any, ByVal nNumberOfCharsToWrite As Long, lpNumberOfCharsWritten As Long, lpReserved As Any) As Long
Public Declare Function GetStdHandle Lib "kernel32" (ByVal nStdHandle As Long) As Long
Public Function WriteMessage(strMsg As String) As Long
Dim Buffer As String
Buffer = strMsg
hConsoleOut = GetStdHandle(STD_OUTPUT_HA
WriteConsole hConsoleOut, strMsg, Len(strMsg), vbNull, vbNull
WriteMessage = nRet
End Function
I did that some time ago, it may help you.
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=65681&lngWId=1
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=65681&lngWId=1
ASKER
also to add i added in a msgbox just before the WriteConsole call and after the assignment of hConsoleOut. now this seems to be triggered around 10 times even though it should only run through once.
also i am only trying to write a set sentence i.e. no vairables are being used in the string
WriteMessage( "ERROR: error message")
any ideas?
also i am only trying to write a set sentence i.e. no vairables are being used in the string
WriteMessage( "ERROR: error message")
any ideas?
If the above code is being run 10 times, then I suspect the code that calls the WriteMessage function is being called 10 times as well. Hard to tell without seeing the rest of the code.
ASKER
sorry yep my bad, i am calling it multiple times as a part of my error handler. basically i output the error and as the error was to do with not enough command line params been enetered it prints off a list of valid commands for the program. if i comment out this then yes it only runs once. could there be a setting in my project not allowing this to be output to the console?
I'm really not sure what could be causing this. I'm able to get it working fine, although I am using a newer version of Visual Studio, so the behavior might be different.
The only thing that I can think of to try would be to change your function declarations to the following (replace long with int, and any with int):
Private Declare Function WriteConsole Lib "kernel32" Alias "WriteConsoleA" (ByVal hConsoleOutput As Integer, ByVal lpBuffer As String, ByVal nNumberOfCharsToWrite As Integer, ByVal lpNumberOfCharsWritten As Integer, ByVal lpReserved As Integer) As Integer
Public Declare Function GetStdHandle Lib "kernel32" (ByVal nStdHandle As Integer) As Long
The only thing that I can think of to try would be to change your function declarations to the following (replace long with int, and any with int):
Private Declare Function WriteConsole Lib "kernel32" Alias "WriteConsoleA" (ByVal hConsoleOutput As Integer, ByVal lpBuffer As String, ByVal nNumberOfCharsToWrite As Integer, ByVal lpNumberOfCharsWritten As Integer, ByVal lpReserved As Integer) As Integer
Public Declare Function GetStdHandle Lib "kernel32" (ByVal nStdHandle As Integer) As Long
Hey, i think i got it. you need to relink your exe.
I had a similar problem in vb6. this link is not the one that i find at the time, but it got the info you may need to "Re-link" the project to get access to the console.
http://www.xaprb.com/blog/2005/10/14/how-to-create-a-vb6-console-program/
here the abstract
Re-link the program for the Windows Console subsystem
There seems to be no option in the VB project properties or compile options to do this automatically when making the program, so you will need to re-link after compilation. If you dont do this, your program will not run correctly. The standard streams will not be available, for one thing. Fortunately, it is quite easy to do:
"C:\Program Files\Microsoft Visual Studio\vb98\LINK.EXE" /EDIT /SUBSYSTEM:CONSOLE <yourfile.exe> (this code should all be on one line).
A handy shortcut is to create a batch file with the command in it. You can then drag your .EXE file onto the batch file. Assuming LINK.EXE is in your path, the following will work:
LINK.EXE /EDIT /SUBSYSTEM:CONSOLE %1
Dont name the batch file link.bat or it will call itself! Another of Microsofts insecure default behaviors.
I had a similar problem in vb6. this link is not the one that i find at the time, but it got the info you may need to "Re-link" the project to get access to the console.
http://www.xaprb.com/blog/2005/10/14/how-to-create-a-vb6-console-program/
here the abstract
Re-link the program for the Windows Console subsystem
There seems to be no option in the VB project properties or compile options to do this automatically when making the program, so you will need to re-link after compilation. If you dont do this, your program will not run correctly. The standard streams will not be available, for one thing. Fortunately, it is quite easy to do:
"C:\Program Files\Microsoft Visual Studio\vb98\LINK.EXE" /EDIT /SUBSYSTEM:CONSOLE <yourfile.exe> (this code should all be on one line).
A handy shortcut is to create a batch file with the command in it. You can then drag your .EXE file onto the batch file. Assuming LINK.EXE is in your path, the following will work:
LINK.EXE /EDIT /SUBSYSTEM:CONSOLE %1
Dont name the batch file link.bat or it will call itself! Another of Microsofts insecure default behaviors.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
After trying all the suggested solutions Idle_minds worked. I have a question about this though please. I order for this to work will i have to call edit bin everytime before i run the program? or is this a one off event? (this may have been in the PDF link, however its not working for me i'm redirected to the home page).
to call edit bin everytime before i run the program?
no
onely evry time you compile the app. (your exe file)
no
onely evry time you compile the app. (your exe file)
Exactly...just everytime you change your code and recompile.
Right...only when you change your code and recompile.