您的位置:首页 > 其它

CreateProcess("cmd.exe", "/k echo hello"

2013-04-29 14:39 190 查看
#include <AFX.H>//#include <Windows.H>
#include <WINBASE.H>
#include <stdio.H>

int main(int argc,char** argv,char** env)
{       
	SECURITY_ATTRIBUTES sa; 
	HANDLE hRead,hWrite; 
	sa.nLength = sizeof(SECURITY_ATTRIBUTES); 
	sa.lpSecurityDescriptor = NULL; 
	sa.bInheritHandle = TRUE; 
	if (!CreatePipe(&hRead,&hWrite,&sa,0)) { 
		printf("Error On CreatePipe()\n"); 
		return 1; 
	} 
	PROCESS_INFORMATION pi; 
	STARTUPINFO si; 
	si.cb = sizeof(STARTUPINFO); 
	GetStartupInfo(&si); 
	si.hStdError = hWrite; 
	si.hStdOutput = hWrite; 
	si.wShowWindow = SW_HIDE; 
	si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; 
	//if (!CreateProcess("C:\\WINDOWS\\system32\\cmd.exe", "/k echo hello", 
	if (!CreateProcess("cmd.exe", "/k echo hello", 
		NULL, NULL, TRUE, NULL, NULL, NULL, &si, &pi)) { 
			printf("Error on CreateProcess():%ld\n",::GetLastError()); 
		return 1;   
	} 
	CloseHandle(hWrite); 
	const int BufferLength = 1024; 
	CString showedMsg = ""; 
	char buffer[BufferLength + 1] = {0}; 
	DWORD bytesRead; 
	while (ReadFile(hRead,buffer,BufferLength,&bytesRead,NULL)) { 
		showedMsg+=buffer; //showedMsg.Append(buffer); //
		Sleep(200); 
	} 
//	if (showedMsg.Find("find Bugs") > 0) { 
//		printf("Error \n"); 
//		return 1; 
//	}
	printf("%s \n",(LPCTSTR)showedMsg); 
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: