c++ - CreateProcess 异常 (kernel32.lib)

标签 c++ c winapi win32-process

<分区>

运行这段代码时出现异常:

STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));

CreateProcess(NULL, L"program.dat", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);

// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );

// Close process and thread handles. 
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );

我在 WaitForSingleObject 上得到异常。

谢谢:)

最佳答案

documentation 中尽可能清楚地说明::

The Unicode version of this function, CreateProcessW, can modify the contents of this string. Therefore, this parameter cannot be a pointer to read-only memory (such as a const variable or a literal string). If this parameter is a constant string, the function may cause an access violation.

您的 L"program.dat" 符合此规则。将字符串复制到某个 WCHAR 变量中并改为传递它。

关于c++ - CreateProcess 异常 (kernel32.lib),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19928109/

相关文章:

c++ - 这是什么样的代码? C 或 C++

c++ - 有没有办法让 CreateProcess 创建的进程在另一个窗口中打开?

c - 在 Windows 上将命令通过管道传输到缓冲区

c++ - Windows:覆盖正在使用的文件

c++ - 长时间锁定互斥锁是否安全?

c++ - 在 qtablewidget 顶部插入行

c++ - 使用 ASSIMP 和 OpenGL 加载 3d 模型时纹理不正确

c - 将指针传递给嵌套函数

mysql - 连接CGI(C编程)与Mysql时出错

c - intel x86汇编到C