vs c 创建进程
时间: 2023-10-01 17:06:39 浏览: 35
在 Visual Studio (VS) 中,可以使用 Windows API 来创建进程。具体而言,可以使用 CreateProcess 函数来创建进程。下面是一个简单的示例代码:
```c
#include <windows.h>
int main()
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
// Create the child process.
if (!CreateProcess(
TEXT("C:\\Windows\\System32\\notepad.exe"), // Path to the executable
NULL, // Command line parameters
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;
}
// Wait until child process exits.
WaitForSingleObject(pi.hProcess, INFINITE);
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 0;
}
```
该代码创建了一个新的进程,并运行 Notepad 程序。需要注意的是,CreateProcess 函数的第一个参数指定了要运行的可执行文件的路径,而第二个参数则可以指定命令行参数。在这个示例中,没有指定任何参数,因此 Notepad 将以默认设置启动。