How to get stdout/stderr from unmanaged DLL?
by VGhvbWFzIFcuIEJyb3du » Thu, 16 Jun 2005 06:02:02 GMT
I have setup Console redirection within my Console app (via Console.SetOut
and Console.SetErrror) to route console WriteLine calls to a logfile. This
works just fine with one exception...
I use PInvoke to call into several unmanaged DLLs which write diagnostic
information out to stdout (and possibly stderr). When I run in a
"standalone" mode with no redirection, these DLLs do generate output to the
console window. But I want to run in a "batch" mode where the app is
launched without a window. In this mode I still get all Console.WriteLine
and Console.Error.WriteLine output properly redirected, but lose all output
from the unmanaged DLLs.
Is there anything I can do to get output from an unmanaged, PInvoke'd DLL to
get redirected properly?
Thanks!
-- Tom
Re: How to get stdout/stderr from unmanaged DLL?
by William Stacey [MVP] » Thu, 16 Jun 2005 11:37:59 GMT
Hi Thomas. What do you mean by "batch" mode without a window? Calling your
c# console app from a batch file?
--
William Stacey [MVP]
Re: How to get stdout/stderr from unmanaged DLL?
by VGhvbWFzIFcuIEJyb3du » Fri, 17 Jun 2005 00:21:09 GMT
Not necessarily, I just mean in a non-interactive way. Actually what I'm
trying to do is the following. I have a WindowsService which responds to
specific IPC commands by starting a utility application. It is this utility
application for which I want to get stdout redirected to a log file.
Reassigning my own TextWriter via Console.SetOut and Console.SetError *does*
correctly route all Console.WriteLine and Console.Error.WriteLine type of
calls from my managed code despite the fact that the app is running without a
console window. But any output from PInvoked calls to these unmanaged DLLs
is lost. I ensure that a Console exists for my application (via
AllocConsole) but this doesn't have any effect.
I know I could redirect to a temp file on the process startup from my
service and then get all output after the fact, but then I would lose the
sequence of output between console output from the managed code and unmanaged
DLL output.
-- Tom
Similar Threads:
1.How to redirect stdout, stderr from a PInvoke'd DLL
As the subject indicates, I'm PInvoking unmanaged DLLs via my .NET
application. These DLLs (which I have to control over) output diagnostic
information to stdout which I would like to intercept. I can install my own
TextWriter derived class as the output and error writers for Console but only
managed Console writes get sent to my writer, the PInvoked DLL writes to
stdout are still sent somewhere into the bit bucket.
Can anyone tell me what I may be missing??
Thanks,
-- Tom
2.fputs to stderr causes exception when called from managed/unmanaged mixed mode DLL
Hi,
We have a large code base of "regular" C++ code (not MFC, COM or ATL).
With VC6.0, we used to compile the "regular" C++ code as static
libraries (.lib files), and then we would link the .lib files with an
MFC GUI "front end".
The "regular" C++ can also run as a standalone .EXE (with no GUI).
The "regular" C++ can be built and runs correctly as a standalone .EXE
with VC7.1 too.
We are now contemplating upgrading the GUI "front end" to C#. This
entails adding a Managed C++ "wrapper" DLL, which gets called frmo C#,
and in turn calls the Unmanaged C++.
The unmanaged C++ uses static variables, and so we have followed the
instructions in the MSDN article on "Converting Managed Extensions for
C++ Projects from Pure Intermediate Language to Mixed Mode". The
initialisation of static variables seems to be working correctly
(verified with breakpoints).
We have got to the point where the C# calls the Managed C++ Wrapper,
which calls Unmanaged C++, but we are getting a
System.NullReferenceException.
The exception is happening when a call is made to fputs, to output a
string to the sdterr, when function _lock_file calls
EnterCriticalSection.
Here is the stack trace:
NTDLL.DLL!_RtlEnterCriticalSection@4() + 0x11e87
msvcr71.dll!_lock_file(void * pf=0x1009d0d4) Line 236 C
msvcr71.dll!fputs(const char * string=0x1009df74, _iobuf *
stream=0x1009d0d4) Line 53 + 0x9 C
AppsEMSWrapper_Take1.dll!RTDiagStream::write(const char *
string=0x1009df74) Line 120 + 0x12 C++
AppsEMSWrapper_Take1.dll!showBanner() Line 189 + 0x13 C++
AppsEMSWrapper_Take1.dll!RTMain::mainLine() Line 221 C++
AppsEMSWrapper_Take1.dll!RTMain::entryPoint(int argc=0x00000006, const
char * const * argv=0x0012f624) Line 67 C++
AppsEMSWrapper_Take1.dll!ModelInterface::modelMainEntryPoint(int
RunAsLCT=0x00000001) Line 130 + 0xb C++
appsemswrapper_take1.dll!AppsEMSWrapper_Take1.ModelInterfaceWrapper.modelMainEntryPointWrapper(__int32
RunAsLCT = 0x1) Line 17 C++
ConsoleApplication1.exe!ConsoleApplication1.Class1.Main(string[] args
= {Length=0x0}) Line 33 C#
Any ideas, anyone ?????
Thanks !!!
3.child process with redirected stdout and stderr
I need to create a child process and read the child's stdout and stderr in
the parent process. This seems to be surprisingly difficult to do in .NET.
The documentation for the System.Diagnostics.Process class talks about
creating separate threads for reading each stream.
Is there an easier way? Has anyone already written a wrapper?
Thanks.
4.Capturing spawned child process's STDOUT & STDERR
Hello,
I'm using _execvpe (Win32 console project in VS.NET)
to spawn a child process. I would like to know if you
have any pointers on how to capture the child's STDOUT
and STDERR? Any help is greatly appreciated.
Thanks,
Sri