我是靠谱客的博主 甜蜜小蘑菇,最近开发中收集的这篇文章主要介绍CreateProcess执行控制台程序,并获取输出,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

void StartProcess(LPCWSTR program, LPCWSTR args)
{
	const int MY_PIPE_BUFFER_SIZE = 1024;
	//初始化管道
	HANDLE hPipeRead;
	HANDLE hPipeWrite;
	SECURITY_ATTRIBUTES saOutPipe;
	::ZeroMemory(&saOutPipe, sizeof(saOutPipe));
	saOutPipe.nLength = sizeof(SECURITY_ATTRIBUTES);
	saOutPipe.lpSecurityDescriptor = NULL;
	saOutPipe.bInheritHandle = TRUE;
	if (CreatePipe(&hPipeRead, &hPipeWrite, &saOutPipe, MY_PIPE_BUFFER_SIZE))
	{
		PROCESS_INFORMATION processInfo;
		::ZeroMemory(&processInfo, sizeof(processInfo));
		STARTUPINFO startupInfo;
		::ZeroMemory(&startupInfo, sizeof(startupInfo));
		startupInfo.cb = sizeof(STARTUPINFO);
		startupInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
		startupInfo.hStdOutput = hPipeWrite;
		startupInfo.hStdError = hPipeWrite;
		startupInfo.wShowWindow = SW_HIDE;

		//startupInfo.dwFlags = STARTF_USESHOWWINDOW;
		//startupInfo.hStdOutput = hPipeWrite;
		//startupInfo.hStdError = hPipeWrite;
		//startupInfo.wShowWindow = SW_HIDE;
		int ret;
		//while(TRUE)
		
		ret = ::CreateProcess(program, (LPWSTR)args,
			NULL,  // process security
			NULL,  // thread security
			TRUE,  //inheritance
			0,     //no startup flags
			NULL,  // no special environment
			NULL,  //default startup directory
			&startupInfo,
			&processInfo);
        //个人发现不加 sleep 进程启动有时候会失败,不清楚具体原因
		::Sleep(1000);
		WaitForSingleObject(processInfo.hProcess, 3000);
		if (ret == 0)
		{
			int nRet = GetLastError();
			printf("CreateProcess last error %d n", nRet);
		}
		else
		{
			DWORD dwReadLen = 0;
			DWORD dwStdLen = 0;
			if (PeekNamedPipe(hPipeRead, NULL, 0, NULL, &dwReadLen, NULL) && dwReadLen > 0)
			{
				char szPipeOut[MY_PIPE_BUFFER_SIZE];
				::ZeroMemory(szPipeOut, sizeof(szPipeOut));
				if (ReadFile(hPipeRead, szPipeOut, dwReadLen, &dwStdLen, NULL))
				{
					string s = string(szPipeOut);
					if (s.npos != s.find("True"))
					{
						printf("Recog Yes n");
					}
					else
					{
						printf("Recog NO n");
					}
				}
			}
	
		}
		if (processInfo.hProcess)
		{
			CloseHandle(processInfo.hProcess);
		}
		if (processInfo.hThread)
		{
			CloseHandle(processInfo.hThread);
		}
	}
	CloseHandle(hPipeRead);
	CloseHandle(hPipeWrite);
}



void ThriftUdpServer::CreatePyProcess(string userID)
{

    wchar_t program[MAX_PATH];// = TEXT("D:\ProgramData\Anaconda3\python.exe");
    wchar_t args[MAX_PATH]; // = TEXT(" E:\wqs\Python\recognition.py 610124198705273325 E:\wqs\vc10\PCMUdpServer\PCMUdpServer\610124198805273130.jpg recog E:\FaceData\FaceEncodingDir\  E:\FaceData\FaceImage\");
    

    wsprintf(program, L"D:\ProgramData\Anaconda3\python.exe");
    wsprintf(args, L" E:\wqs\Python\recognition.py 
        %s 
        %s.jpg 
        recog 
        %s 
        %s",multiByteToWideChar(userID), multiByteToWideChar(userID), FaceEncodingDir, FaceImageDir);

    StartProcess(program, args);
}

 

最后

以上就是甜蜜小蘑菇为你收集整理的CreateProcess执行控制台程序,并获取输出的全部内容,希望文章能够帮你解决CreateProcess执行控制台程序,并获取输出所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部