创建进程,输出重定向

两个函数,输出重定向以及转换编码,想用的拿去用,思路就是这样,拿去自己封装函数即可

void OutputRedirection()
{
	PROCESS_INFORMATION pi = {0};
	STARTUPINFO si = {0};
	si.cb = sizeof(si);

	HANDLE hStdInRead = 0;
	HANDLE hStdOutWrite = 0;

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

	if(!CreatePipe(&hStdInRead, &hStdOutWrite, &seAttr, 0))
	{
		return;
	}

	if(!SetHandleInformation(hStdInRead, HANDLE_FLAG_INHERIT, 0))
	{
		return;
	}

	si.hStdError = hStdOutWrite;
	si.hStdOutput = hStdOutWrite;
	si.hStdInput = hStdInRead;
	si.dwFlags |= (STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW);
	si.wShowWindow = SW_HIDE;

	BOOL bSuc = CreateProcess(L"Exe",
							  L"CmdLine",
							  NULL,
							  NULL,
							  TRUE,
							  0,
							  NULL,
							  NULL,
							  &si,
							  &pi);
	if(bSuc)
	{
		CloseHandle(pi.hThread);
		DWORD dwRet = WaitForSingleObject(pi.hProcess, 3000);

		DWORD dwRead;
		char szbuf[4096] = {0};
		BOOL bPeek = PeekNamedPipe(hStdInRead, szbuf, 4096, &dwRead, NULL, NULL);
		if(!bPeek)
		{
			return;
		}
		// szBuf = OutPut
	}
}

std::wstring Utf82Unicode(std::string _str)
{
	std::wstring strOut = L"";
	int nLen = (int)_str.length();
	int nResult = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)_str.c_str(), nLen, NULL, NULL);
	if(nResult == 0)
	{
		return L"";
	}
	strOut.resize(nResult, L' ');
	MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)_str.c_str(), nLen, (LPWSTR)strOut.c_str(), nResult);
	return strOut;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值