createprocess如何使用
时间: 2025-02-12 10:23:00 浏览: 32
### 使用 `CreateProcess` 函数创建进程
在 Windows API 中,`CreateProcess` 是用于启动新进程的重要函数。下面展示了一个基本的例子,说明如何通过该函数启动一个新的应用程序。
#### 基本语法
为了调用此功能并成功启动目标应用,需提供必要的参数给 `CreateProcess` 函数[^1]:
```cpp
#include <windows.h>
#include <stdio.h>
int main() {
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
// 启动记事本程序作为例子
if (!CreateProcess(
NULL, // No module name (use command line)
L"notepad.exe", // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
) {
printf("CreateProcess failed (%d).\n", GetLastError());
return -1;
}
// 等待直到子进程退出.
WaitForSingleObject(pi.hProcess, INFINITE);
// 关闭句柄
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 0;
}
```
这段 C++ 代码片段展示了怎样利用 `CreateProcess` 来启动一个名为 "notepad.exe" 的简单外部程序,并等待其完成执行后再继续运行后续逻辑。
阅读全文
相关推荐


















