#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<windows.h>
#include <process.h>
using namespace std;
//拉起谷歌浏览器
int RunExe()
{
STARTUPINFO strStartupInfo;
memset(&strStartupInfo, 0, sizeof(strStartupInfo));
strStartupInfo.cb = sizeof(strStartupInfo);
PROCESS_INFORMATION szProcessInformation;
memset(&szProcessInformation, 0, sizeof(szProcessInformation));
/**
* CreateProcessW(
_In_opt_ LPCWSTR lpApplicationName,// 该字符串可以指定要执行的模块的完整路径和文件名 NULL
_Inout_opt_ LPWSTR lpCommandLine, //命令行
_In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes,//该 结构确定子进程是否可以继承返回到新进程对象的句柄。如果//lpProcessAttributes为NULL,则不能继承该句柄
_In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes,
//该 结构确定子进程是否可以继承返回到新线程对象的句柄。如果//lpThreadAttributes为NULL,则不能继承该句柄
_In_ BOOL bInheritHandles,
//如果此参数为TRUE,则新进程将继承调用进程中的每个可继承句柄。如果参//数为FALSE,则不会继承句柄。请注意,继承的句柄与原始句柄具有相同的值和//访问权限
_In_ DWORD dwCreationFlags,// 控制优先级类别和流程创建的标志 CREATE_NEW_CONSOLE
_In_opt_ LPVOID lpEnvironment,// 指向新进程的环境块的指针。如果此参数为//NULL,则新进程将使用调用进程的环境
//
_In_opt_ LPCWSTR lpCurrentDirectory,// 进程当前目录的完整路径
_In_ LPSTARTUPINFOW lpStartupInfo, //设置扩展属性
_Out_ LPPROCESS_INFORMATION lpProcessInformation // 该 结构接收有关新进程的标识//信息
);
*/
//TCHAR szCommanLine[] = L"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe";
TCHAR szCommanLine[] = L"\"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe\"http://www.baidu.com/";
//TCHAR szCommanLine[] = L"cmd.exe";
BOOL flag=CreateProcess(NULL,// 该字符串可以指定要执行的模块的完整路径和文件名 NULL
szCommanLine, //命令行
NULL,//该 结构确定子进程是否可以继承返回到新进程对象的句柄。如果//lpProcessAttributes为NULL,则不能继承该句柄
NULL,//该 结构确定子进程是否可以继承返回到新进程对象的句柄。如果//lpProcessAttributes为NULL,则不能继承该句柄
FALSE,//如果此参数为TRUE,则新进程将继承调用进程中的每个可继承句柄。如果参//数为FALSE,则不会继承句柄。请注意,继承的句柄与原始句柄具有相同的值和//访问权限
CREATE_NEW_CONSOLE,// 控制优先级类别和流程创建的标志 CREATE_NEW_CONSOLE
NULL,// 指向新进程的环境块的指针。如果此参数为//NULL,则新进程将使用调用进程的环境
NULL,// 进程当前目录的完整路径
&strStartupInfo,//设置扩展属性
&szProcessInformation//结构接收有关新进程的标识//信息
);
if (flag)
{
//创建成功
cout << "flag = " << flag << endl;
WaitForSingleObject(szProcessInformation.hProcess, 3000);
cout << "Create Success szProcessInformation.hProcess = " << szProcessInformation.hProcess << endl;
cout << "Create Success szProcessInformation.hThread = " << szProcessInformation.hThread << endl;
cout << "Create Success szProcessInformation.dwProcessId = " << szProcessInformation.dwProcessId << endl;
cout << "Create Success szProcessInformation.dwThreadId = " << szProcessInformation.dwThreadId << endl;
CloseHandle(szProcessInformation.hProcess);//关闭新进程的进程句柄 谷歌 计数-1
CloseHandle(szProcessInformation.hThread);//关闭新进程的线程句柄 谷歌 计数-1
szProcessInformation.hProcess = NULL;
szProcessInformation.hThread = NULL;
szProcessInformation.dwProcessId = 0;
szProcessInformation.dwThreadId = 0;
}
else
{
cout << "Create Failed flag=" << flag << endl;
cout << "errorcode = " << GetLastError() << endl;
}
return 0;
}
int main()
{
cout << "拉起谷歌浏览器" << endl;
RunExe();
system("pause");
return EXIT_SUCCESS;
}