Why do I get error 6 (The Handle is Invalid) trying to WriteFile to STDOUT from VB 6 exe?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hello,

- I create the following VB 6 app to write to STDOUT (the console).
- I build it into an exe.
- I run the exe from a command line.
- I get no output and my code detects a WriteFile failure with an underlying
result code of 6, Invalid handle.

The handle I get back from GetStdHandle(STD_OUTPUT_HANDLE) is 7 which I
believe is valid.

I'm assuming somehow my vb app can't see the console it is being run from.
I'd expect this failure running from the IDE but not from the command line.
What is the story? I checked the web and this seems like the right way to
write to the console. What am I missing???

-------------------------------------------------------
Public Const STD_OUTPUT_HANDLE = -11&
Declare Function GetStdHandle Lib "kernel32" (ByVal nStdHandle As Long) As
Long
Private Declare Function WriteFile Lib "kernel32" _
(ByVal hFile As Long, _
ByVal lpBuffer As Any, _
ByVal nNumberOfBytesToWrite As Long, _
lpNumberOfBytesWritten As Long, _
ByVal lpOverlapped As Any) As Long

Sub Out(ByVal strOut As String)
' Sends data to StdOut stream
Dim lHandle As Long
lHandle = GetStdHandle(STD_OUTPUT_HANDLE)

Dim lReturn As Long
Dim lResult As Long
lResult = WriteFile(lHandle, (strOut), Len(strOut), lReturn, ByVal 0&)

If Not lResult Then
MsgBox "HANDLE: " + Str(lHandle) + ", LastDLLError: " +
Str(Err.LastDllError)
End If
End Sub

Public Sub Main()
Out "This is a test"
End Sub

-------------------------------------------------------

Thanks!
--
Grant Schenck




.



Relevant Pages

  • Re: [opensuse] saving boot-time console output
    ... it redirects stdout and stderr to /dev/console. ... # Set I/O of this script and its childs to console ... Duplicating File Descriptors ... can use the command "tee" command. ...
    (SuSE)
  • Re: wait for RAPI.CreateProcess() to complete before continue
    ... Then from the desktop I copy my little device side console app to the device ... The desktop app attempts to delete the exe on the device a few ... a "launch and wait" type command. ...
    (microsoft.public.pocketpc.developer)
  • open CONSOLE?
    ... My process is running in an environment in which STDOUT and STDERR have ... command line tools, then restore them afterwards. ... STDOUT so that it goes to the "console", as if it had never been redirected? ...
    (perl.beginners)
  • Windows forms from CommandLine exe
    ... I've a command line exe that, if invoked without any parameter, show a ... Windows Form interface. ... the console application remain active. ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: How to make a starpack write to standard channels on windows?
    ... I'm not succeeding in making a windows starpack write to stdout and stderr. ... outputs to the command window. ... The gui versions include the windows console and these write to ...
    (comp.lang.tcl)