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

    Get the stdout of a console process

    (First of all,sorry for my poor english)

    Hy guys,what i want is simple but i can't do.
    I want to create a process using the Windows API and get the output of this process.
    For example,i start the ipconfig.exe process with my code and get the output of this process.

    Like this:

    c:\>ipconfig.exe

    //THE OUTPUT OF THE PROCESS
    Wireless LAN adapter Wi-Fi:

    Connection-specific DNS Suffix . :
    IPv4 Address. . . . . . . . . . . : 192.168.0.105
    Subnet Mask . . . . . . . . . . . : 255.255.255.0
    Default Gateway . . . . . . . . . : 192.168.0.1

    My code

    Code:
    #include <windows.h>
    #include <stdio.h>
    
    int main(){
    
    	HANDLE hChildOut = INVALID_HANDLE_VALUE;
    	HANDLE hChildIn = INVALID_HANDLE_VALUE;
    	
    	SECURITY_ATTRIBUTES saAttr; 
    	PROCESS_INFORMATION piProcInfo; 
    	STARTUPINFO siStartInfo,si;
    	BOOL bSuccess = FALSE;   //NON SEI ONDE ENTRA ESSA PORRA
    	unsigned char buffer[1000]; 
    
    	saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);  //DEFINE AQUI SASPORRA
    	saAttr.bInheritHandle = TRUE; 
    	saAttr.lpSecurityDescriptor = NULL;
    
    	// Create a pipe for the child process's STDOUT.
    	if ( !CreatePipe(&hChildIn, &hChildOut, &saAttr, 0) ) {
    			printf("Failed to create a pipe"); 
    	}
    
    	// Ensure the read handle to the pipe for STDOUT is not inherited.
    	if ( !SetHandleInformation(hChildOut, HANDLE_FLAG_INHERIT, 0) ){
    			printf("Failed to set handle information");
    	}
    
    	//ZERA PROCESS INFORMATION
    	//ISSO SÓ É PREENCHIDO DEPOIS DO PROCESSO EXECUTADO
    	ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
    
    	// Set up members of the STARTUPINFO structure. 
    	// This structure specifies the STDIN and STDOUT handles for redirection.
    	ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );  //START INFOS
    
    	siStartInfo.cb = sizeof(STARTUPINFO); 
    	siStartInfo.hStdError = hChildOut;
    	siStartInfo.hStdOutput = hChildOut;
    	siStartInfo.hStdInput = hChildIn;
    	siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
    
    	if(!CreateProcess("c:\\windows\\system32\\ipconfig.exe", 
          NULL,     //command line 
          &saAttr,    //process security attributes 
          NULL,     // primary thread security attributes 
          TRUE,     // handles are inherited 
          0,        // creation flags 
          NULL,     // use parent's environment 
          NULL,     // use parent's current directory 
          &siStartInfo,  // STARTUPINFO pointer 
          &piProcInfo)  // receives PROCESS_INFORMATION 
    
    	){
    
    		printf("Seh fodeo %d\n",GetLastError());
    		return -1;
    	}
    
    	printf("hChildOut handle -> %d\n",hChildOut);
    
    	if( !ReadFile(hChildOut,buffer,500,NULL,NULL) ){
    
    		printf("Error %d\n",GetLastError());
    		return -10;
    	}
    
    	printf("Saida do processo:\n%s\n",&buffer);
    
    	return 0;
    }

    Error five means:


    ERROR_ACCESS_DENIED
    5 (0x5)
    Access is denied.

    Anyone can help me?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Get the stdout of a console process

    What API call failed?
    Victor Nijegorodov

  3. #3
    Join Date
    Dec 2014
    Posts
    4

    Re: Get the stdout of a console process

    ReadFile

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

    Re: Get the stdout of a console process

    Why not simply use stream redirection? eg
    Code:
    c:\>ipconfig > ipcon.txt
    this will create the file ipcon.txt containing the output from the ipconfig command.
    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)

  5. #5
    Join Date
    Dec 2014
    Posts
    4

    Re: Get the stdout of a console process

    This is a clever trick 2kaud,but i want to learn how to use Pipe,indeed using the redirect stream was my first idea,but now i want do with pipe...
    I saw in another forum a guy using pipes of windows API with pascal OO ( delphi ),and worked,but my code don't.

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

    Re: Get the stdout of a console process

    You need to create 2 pipes - one for stdin, one for stdout/stderr. You then use the stdout/write handle for stdout/stderr and the stdin/read handle for stdin.

    See http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx for an example.
    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)

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Get the stdout of a console process

    Best regards,
    Igor

  8. #8
    Join Date
    Dec 2014
    Posts
    4

    Re: Get the stdout of a console process

    I saw all this examples,i Will try again...

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