如何使CreateProccess和管道完全像普通的cmd管道一样

时间:2016-06-06 13:05:24

标签: c++ windows winapi

我正在尝试创建软件以准确模仿CMD的管道。

在我输入的cmd中,例如:

openssl enc -aes-128-ofb -d -in encrypted.bin -iv a2b050be9463 -K 6ba62eb7bb2ccace -nopad|mplayer -

然后openssl直接将内容传递给mplayer,当你寻找到文件的末尾时,mplayer关闭,一切正常。

我试着像这样模仿CMD的管道:

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);

我的问题是我的程序和CMD的管道行为不一样。

在我的节目中,当电影结束时MPLAYER卡住了,(他正在运行电影最后一帧的某种循环,然后屏幕冻结,软件停止(很差 - 崩溃)),这让我我想可能是我没有使用正确的标志打开过程,或者我打开管道时可能会出错。

如何确保我的程序和CMD的管道行为完全相同?

0 个答案:

没有答案