We help IT Professionals succeed at work.

help outputting to console

flynny
flynny asked
on
384 Views
Last Modified: 2010-04-30
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_HANDLE)
   
    nRet = WriteConsole(hStdout, Buffer, Len(Buffer), cBytes, 0&)

    WriteMessage = nRet

End Function
Comment
Watch Question

Commented:
Try using System.Console.WriteLine(strMsg);

Commented:
Sorry, got confused between two of my IE windows. This isn't the .NET section. Ignore my post. :)

Commented:
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_HANDLE)
ConsolePrint "This is a test"


Private Sub ConsolePrint(szOut As String)
WriteConsole hConsoleOut, szOut, Len(szOut), vbNull, vbNull
End Sub

Author

Commented:
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_HANDLE)
   
    WriteConsole hConsoleOut, strMsg, Len(strMsg), vbNull, vbNull

    WriteMessage = nRet

End Function
David RobitailleAnalyst Programmer

Commented:

Author

Commented:
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?

Commented:
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.

Author

Commented:
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?

Commented:
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
David RobitailleAnalyst Programmer

Commented:
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.

High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
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).
 
David RobitailleAnalyst Programmer

Commented:
to call edit bin everytime before i run the program?

no

onely evry time you compile the app. (your exe file)
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
Exactly...just everytime you change your code and recompile.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
Right...only when you change your code and recompile.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.