win32 创建进程三种方式简单示例 - 使用CFree
程序员文章站
2024-03-01 12:16:58
...
新建一个简单窗口工程;
语言在C和C++中选择C;构建配置默认,mingw5;
#include <windows.h>
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
/* TODO: Place code here.*/
//LPPROCESS_INFORMATION info=NULL;
PROCESS_INFORMATION pi;
STARTUPINFO si = { sizeof(si) };
//CreateProcess(TEXT("Y:\\temp\\notepad.exe"),0,0,0,0,0,0,0,&si,info);
CreateProcess("c:\\windows\\write.exe",NULL,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi);
WinExec("mspaint",SW_SHOW);
ShellExecute(0,"open","notepad","c:\\a.txt","",SW_SHOW);
return 0;
}
代码如上;构建成功;运行;
CreateProcess打开了写字板程序;WinExec打开了Win自带画图程序;ShellExecute打开了C盘的a.txt文件;
CreateProcess最后两个参数是两个结构体的地址,启动信息,进程信息;
如果如下定义和调用,构建成功,但创建进程失败;
LPPROCESS_INFORMATION info=NULL;
CreateProcess(TEXT("Y:\\temp\\notepad.exe"),0,0,0,0,0,0,0,&si,info);