Dragon,
As stand__sure suggests, this works on my dev box also; that is if I run it
without the debugger (VS.NET = "Debug - Start Without Debugging").
If I attempt to run it inside the debugger it fails! Failing inside the
debugger is curious in that I would not expect it to actually fail, I would
expect different handle values (as actual handle values are opaque, in that
they represent a HANDLE to a resource & their actual value has no meaning).
Remember VB.NET redirects console output to the Debug window, it would
appear when it does this it causes GetStdHandle to behave oddly...
If you call Console.Write instead of the WriteConsole API in your sample,
the output goes to the Debugger's output window, as evidence of the above
redirection.
Unfortunately I have not used consoles enough to offer any suggestions on
how to "work around" the above behavior... Reading the Remarks of
GetStdHandle offers a suggestion. Open CONIN$ or CONOUT$ to get a handle to
a "console's active screen buffer":
http://msdn.microsoft.com/library/de...tstdhandle.asp
Rather then Declare GetLastError, you need to call
System.Runtime.InteropServices.Marshal.GetLastWin3 2Error, for details see:
http://msdn.microsoft.com/library/de...errortopic.asp
Something like:
Imports System.Runtime.InteropServices
Private Declare Function AllocConsole Lib "kernel32.dll" () As Int32
Private Declare Function FreeConsole Lib "kernel32.dll" () As Int32
Private Declare Function GetStdHandle Lib "kernel32.dll" (ByVal
nStdHandle As Int32) As IntPtr
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject
As IntPtr) As Int32
Private Declare Function WriteConsole Lib "kernel32.dll" Alias
"WriteConsoleA" (ByVal hConsoleOutput As IntPtr, ByVal lpBuffer As IntPtr,
ByVal nNumberOfCharsToWrite As Int32, ByRef lpNumberOfCharsWritten As Int32,
ByVal lpReserved As IntPtr) As Int32
'Private Declare Function GetLastError Lib "kernel32.dll" () As Int32
Private Const STD_OUTPUT_HANDLE As Int32 = -11I
Public Shared Sub SomeButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
If AllocConsole() = 0 Then Debug.WriteLine("AllocConsole failed: " &
Marshal.GetLastWin32Error().ToString)
Dim hOutPut As IntPtr = GetStdHandle(STD_OUTPUT_HANDLE)
If hOutPut.ToInt32 = 0 Then Debug.WriteLine("GetStdHandle failed: "
& Marshal.GetLastWin32Error().ToString)
Debug.WriteLine("Standard output: " & hOutPut.ToString)
Dim str As String = "It's working!"
Dim written As Int32
Dim handle As IntPtr = Marshal.StringToHGlobalAnsi(str)
If WriteConsole(hOutPut, handle, str.Length, written, Intptr.Zero) =
0 Then Debug.WriteLine("WriteConsole failed: " &
Marshal.GetLastWin32Error())
If CloseHandle(hOutPut) = 0 Then Debug.WriteLine("CloseHandle
failed: " & Marshal.GetLastWin32Error())
Threading.Thread.Sleep(1000)
If FreeConsole() = 0 Then Debug.WriteLine("FreeConsole failed: " &
Marshal.GetLastWin32Error().ToString)
End Sub
Hope this helps
Jay
"Dragon" <no@spam.please> wrote in message news:d9**********@news.rol.ru...
|
| "stand__sure" <st*********@hotmail.com> ???????/???????? ? ????????
| ?????????: news:11**********************@g49g2000cwa.googlegr oups.com...
| > can you give us more code? how are you dereferencing the value?
| >
| Well, that's my code:
| ~
| Private Declare Function AllocConsole Lib "kernel32.dll" () As Int32
|
| Private Declare Function FreeConsole Lib "kernel32.dll" () As Int32
|
| Private Declare Function GetStdHandle Lib "kernel32.dll" (ByVal nStdHandle
| As Int32) As IntPtr
|
| Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As
| IntPtr) As Int32
|
| Private Declare Function WriteConsole Lib "kernel32.dll" Alias
| "WriteConsoleA" (ByVal hConsoleOutput As IntPtr, ByVal lpBuffer As IntPtr,
| ByVal nNumberOfCharsToWrite As Int32, ByRef lpNumberOfCharsWritten As
Int32,
| ByVal lpReserved As IntPtr) As Int32
|
| Private Declare Function GetLastError Lib "kernel32.dll" () As Int32
|
| Private Const STD_OUTPUT_HANDLE As Int32 = -11I
|
| Private Sub SomeButton_Click(ByVal sender As Object, ByVal e As
| System.EventArgs) Handles SomeButton.Click
|
| If AllocConsole() = 0 Then Debug.WriteLine("AllocConsole failed: " &
| GetLastError.ToString)
|
| Dim hOutPut As IntPtr = GetStdHandle(STD_OUTPUT_HANDLE)
|
| If hOutPut.ToInt32 = 0 Then Debug.WriteLine("GetStdHandle failed: " &
| GetLastError.ToString)
|
| Debug.WriteLine("Standard output: " & hOutPut.ToString)
|
| Dim str As String = "It's working!"
|
| Dim written As Int32
|
| Dim handle As IntPtr = Marshal.StringToHGlobalAnsi(str)
|
| If WriteConsole(hOutPut, handle, str.Length, written, New IntPtr(0)) = 0
| Then Debug.WriteLine("WriteConsole failed: " & GetLastError)
|
| If CloseHandle(hOutPut) = 0 Then Debug.WriteLine("CloseHandle failed: " &
| GetLastError)
|
| Threading.Thread.Sleep(1000)
|
| If FreeConsole() = 0 Then Debug.WriteLine("FreeConsole failed: " &
| GetLastError.ToString)
|
| End Sub
|
| ~
|
| When I click SomeButton for first time I get following output:
|
| Standard output: 1912 (or any other bad value)
|
| WriteConsole failed: 6
|
| If I keep clicking it prints:
|
| Standard output: 1912
|
| WriteConsole failed: 6
|
| CloseHandle failed: 6
|
| When I restart application number 1912 changes to another number and
| situation repeats.
|
|