Question Redirect stdout from unmanaged dll

chrismanster

New member
Joined
Aug 27, 2008
Messages
1
Programming Experience
3-5
I have an application that I would like to redirect the stdout stream of a C dll to a file for debugging purposes. I found the following code but it is not working:

VB.NET:
Declare Ansi Function SetStdHandle Lib "Kernel32.dll" ( _
        ByVal device As Integer, _
        ByVal handle As IntPtr) As Integer

Declare Ansi Function GetStdHandle Lib "Kernel32.dll" ( _
        ByVal nStdHandle As Integer) As IntPtr

Private Const STD_INPUT_HANDLE As Integer = -10
Private Const STD_OUTPUT_HANDLE As Integer = -11
Private Const STD_ERROR_HANDLE As Integer = -12
Private OldStdOutHandle As IntPtr
Private OldStdOut As TextWriter

Dim status As Integer
Dim handle As IntPtr
Dim fs As IO.FileStream
Dim sw As IO.StreamWriter

OldStdOutHandle = GetStdHandle(STD_OUTPUT_HANDLE)
OldStdOut = Console.Out

fs = New IO.FileStream("c:\test.log", IO.FileMode.Create)

sw = New IO.StreamWriter(fs)
sw.AutoFlush = True
Console.SetOut(sw)

handle = fs.SafeFileHandle.DangerousGetHandle()
status = SetStdHandle(STD_OUTPUT_HANDLE, handle)

'call to dll goes here
 
Back
Top