Beginner C++ CreateProcess () Error 2
Beginner C++ CreateProcess () Error 2
我正在尝试创建一个发送命令到cmd.exe并接收错误2的进程,为什么?这是可能吗?如何?
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
String pathexe = "C:Windowssystem32cmd.exe";
String command= "notepad.exe";
if(!CreateProcess(
pathexe.c_str(), // lpApplicationName
command.c_str(), // lpCommandLine
NULL, // lpProcessAttributes
NULL, // lpThreadAttributes
FALSE, // bInheritHandles
0, // dwCreationFlags
NULL, // lpEnvironment
NULL, // lpCurrentDirectory
&si, // lpStartupInfo
&pi // lpProcessInformation
))
{
AnsiString error = GetLastError();
ShowMessage("Error: " + error);
}
WaitForSingleObject( pi.hProcess, INFINITE );
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
PD: 1)假设您可以使用CreateProcess()来实现此目的,我不应该使用ShellExecute()或system()。2)我在论坛上读到过,找不到解决这个错误的方法,类似的问题有很多答案,但没有解决这个错误,提出了其他功能,或者与route命令混合。3)我认为这个问题不允许,因为我在舱单上建造。4)我目前使用c++ Builder,在win7, 32位,但不重要。5)我猜这个问题将被投票为负面和重复(像往常一样),但提议的测试示例也会收到错误。Thanks to all
第一个结论:
错误2:The system cannot find The file specified.
链接功能:https://msdn.microsoft.com/es-es/library/windows/desktop/ms679360(v=vs.85).aspx链接错误:https://msdn.microsoft.com/es-es/library/windows/desktop/ms681382(v=vs.85).aspx
与错误2:检查语法,文件路径和存在。
:
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
String command = "notepad.exe";
if(!CreateProcess(
NULL, // lpApplicationName
commmand.c_str(), // lpCommandLine
NULL, // lpProcessAttributes
NULL, // lpThreadAttributes
FALSE, // bInheritHandles
0, // dwCreationFlags
NULL, // lpEnvironment
NULL, // lpCurrentDirectory
&si, // lpStartupInfo
&pi // lpProcessInformation
))
{
AnsiString error = GetLastError();
ShowMessage("Error: " + error);
}
WaitForSingleObject( pi.hProcess, INFINITE );
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
这个例子也适用于exe
String command = "cd C:\sample\calc.exe";
但是不能使用cmd的通用命令,必须有一种方法将命令发送给cmd,如:
notepad.exe && cd C:sample && sample1.txt
THANKS TO ALL
您正在尝试运行这个命令:
cmd notepad
(你也做得不对;lpCommandLine参数应该包括整个字符串,而不仅仅是notepad
,并且您没有正确地引用反斜杠。
但是即使你解决了这些问题,它也不会工作,因为你的语法是错误的。你会发现如果在命令行上输入它也不会起作用!
相反,尝试:
String pathexe = "C:\Windows\system32\cmd.exe";
String command= "cmd /c notepad.exe";
/c
选项表示"执行此命令"。如果您希望命令窗口在命令完成后保持打开状态,则可以使用/k
,尽管程序很少这样做。
最后一个注意事项:我在这里假设notepad
只是一个更复杂的命令的替代品。如果你真的想运行记事本,或任何其他可执行文件,你不应该调用cmd.exe
:
String command= "notepad";
if(!CreateProcess(
NULL, // lpApplicationName
command.c_str(), // lpCommandLine
...
只有在需要运行内置命令或复合命令行时才需要调用cmd.exe
。
(实际上,即使在这些情况下,调用cmd.exe
也被认为是不好的做法;在Windows中,通常期望您通过API为自己做这类事情,而不是将工作外包给命令解释器。但也有一些极端情况,您的情况可能会有所不同。
- "error: no matching function for call to"构造函数错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 使用 LuaBridge 将 LuaJIT 绑定到C++会导致"PANIC: unprotected error"
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- C++错误:"error: int aaa::bbb is protected within this context"
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- 使用 cmake 的 LLVM 构建在 tsan_libdispatch_mac.cc 期间失败; "Error: conflicting types for ..."
- C++ "error: invalid use of void expression"
- struct.error:解压缩 C++ 结构时,解包需要 288 字节的缓冲区
- 如何修复"error: ‘_1’ was not declared in this scope"?
- 从不同目录执行时"symbol lookup error:",否则从构建目录执行时按预期工作
- 对象声明中的模板"error: expected expression"
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- OpenCV 3.4.1 error readNetFromTensorflow 无法在 cv::d nn::ReadProtoFromBinaryFile 中打开 .pb
- 已解决 - C++ 的崇高文本中的"fatal error: opencv: no such file or directory"
- 为什么我会"error: expected expression"?
- CMake Error at modules/videoio/cmake/detect_ffmpeg.cmake:16
- 如果两个静态库使用相同的另一个静态库,如何避免"LNK2005 Already Defined error"?
- 在 Objective-C++ -> EXC_BAD_ACCESS Error 中使用"new"关键字