none
Redirection of STDOUT and STDERR from a DLL...

    Question

  • OK, here is my situtation. I have a third party unmanged library that performs some functions for me. I do not have source for it so I cannot change it. I have wrapped this library up and made a managed DLL out of it. My problem is that I am developing a GUI application and many of the third party functions print messages to STDOUT and/or STDERR via std::cout and/or printf. I would like to be able to redirect these irksome messages to a RichTextBox I just don't have any idea of how to do it.

    I have read several articles on redirecting STDOUT from a spawned console application and such but that isn't what I have here. There is no console application running and the group that I am writing this for doesn't want one.

    So, is it possible to redirect the output from this library to a RichText box in Visual C++ 2005 and if so how? Any and all advice will be greatly appreciated.

     

    SoftwareGerbil

    Monday, November 06, 2006 8:26 PM

Answers

  •  SoftwareGerbil wrote:

     I have a third party unmanged library that performs some functions for me. I do not have source for it so I cannot change it. I have wrapped this library up and made a managed DLL out of it. My problem is that I am developing a GUI application and many of the third party functions print messages to STDOUT and/or STDERR via std::cout and/or printf. I would like to be able to redirect these irksome messages to a RichTextBox I just don't have any idea of how to do it.

      Try this :

      void CMFCTest2Dlg::OnBnClickedButton1()
      {
        ostrstream ostr;
        cout.rdbuf(ostr.rdbuf());
        cout<<"hello world";
        char* s = ostr.str(); 
        MessageBox(s);
      }

     This may need some refinement, but this is the general principle of redirecting output.

     Regards

       Sahir

     

     

    Friday, November 17, 2006 5:38 PM

All replies

  •  SoftwareGerbil wrote:

     I have a third party unmanged library that performs some functions for me. I do not have source for it so I cannot change it. I have wrapped this library up and made a managed DLL out of it. My problem is that I am developing a GUI application and many of the third party functions print messages to STDOUT and/or STDERR via std::cout and/or printf. I would like to be able to redirect these irksome messages to a RichTextBox I just don't have any idea of how to do it.

      Try this :

      void CMFCTest2Dlg::OnBnClickedButton1()
      {
        ostrstream ostr;
        cout.rdbuf(ostr.rdbuf());
        cout<<"hello world";
        char* s = ostr.str(); 
        MessageBox(s);
      }

     This may need some refinement, but this is the general principle of redirecting output.

     Regards

       Sahir

     

     

    Friday, November 17, 2006 5:38 PM
  • Thanks for the information. Your solution, with some changes for my specific needs, does seem to work for redirecting std:cout. I am, however,  still at a loss for how to do the same thing with the "printf" calls that the third party library makes. Thanks again for your solution. It solved the lion's share of my problem as it appears most of the outputs were from std:cout calls.

     

    Sincerely,

    SoftwareGerbil

    Wednesday, November 22, 2006 12:42 PM
  • I am curious if you have come up with a solution for redirecting printf? I also have a DLL from a third party that is using printf to write to stdout. I have been looking for a way to redirect or log what is written to stdout off and on for the past couple days. I have found Win32 functions that read the screenbuffer, which might work, but would not be my first choice. I do all my work now in .NET using C# and I do know how to wrap unmanged code to use in C# if that was necessary

    Thanks

    ElectricAttic 

    Wednesday, November 29, 2006 9:14 PM
  •  ElectricAttic wrote:

     I am curious if you have come up with a solution for redirecting printf?

     One thing I can think of now is to use _wfreopen  to redirect stdout to a disk file. I tried this and it worked 

      void CMFCTestDlg::OnBnClickedButton2()
      {
         _wfreopen(L"c:\\Test\\consoleoutput.txt" , L"a" , stdout);
          printf("hello world");
      }

      You basically need to read from stdout which is a file pointer. If you look through the C run time library it shouldn't be too difficult to find a function to read directly from stdout into a char array. 

     Regards

       Sahir Shah 

    Saturday, December 02, 2006 7:31 AM
  • This question is a year old, but for the benefit of anyone in the future, if this question were asked in a Windows/Platform SDK newsgroup or in a forum in another site in which Windows programming is a subject, then there is a SDK solution that would work for printf as well as std :: cout and std :: cerr. SDK redirection can also write to std :: cin.

    Thursday, November 08, 2007 7:25 AM
  • Simple Samples said:

    This question is a year old, but for the benefit of anyone in the future, if this question were asked in a Windows/Platform SDK newsgroup or in a forum in another site in which Windows programming is a subject, then there is a SDK solution that would work for printf as well as std :: cout and std :: cerr. SDK redirection can also write to std :: cin.





    Which SDK solution would you be referring to as I am revisiting this subject area again?

    Software Gerbil
    Saturday, August 02, 2008 4:30 PM
  • Hi,
    I am also in a similar problem, have yuo found any solution to this?

    Vijay
    Thursday, July 23, 2009 2:26 AM
  • I am interested in this as well.

    Jer 29:11
    Friday, October 09, 2009 11:50 AM
  • I am sorry for not responding a couple of years ago. I probably did not see the reply then. For Windows API, see Redirection in my web site. For .Net it is easier; the following is a simple sample of redirecting the standard output of a process we created:

    Process myProcess = new Process();
    ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("netstat");
    myProcessStartInfo.UseShellExecute = false;
    myProcessStartInfo.RedirectStandardOutput = true;
    myProcess.StartInfo = myProcessStartInfo;
    myProcess.Start();
    myProcess.WaitForExit(30000);
    StreamReader myStreamReader = myProcess.StandardOutput;
    string myString = myStreamReader.ReadToEnd();
    Console.Write(myString);
    myProcess.Close();
    


    Sam Hobbs; see my SimpleSamples.Info
    Sunday, April 11, 2010 2:30 AM