CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2014
    Posts
    2

    Reading output from console through asp.net

    I am using this code to pass two numbers as input to an .exe of a C program file through asp.net and after that trying to read the output from console. I am having problem to read any output from the console. My asp.net code is.
    Code:
    string returnvalue;
    
    Process p = new Process();
    
    p.StartInfo.CreateNoWindow = true;
    p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    p.StartInfo.FileName = ("C:\\Users\\...\\noname01.exe");
    p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardInput = true;
    p.Start();
    Thread.Sleep(500);
    SendKeys.SendWait("1");
    Thread.Sleep(500);
    SendKeys.SendWait("~");
    Thread.Sleep(500);
    SendKeys.SendWait("2");
    Thread.Sleep(500);
    SendKeys.SendWait("~");
    Thread.Sleep(500);
    
    StreamReader sr = p.StandardOutput;
    
    returnvalue = sr.ReadToEnd();
    
    System.IO.StreamWriter file = new System.IO.StreamWriter("C:\\...\\StudentOutput.txt");
    file.WriteLine(returnvalue);
    My C code to which am passing inputs is.
    Code:
    #include<stdio.h>
    
        int main()
        {
        int a, b, c;
    
        printf("Enter two numbers to add\n");
        scanf("%d%d",&a,&b);
    
        c = a + b;
    
        printf("Sum of entered numbers = %d\n",c);
    
        return 0;
        }

    I am having problem to read any output from the console.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Reading output from console through asp.net

    Perhaps it's a permissions issue. Or it might be the account asp.net runs under can't access the console. Try piping the output of the exe to a file and then read the file.

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Reading output from console through asp.net

    I am having problem to read any output from the console.
    What problems are you having?

    I don't see any error checking in your asp.net code?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Aug 2014
    Posts
    2

    Re: Reading output from console through asp.net

    Quote Originally Posted by 2kaud View Post
    I don't see any error checking in your asp.net code?
    Whenever i run my program it opens the .exe file of the C program and pass the in put through sendkeys
    now i want to read all of that output from the console of the C program but its not working

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Reading output from console through asp.net

    Quote Originally Posted by hussainahmad007 View Post
    Whenever i run my program it opens the .exe file of the C program and pass the in put through sendkeys
    now i want to read all of that output from the console of the C program but its not working
    In what way is it not working? Do you get any errors reported?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Reading output from console through asp.net

    Sorry to say, but using send keys with a console app isn't the most reliable approach even under the best circumstances.

    That being said, did you read my first response? Did that make sense?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured