Programming/MFC-C++

Cmd 명령 실행, 제어, Pipe, SpawnAndRedirect

빠릿베짱이 2015. 5. 13. 09:17
반응형

ShellExecute(NULL, NULL, "cmd.exe", " /c 프로그램명 파라미터", NULL, SW_HIDE);

WinExec ("cmd.exe /k 프로그램명 파라미터",SW_SHOW) ;

system("프로그램명 파라미터");


			// 실행을 위해 구조체 세트
			ZeroMemory( &execinfo, sizeof(execinfo) );
			execinfo.cbSize = sizeof(execinfo);
			execinfo.lpVerb = "open";
			execinfo.lpFile = "cmd.exe";
			execinfo.lpParameters =  strPgmFileName;
			execinfo.fMask = SEE_MASK_FLAG_NO_UI | SEE_MASK_NOCLOSEPROCESS;
			execinfo.nShow = SW_HIDE;

			// 프로그램을 실행한다.
			int r = (int)ShellExecuteEx( &execinfo );
			//프로세스가 종료될 때까지 무한정 기다림
			WaitForSingleObject(execinfo.hProcess, INFINITE);



전역 함수 설정

HANDLE SpawnAndRedirect(LPCTSTR commandLine, HANDLE *hStdOutputReadPipe, LPCTSTR lpCurrentDirectory)
{
	HANDLE hStdOutputWritePipe, hStdOutput, hStdError;
	CreatePipe(hStdOutputReadPipe, &hStdOutputWritePipe, NULL, 0);	// create a non-inheritable pipe
	DuplicateHandle(GetCurrentProcess(), hStdOutputWritePipe,
		GetCurrentProcess(), &hStdOutput,	// duplicate the "write" end as inheritable stdout
		0, TRUE, DUPLICATE_SAME_ACCESS);
	DuplicateHandle(GetCurrentProcess(), hStdOutput,
		GetCurrentProcess(), &hStdError,	// duplicate stdout as inheritable stderr
		0, TRUE, DUPLICATE_SAME_ACCESS);
	CloseHandle(hStdOutputWritePipe);								// no longer need the non-inheritable "write" end of the pipe

	PROCESS_INFORMATION pi;
	STARTUPINFO si;
	ZeroMemory(&si, sizeof(STARTUPINFO));
	si.cb = sizeof(STARTUPINFO);
	si.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
	si.hStdInput  = GetStdHandle(STD_INPUT_HANDLE);	// (this is bad on a GUI app)
	si.hStdOutput = hStdOutput;
	si.hStdError  = hStdError;
	si.wShowWindow = SW_HIDE;						// IMPORTANT: hide subprocess console window
	TCHAR commandLineCopy[1024];					// CreateProcess requires a modifiable buffer
	_tcscpy(commandLineCopy, commandLine);
	if (!CreateProcess(	NULL, commandLineCopy, NULL, NULL, TRUE,
		CREATE_NEW_CONSOLE, NULL, lpCurrentDirectory, &si, &pi))
	{
		CloseHandle(hStdOutput);
		CloseHandle(hStdError);
		CloseHandle(*hStdOutputReadPipe);
		*hStdOutputReadPipe = INVALID_HANDLE_VALUE;
		return NULL;
	}

	CloseHandle(pi.hThread);
	CloseHandle(hStdOutput);
	CloseHandle(hStdError);
	return pi.hProcess;
}



명령 사용시


CSvMat CSvLibLinear::train(CString strParam, CString strFileName)
{
	CSvMat tt;
	CString strExcute,strStartTime, argu, OutputFile,execute1;
	CString strResize;
	strExcute.Format("train.exe ");
	
	HANDLE hOutput, hProcess;
	//strExcute + strParam : cmd 명령, 파라미터도 가능
	hProcess = SpawnAndRedirect(strExcute + strParam, &hOutput, NULL);
	if (!hProcess) 
		return tt;

	//pDlg->BeginWaitCursor();
	CHAR buffer[129];
	CString buff;
	DWORD read;
	CString str, tmp;
	int LineCount=0;
	//cmd 창의 출력 내용 가져오기
	while (ReadFile(hOutput, buffer, 128, &read, NULL))
	{
		buffer[read] = '\0';
		printf("%s\r\n", buffer);

	}

	printf(".");
	CloseHandle(hOutput);
	TerminateProcess(hProcess,0);
	CloseHandle(hProcess);

	return ReadTrainFile(strFileName+".model");
}


반응형