Thread: catch all printf of c process in c# application

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    12

    catch all printf of c process in c# application

    I have c# app. that run c exe with Process.Start

    I want that c exe will communicate with c# exe (not only via return value).

    I think if is there any way that in c file I will print with printf and c# application will listening to the those print (catch it like event)

    is that option? or is there any simple way?

    thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2018
    Posts
    12
    Are you sure that those function will catch printf?

    That not working

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by C progammer View Post
    Are you sure that those function will catch printf?

    That not working
    1. Does the sample code given in that Received Event page work for you? (If not, what doesn't work about it?)
    2. Can your C# program run your C program (i.e., do you have the right path, does process.Start() fail, etc)? (If your C program is supposed to be already running when your C# program starts, then you'll probably have to get a bit fancier about this part.)
    3. Does your C program print out the kind of data that this thing expects (lines that end in a \n character)?

    If the program "doesn't work", then either your getting a compile error, or you should be checking return codes/exceptions/etc to find out what's not working about it.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Are you sure that those function will catch printf?
    Microsoft seem pretty sure that they will.

    > That not working
    Post some evidence of effort!

    Does this work in a console?
    prog.exe > results.txt

    That is, if you invoke your C executable from a command prompt, and redirect stdout to a file, do you get a file?
    If you do, then there is nothing wrong with the general concept of using those functions I previously listed to capture the stdout of a sub-process.


    Unless your 'C' program is some 30-year old ancient DOS program which achieves output by writing directly to the screen, it's going to work if you've done your job properly.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Nov 2018
    Posts
    12
    Quote Originally Posted by Salem View Post
    > Are you sure that those function will catch printf?
    Microsoft seem pretty sure that they will.

    > That not working
    Post some evidence of effort!

    Does this work in a console?
    prog.exe > results.txt

    That is, if you invoke your C executable from a command prompt, and redirect stdout to a file, do you get a file?
    If you do, then there is nothing wrong with the general concept of using those functions I previously listed to capture the stdout of a sub-process.


    Unless your 'C' program is some 30-year old ancient DOS program which achieves output by writing directly to the screen, it's going to work if you've done your job properly.
    When I running my c code like prog.exe > results.txt I get all the printf to files.

    In my c I do printf("test \n"); and then it continue to run ....

    I tried to catch the printf with OutputDataReceived event
    I see that I catch the "test" in my c# code event only when the c process finish.

    I want to catch it with OutputDataReceived immediately when printf happend.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Post your actual C# code.

    Does it look like the examples provided by Microsoft, or have you missed of some important detail.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you get all the other output you're expecting from the program too? (Or is the program supposed to be interactive in some way?) It's supposed to be asynchronous reading, so your C program isn't necessarily going to wait on your C# reader unless there's some manual input required.

  9. #9
    Registered User
    Join Date
    Nov 2018
    Posts
    12
    Quote Originally Posted by tabstop View Post
    Do you get all the other output you're expecting from the program too? (Or is the program supposed to be interactive in some way?) It's supposed to be asynchronous reading, so your C program isn't necessarily going to wait on your C# reader unless there's some manual input required.
    I don't get you
    when the c program finish I get all the printf to my c# in OutputDataReceived event.
    What I want is get to c# event all the output from printf
    immediately when the c code print it, like I run this c code and see printf in cmd immediately when c print it

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by C progammer View Post
    I don't get you
    when the c program finish I get all the printf to my c# in OutputDataReceived event.
    What I want is get to c# event all the output from printf
    immediately when the c code print it, like I run this c code and see printf in cmd immediately when c print it
    Based on what you're saying, I would guess that you are getting all the output from printf immediately when the C code prints it. You just need to actually process it in your event handler. Every time your C code prints a line, your event handler gets that info.

    (Note that the example at the link Salem posted used WaitForExit before printing any output -- the event handler just added data to a big ol' string, which was printed at the very end in one go. Since WaitForExit, perhaps unsurprisingly, waits there until the calling program finishes, that may not be exactly what you want to do.)

  11. #11
    Registered User
    Join Date
    Nov 2018
    Posts
    12
    Quote Originally Posted by tabstop View Post
    Based on what you're saying, I would guess that you are getting all the output from printf immediately when the C code prints it. You just need to actually process it in your event handler. Every time your C code prints a line, your event handler gets that info.

    (Note that the example at the link Salem posted used WaitForExit before printing any output -- the event handler just added data to a big ol' string, which was printed at the very end in one go. Since WaitForExit, perhaps unsurprisingly, waits there until the calling program finishes, that may not be exactly what you want to do.)
    I have event handler ofcourse, but it called only when c program finish to run...even I printf line

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by C progammer View Post
    I have event handler ofcourse, but it called only when c program finish to run
    I don't believe that that is true (not that I believe you to be lying, just mistaken). Why do you believe that to be true? How can you tell based on what you are seeing that the C program is already finished? (In other words, show something. Ideally code, but if not code at least the output that you are basing this belief on.)

  13. #13
    Registered User
    Join Date
    Nov 2018
    Posts
    12
    Quote Originally Posted by tabstop View Post
    I don't believe that that is true (not that I believe you to be lying, just mistaken). Why do you believe that to be true? How can you tell based on what you are seeing that the C program is already finished? (In other words, show something. Ideally code, but if not code at least the output that you are basing this belief on.)
    C code Paste ofCode
    C# code Paste ofCode

    I catch the start printf in c# code event only after 5 seconds

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your Sleep(5000) puts everything to sleep, since that process that you started is in the same thread as everything else. I modified a numerical DE solver I had sitting around that took about eight seconds to run to print "Starting" when it started and "Finished" when it ended and called it from your project2.cs code, and it printed "Starting" right away and "Finished" about eight seconds later. As long as your C program is doing work, your C# program will process.

  15. #15
    Registered User
    Join Date
    Nov 2018
    Posts
    12
    Quote Originally Posted by tabstop View Post
    Your Sleep(5000) puts everything to sleep, since that process that you started is in the same thread as everything else. I modified a numerical DE solver I had sitting around that took about eight seconds to run to print "Starting" when it started and "Finished" when it ended and called it from your project2.cs code, and it printed "Starting" right away and "Finished" about eight seconds later. As long as your C program is doing work, your C# program will process.
    I dont understand you, do you say that it relate to sleep?

    Even ehen I edit my c code to , drop out the sleep and put some loops that take time
    Paste ofCode
    (those loops take about 18 seconds) , I get the "start" only when the c program finish
    Last edited by C progammer; 11-10-2018 at 11:21 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. best process for cross compiling an application
    By nanodog in forum C Programming
    Replies: 4
    Last Post: 10-26-2013, 04:04 PM
  2. Replies: 5
    Last Post: 09-04-2011, 01:22 AM
  3. ?writing bug in a multi-process Linux server application?
    By Arrummzen in forum C Programming
    Replies: 7
    Last Post: 12-14-2002, 11:53 AM
  4. Process still active when application exits
    By xds4lx in forum Windows Programming
    Replies: 13
    Last Post: 08-15-2002, 02:17 AM
  5. Application running as a process in Win2k?????
    By J_Bravo in forum Windows Programming
    Replies: 8
    Last Post: 05-07-2002, 04:02 AM

Tags for this Thread