Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6629503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:16:14+00:00 2026-05-25T22:16:14+00:00

Please bear with me – I’m a c# developer with little experience with C++,

  • 0

Please bear with me – I’m a c# developer with little experience with C++, and this is a steep learning curve!

From a c# console app, I’m calling some methods from an unmanaged C++ dll. The DLL writes to the stdout stream, although this was not being picked up by the c# console.

I found the following code, which I added to the C++ dll, which now successfully sends the contents of “printf” to the c# console.

#include <windows.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>

void redirect_stdout()
{
int hConHandle;
long lStdHandle;
FILE *fp;
// allocate a console for this app
AllocConsole();
// redirect unbuffered STDOUT to the console
lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "w" );
*stdout = *fp;
setvbuf( stdout, NULL, _IONBF, 0 );
}

AOK so far:

What I’d like to do is capture the stdoutfrom the DLL to a c# stream, rather than send it to the console. I tried the method detailed here (Redirect stdout+stderr on a C# Windows service), which does capture the output, however the app “crashes” when the program closes (“vshost.exe has stopped working”).

(Note: setting Console.SetOut() on the stream captures the c# output, not the c++ output).

So I thought what if I use the “Filestream.SafeFileHandle.DangerousGetHandle()” method to get a handle to the filestream from c#, and pass this into the C++ method redirect_stdout() method:

void redirect_stdout(FILE *passedInHandle)
{

    // allocate a console for this app
    AllocConsole();
    *stdout= *passedInHandle;
    setvbuf( stdout, NULL, _IONBF, 0 );
}

When I run the above version, the output from the DLL is no longer piped to the c# Console, however, the filestream on the c# side is always empty.

Can any expert give guidance to have the STDOUT write its output to the c# filestream? I’m sure I’ve made some stupid error about how to achieve this or I am not understanding how to achieve what I am trying to do.

Thank you for your time and input – really appreciated!

[EDIT]

OK – I’ve played a bit more and modified the C++ method as such:

void redirect_stdout(int passedInHandle)
{
    int hConHandle;
    long lStdHandle;
    FILE *fp;
    // allocate a console for this app
    AllocConsole();

    hConHandle = _open_osfhandle(passedInHandle, _O_TEXT);
    fp = _fdopen(hConHandle, "w");
    *stdout = *fp;
    setvbuf( stdout, NULL, _IONBF, 0 );
}

This also successfully populates the c# stream, however when the c# Console app closes, the app crashes with the error “vshost.exe has stopped working”. This is the same error as when I use the method from Redirect stdout+stderr on a C# Windows service

Very odd find: If I run the console app “outside of visual studio” (eg, double click on the .exe in the bin folder), there is no crash!

So I guess my next question is: how do I track down the source of this crash? Is it VS related? It occurs in either debug or release mode when running from VS, and no crash when run outside of VS.

I am at a loss as to how to debug this one!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:16:14+00:00Added an answer on May 25, 2026 at 10:16 pm

    You might consider using named pipes: i.e

    communication between c++ and c# through pipe

    http://www.switchonthecode.com/tutorials/interprocess-communication-using-named-pipes-in-csharp

    Hopefully, that should work…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please bear with me as I am a cakephp noob. I have this app
Please bear with me, I'm just learning C++. I'm trying to write my header
Please bear with me if this question isn't well formulated. Not knowing is part
Please bear with me, newbie just learning the ropes. I am getting the below
I am a beginner in Haskell so please bear with me. (Just started learning
I am new to bash so please bear with me if this is a
Please bear with me as I've been thrown into the middle of this project
I'm javascript newbie...please bear with me. Modified some jQuery animation code from w3c tutorial.
I'm kinda noob to android so please bear with me. I'm currently developing app
This is kinda confusing so please bear with me. I have an array (listOfStates)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.