错误“未被识别为内部或外部命令,可操作程序或批处理文件”。
我的代码应当采取论证,把“+”其间他们,和搜索谷歌浏览器这一点,但我得到的错误(命令行参数=“堆栈溢出网站”):错误“<url>未被识别为内部或外部命令,可操作程序或批处理文件”。
http://www.google.com/search?q=Stack+Overflow+Site 'C:\Program' is not recognized as an internal or external command, operable program or batch file.
而且在我的计划我得到这个错误:
error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. c:\users\user\documents\visual studio 2013\projects\project1\project1\main.cpp I have been ignoring this, because I thought it was just a warning, but I'm not sure if it is relevant.
我的代码:
#include <Windows.h>
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char** argv){
//Loop through arguments and put a "+" between them.
string out = "";
for (int i = 1; i < argc; ++i) {
if (i != 1){
out += "+";
}
out += argv[i];
}
string newout = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe \"http://www.google.com/search?q=" + out + "\"";
// set the size of the structures
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
//set y to newout and convert
char *y = new char[newout.length() + 1];
strcpy(y, newout.c_str());
//Run Google Chrome with argument
CreateProcessA("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe", // the path
y, // 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
);
delete[] y;
cin.ignore();
}
我怀疑你,因为你没有得到完全同样的错误 停止在使用它之前删除命令行缓冲区,但有些不同的,同样出乎意料的结果。
如果你愿意读the documentation of CreateProcess
你将学到的参数:
_In_opt_ LPCTSTR lpApplicationName,
_Inout_opt_ LPTSTR lpCommandLine,
可以通过以下三种方式之一提供,如图所示: -
路1
lpApplicationName = "\path\to\executable"
lpCommandLine = "args for the executable"
这会导致用命令启动的进程:
\path\to\executable args for the executable
方式2
lpApplicationName = NULL
lpCommandLine = "\path\to\executable args for the executable"
其导致相同的处理方式1种
方式三
lpApplicationName = "\path\to\executable"
lpCommandLine = NULL
这导致与发起的过程公司命令\path\to\executable
。
您没有使用路1或路2或路3,但:
lpApplicationName = "\path\to\executable"
lpCommandLine = "\path\to\executable args for the executable"
这与命令启动了一个进程的情况下的结果:
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe \
C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe \
http://www.google.com/search?q=Stack+Overflow+Site"
其中传递给Chrome的参数是:
C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe \
http://www.google.com/search?q=Stack+Overflow+Site"
当然这并没有预期的结果。
你应该希望通过采用路2纠正这一点,牢记特别 什么文件说,大约lpCommandLine
:
... If lpApplicationName is NULL, the first white space–delimited token of the command line specifies the module name. ...
的** **相关代码的部分需要在这里,在问题本身,而不是异地。如果相关详细信息不在现场,并且该非现场位置不可用(移动,删除或脱机),则您的问题对于未来的读者来说是零价值。请注意强调**代码的相关部分**。 –
删除它后使用'y',这样做很糟糕。删除必须在完成使用后。还要注意,使用'CreateProcessA'更好地显式使用ANSI字符集版本。 – MikeCAT
@MikeCAT,我已经做出了你所建议的改变,但我的代码中仍然有相同的错误。 – Melkor