How to get stdout/stderr from unmanaged DLL?



  • 1. enumerating and using fonts installed by AddFontResourceEx
    1. I've installed a bunch of fonts in main application, which is in native C++ code using AddFontResourceEx 2. A C# application is launched from this main application, but failed to enumerate those installed fonts. My issue is that I don't want those fonts to be used outside of the main application so that I've set the font characteristics as FR_PRIVATE. However, I want the C# application be able to use those fonts. Although the C# app is from Main application, probably it is in another process so that it is not able to enumerate the fonts. My question is may I view and use those fonts in C# application? And how? Thanks in advance,
  • 2. Changing border color?
    Is there a simple way to change a UserControl border color? I'm creating a UserControl that will have a border, using the UserControl.BorderStyle property. The standard border is black; I'd like to change the color to something a little more appealing. I've tried overriding OnPaint, like this: public partial class MyControl : UserControl { public MyControl() { this.SetStyle(ControlStyles.UserPaint, true); InitializeComponent(); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if (this.BorderStyle == BorderStyle.FixedSingle) { int borderWidth = 1; Color borderColor = SystemColors.ControlDark; ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, borderColor, borderWidth, ButtonBorderStyle.Solid, borderColor, borderWidth, ButtonBorderStyle.Solid, borderColor, borderWidth, ButtonBorderStyle.Solid, borderColor, borderWidth, ButtonBorderStyle.Solid); } } } That doesn't work, because it leaves the black border. Plus, UserPaint setting apparently drops all control painting in my lap, when all I want to do is change the border color. Is there a P/Invoke or a Windows message I can use to change the border color? Thanks. -- David Veeneman Foresight Systems
  • 3. StackTrace constructor on other threads
    There is a StackTrace constructor that can take a target thread. This requires that the target thread be Suspended. However, the Suspend() method of Thread is now deprecated. Is there an alternative? -- Adam Clauss
  • 4. [vb->c#] Some conversion problems about handles and Master Page
    same question about WEB app.: In vb i use "handles + handler class name" keyword for events, what can i use in c#? In vb i use the directive masterpagefile and mastertype and than i can see all public methods, classes and vars of the master page, in c# it seems it is not enaugh, how can i do to """see""" masterPage?

How to get stdout/stderr from unmanaged DLL?

Postby 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?

-- Tom

Re: How to get stdout/stderr from unmanaged DLL?

Postby 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?

Postby 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??

-- Tom

2.fputs to stderr causes exception when called from managed/unmanaged mixed mode DLL


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

The exception is happening when a call is made to fputs, to output a
string to the sdterr, when function _lock_file calls

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++
RunAsLCT=0x00000001)  Line 130 + 0xb	C++
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?


4.Capturing spawned child process's STDOUT & STDERR

   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.


Return to C#


Who is online

Users browsing this forum: No registered users and 7 guest