错误“<url>未被识别为内部或外部命令,可运行的程序或批处理文件。”

时间:2016-06-09 14:55:12

标签: c++ runtime-error

我的代码应该接受参数,在它们之间添加“+”,并用此搜索谷歌浏览器,但是我得到错误(命令行参数=“堆栈溢出站点”):

  

http://www.google.com/search?q=Stack+Overflow+Site'C:\ Program'不是   被认可为内部或外部命令,可操作程序或   批处理文件。

同样在我的程序中,我收到此错误:

  

错误C4996:'strcpy':此函数或变量可能不安全。   请考虑使用strcpy_s。要禁用弃用,请使用   _CRT_SECURE_NO_WARNINGS。详细信息请参见在线帮助。 c:\ users \ user \ documents \ visual studio   2013 \项目\ PROJECT1 \ PROJECT1 \ main.cpp中   我一直忽视这一点,因为我认为这只是一个警告,但我不确定它是否相关。

我的代码:

#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();
}

1 个答案:

答案 0 :(得分:0)

我怀疑你没有得到完全相同的错误 在使用之前停止删除命令行缓冲区,但是 一些不同的,同样意想不到的结果。

如果您阅读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"

导致与 Way 1

相同的过程

方式3

lpApplicationName = "\path\to\executable" 
lpCommandLine = NULL

导致使用命令\path\to\executable启动进程。

您没有使用 Way 1 Way 2 Way 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"

当然,这没有预期的结果。

如果您希望通过采用 Way 2 来纠正这一点,请特别注意 请记住文档中有关lpCommandLine的内容:

  

...   如果lpApplicationName为NULL,则命令行的第一个以空格分隔的标记指定模块名称。   ...