简体   繁体   English

如何使用C ++执行极长的命令?

[英]How to execute extreme long command with C++?

I am trying to execute quite long external command (curl with long parameters) in my C++ program. 我试图在我的C ++程序中执行相当长的外部命令(带有长参数的卷曲)。

Works fine on Linux, but Windows (under Cygwin) always shows "System cannot execute specified program" if "cmd" is longer than 127 chars. 在Linux上可以正常运行,但是如果“ cmd”长于127个字符,则Windows(在Cygwin下)始终显示“系统无法执行指定的程序”。

Linux accepts with same code almost unlimited size. Linux接受几乎相同大小的相同代码。 Tried changing size of buffer, no change. 尝试更改缓冲区的大小,无变化。

Used this code: 使用此代码:

#include <string>
#include <iostream>
#include <stdio.h>

std::string exec(char* cmd) {
    FILE* pipe = _popen(cmd, "r");
    if (!pipe) 
        return "ERROR";

    char buffer[128];
    std::string result = "";
    while(!feof(pipe)) {
        if(fgets(buffer, 128, pipe) != NULL)
            result += buffer;
    }
    _pclose(pipe);
    return result;
}

Is that Windows specific limitation, or is possible somehow to extend the cmd buffer? 这是Windows的特定限制,还是可以通过某种方式扩展cmd缓冲区?

Added how is created char* which I am trying to send: 添加了如何创建要发送的char *:

   string message="This is message to send.";
   std::string send="";
   std::string curl_header="curl -s -F \"token=aE71iieVt8G1RWdJb59qmAa3hbxxxx\" -F \"user=uPKYQyNgGURbH1g56nBnjn9jNsxxxxx\" -F \"message=";
   send.append(curl_header);
   send.append(message);
   send.append("\" https://api.pushover.net/1/messages.json");
   cout << "\n";
   cout << "FULL STRING WITH PARAMS TO EXECUTE:";
   std::cout << send << '\n';
   char *cstr = new char[send.length() + 1];
   strcpy(cstr, send.c_str());
   cout << "FULL CHAR ARRAY WITH PARAMS TO EXECUTE:";
   std::cout << cstr << '\n';//this returns correct string, even longer than 127 chars
   cout << exec(cstr);// this returns error, same with system() if cstr is longer than 127 chars
    delete [] cstr;

Have you tried using system? 您是否尝试过使用系统?

#include <iostream>

using namespace std;

int main(int argc,char** args){

if(argc<2){
    system("run run run run run run run run run run runr urn runrunrunru unr unrunr urn run runr unr urn run runrun runr unr urnrun runr urnurnrunrunrunrunrunrurn urnurnunrunrunrunrunrunrn u unru ru runr ur urn ru run rururnunrunrunr");
}else{
    printf("argc: %d",argc);
}
system("pause");
}

This seems to work fine to me. 这似乎对我来说很好。 The program is named run, so it runs itself with that huge argument and then prints out the number of arguments it sees. 该程序名为run,因此它使用巨大的参数运行自身,然后输出看到的参数数量。 Is this what you are trying to do? 这是您要做什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM