Here's my code.
What it should do is display the text "This is a test." in the console window, when you click the button Command1. But it's not working. By printing out the variable CharCount (the return value for number of characters written to the console), you can see that it indeed THINKS that it put that text into the console, but if you look at the console you'll see NO TEXT. And if you look at the printed value after CharCount, which is ErrNum (the error number returned from Err.LastDllError, which is the same as the API call GetLastError) it shows 87 (or 0x57 in hexadecimal). I don't know what error 87 means.
Code:
Private Declare Function AllocConsole Lib "kernel32.dll" () As Long
Private Declare Function FreeConsole Lib "kernel32.dll" () As Long
Private Declare Function GetStdHandle Lib "kernel32.dll" (ByVal nStdHandle As Long) As Long
Private Declare Function WriteConsoleOutputCharacter Lib "kernel32.dll" Alias "WriteConsoleOutputCharacterA" (ByVal hConsoleOutput As Long, ByVal lpCharacter As String, ByVal nLength As Long, ByRef dwWriteCoord As COORD, ByRef lpNumberOfCharsWritten As Long) As Long
Private Const STD_OUTPUT_HANDLE As Long = -11&
Private Type COORD
x As Integer
y As Integer
End Type
Dim hStdOut As Long
Private Sub Form_Load()
AllocConsole
hStdOut = GetStdHandle(STD_OUTPUT_HANDLE)
End Sub
Private Sub Form_Unload(Cancel As Integer)
FreeConsole
End Sub
Private Sub Command1_Click()
Dim Text As String
Dim Pos As COORD
Dim CharCount As Long
Dim ErrNum As Long
Text = "This is a test."
With Pos
.x = 0
.y = 0
End With
WriteConsoleOutputCharacter hStdOut, Text, Len(Text), Pos, CharCount
ErrNum = Err.LastDllError
Print CharCount
Print ErrNum
End Sub
What it should do is display the text "This is a test." in the console window, when you click the button Command1. But it's not working. By printing out the variable CharCount (the return value for number of characters written to the console), you can see that it indeed THINKS that it put that text into the console, but if you look at the console you'll see NO TEXT. And if you look at the printed value after CharCount, which is ErrNum (the error number returned from Err.LastDllError, which is the same as the API call GetLastError) it shows 87 (or 0x57 in hexadecimal). I don't know what error 87 means.