1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
   |  
void		WExec::createChildProcess(HANDLE& wr_h)
{
	TCHAR cmd[]=TEXT("toto");
	PROCESS_INFORMATION pi;
	STARTUPINFO si;
	BOOL bSuccess = FALSE;
 
	ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
	ZeroMemory(&si, sizeof(STARTUPINFO));
	si.cb = sizeof(STARTUPINFO);
	si.hStdError = stderr;
	si.hStdOutput = wr_h;
	si.hStdInput = stdin;
	si.dwFlags |= STARTF_USESTDHANDLES;
 
	if (CreateProcess(NULL, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
		std::cout << "Creating Process Succeed" << std::endl;
	else
	{
		std::cout << "Creating Process Failed" << std::endl;
		CloseHandle(pi.hProcess);
		CloseHandle(pi.hThread);
	}
}
 
void		WExec::readPipe(HANDLE& rd_p, HANDLE& wr_c)
{
	LPDWORD dr = 0, dw = 0;
	CHAR buf[BUFSIZE];	
	BOOL flag = FALSE;
	std::string	tmp;
 
	HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
	ZeroMemory(&buf, BUFSIZE);
	if (!CloseHandle(wr_c))
		std::cout << "Closing Child HANDLE Failed" << std::endl;
	else
		std::cout << "Closing Child Handle succeed" << std::endl;
	for (;;)
	{
		flag = ReadFile(rd_p, &buf, BUFSIZE, NULL, NULL);
		std::cout << GetLastError() << std::endl;	
		if (!flag || dr == 0)
			break;
		flag = WriteFile(out, buf, (DWORD)dr, dw, NULL);
		if (!flag)
			break;
	}
	CloseHandle(rd_p);
}
 
std::string&	WExec::execute(std::string& file, std::list<std::string> & param)
{
	SECURITY_ATTRIBUTES	sa;
	HANDLE wr_child = NULL;
	HANDLE rd_parent = NULL;
 
	sa.bInheritHandle = TRUE;
	sa.lpSecurityDescriptor = NULL;
	sa.nLength = sizeof(SECURITY_ATTRIBUTES);
 
	if (CreatePipe(&rd_parent, &wr_child, &sa, 0) == 0)
	{
		std::cout << "Error creating first pipe" << std::endl;
		return *(new std::string(""));
	}
	if (!SetHandleInformation(rd_parent, HANDLE_FLAG_INHERIT, 0))
	{
		std::cout << "Error in inheriting handle : " << GetLastError() << std::endl;
		return *(new std::string(""));
	}
 
	this->createChildProcess(wr_child);
	this->readPipe(rd_parent, wr_child);
	return *(new std::string(""));
} | 
Partager