-1

I want to do something like that:

int main(int argc, char* argv[]) {

    string input = string(argv[1]);

    // some code to run MyScript.vbs with arg=input

    return 0;
    }

What should I write in the third line of that code to run the script with arg from my program input?

  • 1
    [`system`](https://en.cppreference.com/w/cpp/utility/program/system) function? Possibly not the best option, but probably easiest to implement... – Aconcagua Sep 11 '18 at 08:17

1 Answers1

0
#include <cstddef>
#include <stdlib>
#include <string>

#include <windows.h>

int main(int argc, char* argv[]) {

    if (argc <= 1)
        return EXIT_FAILURE;

    std::string parameters;

    for (std::size_t i{ 1 }; i < argc; ++i)
        parameters += argv[i];

    CreateProcessA("wscript.exe", const_cast<LPSTR>( parameters.c_str() ), nullptr,
        nullptr, false, NORMAL_PRIORITY_CLASS, nullptr, nullptr, nullptr, nullptr);
}

but that's somewhat pointless. Just call wscript.exe directly.

Swordfish
  • 12,971
  • 3
  • 21
  • 43
  • With that code I get the problem: _It is not allowed to use usual pointers to reference classes or C ++ / CLI interface classes_ – Anton Ryabtsev Sep 11 '18 at 08:26
  • Then you are not writing in C++. – Swordfish Sep 11 '18 at 08:30
  • @kosmo_tony C (style) variant: Allocate a large enough char array (possibly of size 1024: `char buffer[1024];`) and concatenate the arguments into this one. Make sure that you don't exceed array bounds, though (use [`strncat`](https://en.cppreference.com/w/c/string/byte/strncat), not `strcat`)! – Aconcagua Sep 11 '18 at 08:33
  • 1
    @Aconcagua The question is tagged C++ and vbscript which implies Windows so chances of not being able to use a full implementation of Standard C++ and the Windows API is negligible. – Swordfish Sep 11 '18 at 08:41
  • @Aconcagua the error message kosmo_tony got ("It is not allowed to use usual pointers to reference classes or C ++ / CLI interface classes") disqualifies for C++. – Swordfish Sep 11 '18 at 08:49
  • @Swordfish "error"? Didn't interprete the sentence as such, but as being a citation from the task... OK, let's forget about then... – Aconcagua Sep 11 '18 at 08:52