Thread: pipe cmd.exe (solved)

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    7

    pipe cmd.exe (solved)

    I am having trouble getting thise code to work:

    Code:
    #include <windows.h>
    #include <stdio.h>
    
    #define MAX_BUFFER_SIZE 512
    
    int EmulateCommandPrompt(LPSTR cmdline)
    {
    	STARTUPINFO sti = { 0 };
    	SECURITY_ATTRIBUTES sats = { 0 };
    	PROCESS_INFORMATION pi = { 0 };
    	HANDLE pipin_w, pipin_r, pipout_w, pipout_r;
    	BYTE buffer[MAX_BUFFER_SIZE];
    	DWORD writ, excode, read, available;
    	int ret = 0;
    	
    	pipin_w = pipin_r = pipout_w = pipout_r = NULL;
    	
    	for(;;)
    	{
    		//set SECURITY_ATTRIBUTES struct fields
    		sats.nLength = sizeof(sats);
    		sats.bInheritHandle = TRUE;
    		sats.lpSecurityDescriptor = NULL;
    		
    		//create child's stdout pipes
    		if(!CreatePipe(&pipout_r, &pipout_w, &sats, 0)) break;
    		//and its stdin pipes
    		if(!CreatePipe(&pipin_r, &pipin_w, &sats, 0)) break;
    		printf("Created pipes\n");
    		
    		//now set STARTUPINFO struct fields (from the child's point of view)
    		sti.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
    		sti.wShowWindow = SW_HIDE;
    		sti.hStdInput = pipin_r;
    		sti.hStdOutput = pipout_w;
    		sti.hStdError = pipout_w;
    		
    		//create the process...
    		if(!CreateProcess(NULL, "cmd.exe", NULL, NULL, TRUE, 
    			0, NULL, NULL, &sti, pi)) break;
    		printf("Created process (%s)\n", cmdline);
    		
    		//now have a continuous loop to get and recieve info
    		for(;;)
    		{
    			//make sure process is still running
    			GetExitCodeProcess(pi.hProcess, &excode);
    			if(excode != STILL_ACTIVE) break;
    			//printf("Process still running\n");
    			
    			//give it time to set up/react
    			Sleep(500);
    			
    			//now check to see if process has anything to say
    			if(!PeekNamedPipe(pipout_r, buffer, 
    				sizeof(buffer), &read, &available, NULL)) ret = 10;
    			//printf("Peeked\n");
    			
    			//is there anything to be read in the pipe?
    			if(read)
    			{
    				do
    				{
    					ZeroMemory(buffer, sizeof(buffer));
    					//read it and print to stdout
    					if(!ReadFile(pipout_r, buffer, sizeof(buffer), &read, NULL) || !read) ret = 7;
    					buffer[read] = 0;
    					fprintf(stdout, "%s", buffer);
    					if(ret) break;
    				}
    				while(read >= sizeof(buffer));
    			}
    			
    			//make sure we didn't run into any errors
    			if(!ret)
    			{
    				//get info and write it to pipe
    				ZeroMemory(buffer, sizeof(buffer));
    				fgets(buffer, sizeof(buffer), stdin);
    				if(!strnicmp(buffer, "exit", 4)) ret = 12;
    				if(!WriteFile(pipin_w, buffer, strlen(buffer), &writ, NULL)) ret = 8;
    			}
    			if(ret) break;
    		}
    		
    		break;
    	}
    	
    	//clean up any unfinished business
    	if(pipin_w != NULL) CloseHandle(pipin_w);
    	if(pipin_r != NULL) CloseHandle(pipin_r);
    	if(pipout_w != NULL) CloseHandle(pipout_w);
    	if(pipout_r != NULL) CloseHandle(pipout_r);
    	if(pi.hProcess != NULL) CloseHandle(pi.hProcess);
    	if(pi.hThread != NULL) CloseHandle(pi.hThread);
    	
    	return ret;
    }
    
    int main(int argc, char *argv[])
    {
    	EmulateCommandPrompt(NULL);
    	
    	return 0;
    }
    I found this on another forum. The problem is that CreateProcess 10th argument only accepts LPPROCESS_INFORMATION, where this program uses PROCESS_INFORMATION. I can change it but it won't compile because of pi not having pi.hPorcess and pi.hThread.

    I am new to windows programming and am not sure what to do. I was using this code as an example for my own program, to use cmd over a connection.

    Foxyy

  2. #2
    Registered User
    Join Date
    Jun 2010
    Posts
    7
    I added this code to my program and modified it so it would compile but it doesn't work, it just makes my wine crash. I don't think it's not working because of wine.

    Foxyy

    EDIT: nvm I got it to work
    Last edited by Foxy999; 06-29-2010 at 06:47 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 10-14-2009, 04:44 PM
  2. cont of IPC using PIPE
    By BMathis in forum C Programming
    Replies: 1
    Last Post: 03-15-2009, 05:16 PM
  3. Simple 2D rubiks cube algorithm.
    By jeanmarc in forum Game Programming
    Replies: 19
    Last Post: 11-11-2008, 07:40 PM
  4. Pipe class.
    By eXeCuTeR in forum Linux Programming
    Replies: 8
    Last Post: 08-21-2008, 03:44 AM
  5. Pipe(): Interprocess or Intraprocess comm?
    By @nthony in forum C Programming
    Replies: 2
    Last Post: 03-28-2007, 07:27 PM