创建进程 API 失败,在窗口 122 上出现错误代码 10
CreateProcess api failing with error code 122 on windows 10
我正在使用CreateProcess api来启动批处理文件。该代码在Windows 7上运行良好,但在Windows 10上失败。 下面是代码片段:
CString param; //it holds the very long string of command line arguments
wstring excFile = L"C:\program files\BatchFile.bat";
wstring csExcuPath = L"C:\program files";
wstring exeWithParam = excFile + _T(" ");
exeWithParam = exeWithParam.append(param);
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
TCHAR lpExeWithParam[8191];
_tcscpy_s(lpExeWithParam, exeWithParam.c_str());
BOOL bStatus = CreateProcess(NULL, lpExeWithParam, NULL, NULL, TRUE, CREATE_NEW_CONSOLE | CREATE_BREAKAWAY_FROM_JOB, NULL, csExcuPath.c_str(), &si, &pi);
DWORD err;
if (!bStatus)
{
err = GetLastError();
}
使用上面的代码,它正在调用一个批处理文件,该文件将使用给定的参数启动可执行文件。此代码不仅适用于我们产品中的 Windows 10。 GetLastError 返回错误代码 122,该代码表示错误"传递给系统调用的数据区域太小"。如何找出导致此错误的原因以及如何解决?
但是,在示例测试应用程序中使用相同的代码时,不会给出任何错误和通过。 任何线索/提示导致它在 Windows 10 上失败。
您需要以.bat
文件作为参数执行cmd.exe
,不要尝试直接执行.bat
。
另外,你不需要lpExeWithParam
,你可以exeWithParam
直接传递给CreateProcess()
。
尝试更多类似的东西:
CString param; //it holds the very long string of command line arguments
...
wstring excFile = L"C:\program files\BatchFile.bat";
wstring csExcuPath = L"C:\program files";
wstring exeWithParam = L"cmd.exe /c "" + excFile + L"" ";
exeWithParam.append(param);
STARTUPINFOW si = { sizeof(si) };
PROCESS_INFORMATION pi = {};
BOOL bStatus = CreateProcessW(NULL, &exeWithParam[0]/*or exeWithParam.data() in C++17*/, NULL, NULL, TRUE, CREATE_NEW_CONSOLE | CREATE_BREAKAWAY_FROM_JOB, NULL, csExcuPath.c_str(), &si, &pi);
if (!bStatus)
{
DWORD err = GetLastError();
...
}
else
{
...
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
错误 122 等同于ERROR_INSUFFICIENT_BUFFER,我认为这里的线索是"它包含很长的命令行参数字符串"。
到底有多长? Windows 10 的限制可能更低 - 我建议您进行实验(二进制印章(。
此外,CreateProcess
的文档指出您必须显式启动cmd.exe
才能运行批处理文件,因此我想您应该按照它所说的去做。
我认为要运行批处理文件,您必须将lpApplicationName设置为cmd.exe并将lpCommandLine设置为以下参数:/c加上批处理文件的名称
相关文章:
- 错误处理.将系统错误代码映射到泛型
- 为什么我在使用void函数时得到错误代码C2276
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- 逻辑运算符上出现错误代码 a')'
- 'val' Arduino 错误代码之前的预期'('
- 我在贪吃蛇游戏中收到了错误代码 -1073741571
- 根据 GetLastError 直接写入磁盘会导致错误代码 5
- [longjmp/setjmp]c++ 相同的代码窗口有异常 Linux 没有错误并且运行良好
- 当我选择大于 720 的矩阵大小时,程序退出并显示错误代码.可能是什么原因?
- 创建进程 API 失败,在窗口 122 上出现错误代码 10
- 使用另一个函数调用一个函数(都在类中)时出现问题.没有错误代码C++
- 当命令失败时,gzip 会在 C++ 中返回错误代码吗?
- 从 C++ 调用 cURL 命令会返回意外的错误代码,如 1792 和 6656
- WSALookupServiceBegin() 上的错误代码 10022(无效的 arugment)
- C++ Schannel POST 400 错误代码错误请求
- openCL-创建子缓冲区返回错误代码13
- 更新了Runge-Kutta(RK4)C++错误代码中的二阶DE
- 获取有关使用未初始化内存的错误代码
- 排序程序的意外错误代码
- 正在将DJI错误代码记录到流中