How to make CreateProccess and pipe act exactly as normal cmd pipe

I'm trying to create software to accurately imitate the pipe of the CMD.

In cmd when i typed for example :

openssl enc -aes-128-ofb -d -in encrypted.bin -iv a2b050be9463 -K 6ba62eb7bb2ccace -nopad|mplayer -
then openssl pipe the contant directly to mplayer and when you seek to the end of the file , mplayer closed and every thing works fine.

I try to imitate the pipe of the CMD like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
        PROCESS_INFORMATION piSource, piDest;
	HANDLE hPipeIn, hPipeOut;
	HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	STARTUPINFOW suSource, suDest;
	ZeroMemory(&suSource, sizeof(suSource));
	ZeroMemory(&suDest, sizeof(suDest));
	suSource.cb = suDest.cb = sizeof(STARTUPINFOW);
	suSource.dwFlags = suDest.dwFlags = STARTF_USESTDHANDLES;
	SECURITY_ATTRIBUTES sa;
	sa.nLength = sizeof(sa);
	sa.lpSecurityDescriptor = 0;
	sa.bInheritHandle = TRUE;


	//create a pipe
	if (CreatePipe(&hPipeIn, &hPipeOut, &sa, 0) == 0)
	{
		return GetLastError();
	}

	suSource.hStdInput = hIn;		         //hin=STD_INPUT_HANDLE input of source = input
	suSource.hStdError = suSource.hStdOutput = hPipeOut;//output and error of source = input of pipe
	suDest.hStdInput = hPipeIn;					//destInput = pipe output
	suDest.hStdError = suDest.hStdOutput = hOut;	//hout = STD_OUTPUT_HANDLE = output of destination


	
													
        //to hide the new window
	suDest.wShowWindow = SW_HIDE; 

	//open 2 process and pipe them 1 to another
	//openssl proccess
	std::wcout << opensslProgram << L" " << openSslFullParameters << std::endl; 
	LPWSTR param1 = const_cast<LPWSTR>(openSslFullParameters.c_str());
	if (CreateProcess(opensslProgram.c_str(), param1, NULL, NULL, TRUE, 0, NULL, NULL, &suSource, &piSource) == 0)
	{
		return GetLastError();
	}

	CloseHandle(piSource.hThread);

	//mplayer need to read from stdout of openssl		
	LPWSTR param2 = const_cast<LPWSTR>(mplayerParameters.c_str());

	if (CreateProcess(mplayerProgram.c_str(), param2, NULL, NULL, TRUE,/*NULL*/ CREATE_NO_WINDOW, NULL, NULL, &suDest, &piDest) == 0) {//CREATE_NO_WINDOW
		return GetLastError();
	}

	
	CloseHandle(piDest.hThread);
	HANDLE hArray[2];
	hArray[0] = piSource.hProcess;
	hArray[1] = piDest.hProcess;
       WaitForMultipleObjects(2, hArray, TRUE, INFINITE);

My problem is that my program and pipe of the CMD does not behave the same.

In my program, when the film comes to end MPLAYER stuck, (he is running some kind of a loop of the film last frame, and then the screen freezes and the software stops (poorly - crash)), it made me think that probably I did not use the right flags to open the process or that I might have a mistake in opening the pipes.
How can I make sure that my program and the pipes of the CMD will behave exactly the same?
Last edited on
Be careful that you're not mixing and matching wide characters and ASCII. It's not the issue you're having, but it's a bad habit to get into.

My guess is that the program is "freezing" because you haven't closed your pipes yet. Programs don't actually freeze by the way unless the system they are running on has a memory problem. What you are seeing is the application waiting for I\O to finish and handles to close.
Topic archived. No new replies allowed.