匿名管道通讯实现

	// 生成bat文件
	std::ofstream ofs("network_check.bat");
	ofs << "@echo Webservice connect check begin" << std::endl;
	ofs << "@echo 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("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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值