匿名管道通讯实现

2019-02-20 00:43:44来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

	// 生成bat文件
	std::ofstream ofs("network_check.bat");
	ofs << "@echo 360os Webservice connect check begin" << std::endl;
	ofs << "@echo 360os Webservice connect check end" << std::endl;
	ofs.close();
	// 创建管道
	SECURITY_ATTRIBUTES sa;
	sa.nLength = sizeof(SECURITY_ATTRIBUTES);
	sa.bInheritHandle = TRUE;
	sa.lpSecurityDescriptor = NULL;	
	HANDLE hRead, hWrite;
	if (!CreatePipe(&hRead, &hWrite, &sa, 0))
	{
		DWORD dErr = GetLastError();
		CString szInfo;
		szInfo.Format(_T("Fail to Create Pipe Error: %d"), dErr);
		outInfo = szInfo;
		return FALSE;
	}
	// 创建进程
	STARTUPINFO si = {sizeof(STARTUPINFO)};
	si.hStdError = hWrite;
	si.hStdOutput = hWrite;
	si.wShowWindow = SW_HIDE;
	si.hStdInput = hRead;
	si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
	PROCESS_INFORMATION pi;
	CString strCmd = _T("cmd.exe /c network_check.bat");
	if (!CreateProcess(NULL, strCmd.GetBuffer(), NULL, NULL, TRUE, NULL, NULL, NULL, &si, &pi))
	{
		outInfo = _T("CreateProcess failed");
		CloseHandle(hWrite);
		CloseHandle(hRead);
		return FALSE;
	}
	// 读取管道信息
	std::string strResult;
	char buff[1025] = {0};
	while (1)
	{
		DWORD dwRead = 0;
		PeekNamedPipe(hRead, buff, 4, &dwRead, NULL, NULL);
		if (0 == dwRead)
		{
			continue;
		}
		ZeroMemory(buff, sizeof(buff));
		ReadFile(hRead, buff, 1024, &dwRead, NULL);
		strResult += buff;
		char* pStr = new char[strlen(buff) + 1];
		strcpy_s(pStr, strlen(buff) + 1, buff);
		::SendMessage(m_hWnd, WM_MSG_TESTINFO, (WPARAM)pStr,0);
		if (strResult.find("360os Webservice connect check end") != std::string::npos)
		{
			break;
		}
	}
	// 释放资源
	CloseHandle(hWrite);
	CloseHandle(hRead);
	CloseHandle(pi.hThread);
	CloseHandle(pi.hProcess);

  


原文链接:https://www.cnblogs.com/manongdashu/p/10399343.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:L1-054 福到了

下一篇:【HDU - 1429】胜利大逃亡(续) (高级搜索)【状态压缩+BFS】