Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

help outputting to console

Posted on 2007-10-09
15
Medium Priority
?
360 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
0
Comment
Question by:flynny
  • 5
  • 4
  • 3
  • +1
15 Comments
 
LVL 4

Expert Comment

by:jimstar
ID: 20040286
Try using System.Console.WriteLine(strMsg);
0
 
LVL 4

Expert Comment

by:jimstar
ID: 20040292
Sorry, got confused between two of my IE windows. This isn't the .NET section. Ignore my post. :)
0
 
LVL 4

Expert Comment

by:jimstar
ID: 20040339
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

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:flynny
ID: 20040535
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
0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 20040737
0
 

Author Comment

by:flynny
ID: 20040761
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?
0
 
LVL 4

Expert Comment

by:jimstar
ID: 20040791
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.
0
 

Author Comment

by:flynny
ID: 20041013
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?
0
 
LVL 4

Expert Comment

by:jimstar
ID: 20041228
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
0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 20041648
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.

0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 20041922
The article here outlines how to do it:
http://www.charteris.com/Publications/SendFile.asp?Type=WhitePapers&Filename=UsingTheFileSystemObjectsInVisualBasic.pdf

And here is the process I went through to get it to work:
I created a new project and added a reference to the Microsoft Scripting Runtime.  Then I added a Module with the following code:

    Sub Main()
        Dim fso As New FileSystemObject
        fso.GetStandardStream(StdOut).Write ("Hello World")
    End Sub

From the Project Properties, I set the startup object to be Sub Main and then removed Form1 from the project.  I compiled the application as VBConsole.exe in the following folder:

    C:\Program Files\Microsoft Visual Studio\VC98\Bin

Then I copied this file into the Bin folder as well:

    C:\Program Files\Microsoft Visual Studio\VB98\MSPDB60.DLL

Next I used the EditBin utility as described in the PDF document and ran VBConsole.  "Hello World" did echo back successfully to the DOS console from which my app was run.  Here is the text from my Console window showing EditBin being used and then VBConsole.Exe being run:

C:\PROGRA~1\MICROS~6\VC98\BIN>editbin /subsystem:console vbconsole.exe
Microsoft (R) COFF Binary File Editor Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.



C:\PROGRA~1\MICROS~6\VC98\BIN>vbconsole
Hello World
C:\PROGRA~1\MICROS~6\VC98\BIN>
0
 

Author Comment

by:flynny
ID: 20046997
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).
 
0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 20047905
to call edit bin everytime before i run the program?

no

onely evry time you compile the app. (your exe file)
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 20048513
Exactly...just everytime you change your code and recompile.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 20048722
Right...only when you change your code and recompile.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question