1. 程式人生 > >使用CreateProcess建立程序

使用CreateProcess建立程序

#include <iostream>
#include <windows.h>
using namespace std;
 
int main(int argc, char* argv[])
{
	STARTUPINFO si = { sizeof(si) };
	PROCESS_INFORMATION pi;
 
	si.dwFlags = STARTF_USESHOWWINDOW;
	si.wShowWindow = TRUE;
	char szCommandLine[]="cmd";
	BOOL bRet = ::CreateProcess (
		NULL,
		szCommandLine,
		NULL,
		NULL,
		FALSE,
		CREATE_NEW_CONSOLE,
		NULL,
		NULL,
		&si,
		&pi);
	int nError = GetLastError();
	if(bRet)
	{
		cout<<"程序的程序ID號:"<<pi.dwProcessId<<endl;
		cout<<"程序的主執行緒ID號:"<<pi.dwThreadId<<endl;
		::CloseHandle (pi.hThread);
		::CloseHandle (pi.hProcess);
	}
	else
	{
		cout<<"建立程序失敗:"<<nError<<endl;
	}
return 0;
}