Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Get the std output of a console process - Windows API

windows api process stdout

This topic has been archived. This means that you cannot reply to this topic.
15 replies to this topic

#13 FrancoAti

FrancoAti

    CC Regular

  • Member
  • PipPipPip
  • 32 posts

Posted 25 October 2014 - 06:16 PM

Ok,thanks for all,i'll try again and again...

#14 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 25 October 2014 - 07:38 PM

Yeah, sorry about that. If I come across anything in the next few days I'll let you know.


sudo rm -rf / && echo $'Sanitize your inputs!'


#15 FrancoAti

FrancoAti

    CC Regular

  • Member
  • PipPipPip
  • 32 posts

Posted 24 December 2014 - 02:08 PM

After a time without look this code i try again and get the following code:
 

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

int main(){

	HANDLE stdinhRead = INVALID_HANDLE_VALUE;
	HANDLE stdinhWrite = INVALID_HANDLE_VALUE;
	HANDLE stdouthRead = INVALID_HANDLE_VALUE;
	HANDLE stdouthWrite = INVALID_HANDLE_VALUE;
	SECURITY_ATTRIBUTES saAttr; 
	PROCESS_INFORMATION piProcInfo; 
	STARTUPINFO siStartInfo;
	unsigned char buffer[100]; 
	DWORD numRead;

	saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
	saAttr.bInheritHandle = TRUE; 
	saAttr.lpSecurityDescriptor = NULL;

	// Create a pipe for the child process's STDIN.
	if ( !CreatePipe(&stdinhRead,&stdinhWrite,&saAttr,0) ) {
			printf("Failed to create a pipe\nError %d",GetLastError()); 
	}

	// Create a pipe for the child process's STDOUT.
	if ( !CreatePipe(&stdouthRead,&stdouthWrite,&saAttr,0) ) {
			printf("Failed to create a pipe\nError %d",GetLastError()); 
	}

	ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
	ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );

	siStartInfo.cb = sizeof(STARTUPINFO); 
	siStartInfo.hStdError = stdouthRead;
	siStartInfo.hStdOutput = stdouthRead;
	siStartInfo.hStdInput = stdinhWrite;
	siStartInfo.dwFlags |= STARTF_USESTDHANDLES;

	if(!CreateProcess("C:\\windows\\system32\\ipconfig.exe", 
      NULL,     //command line 
      &saAttr,  //process security attributes 
      &saAttr,  // 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;
	}

	WaitForSingleObject(piProcInfo.hProcess,INFINITE);
	
	printf("Reading from the PIPE...\n");
	printf("Read Operation Status: %d",ReadFile(stdouthRead,&buffer,100,&numRead,NULL));

	printf("Saida do processo:\n%s\n",&buffer);
	
	return 0;
}

I now more or less why the ReadFile still reading and complete te operation,but i decided post where for a possible recomendation or something else...
Therefore anyone can help me?



#16 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 25 December 2014 - 11:38 PM

So what's your exact problem now?

 

A few problems I just spotted:

 

1) If you're reading 100 characters with ReadFile, your output buffer needs to be 101 characters to leave space for the terminating null. You also need to call ZeroMemory on it: ZeroMemory(buffer, 101). Note that I don't use &buffer, which brings me to my second point:

 

2) Don't use &buffer anywhere. Arrays are treated as pointers, so &buffer is actually the address of the variable that contains a pointer to your buffer. You just want a pointer to your buffer, which is just buffer. Thus, change where you use it to:

 

ReadFile(stdouthRead,buffer,100,&numRead,NULL))

 

and

 

printf("Saida do processo:\n%s\n",buffer)


Edited by dargueta, 25 December 2014 - 11:45 PM.

sudo rm -rf / && echo $'Sanitize your inputs!'





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download