-3

i would like to give my file a default argv, if none is given in the console. It doesn't work cause of duplicate parameter names. if no parameter is given, i would like to use a fixed filename in the same folder.

int main(int argc, char* argv[], char* argv[1]="test.ps1")
{
    std::string target = _T(argv[1]);
    std::string temp= std::string("powershell.exe -command \"")+target +std::string("\"");

ShellExecuteA(0, "runas", "powershell.exe", temp.c_str, "", SW_HIDE);
}
roalz
  • 2,699
  • 3
  • 25
  • 42
Marabunta
  • 45
  • 1
  • 7

1 Answers1

4
int main(int argc, char **argv) {
  std::string const default_file = "test.ps1";
  std::string file = (argc < 2) ? default_file : argv[1];

  doSomethingWithFile(file);
}
Paul R
  • 208,748
  • 37
  • 389
  • 560
bolov
  • 72,283
  • 15
  • 145
  • 224
  • Didnt work: C:\Scripts>CreateProcess.exe "C:\Scripts\test.ps1" argc = 2 argv[0] = C:\Scripts\CreateProcess.exe argv[1] = C:\Scripts\test.ps1 And if there is no param given: CreateProcess.exe argc = 1 argv[0] = C:\Scripts\CreateProcess.exe – Marabunta Jan 26 '15 at 22:53
  • @Marabunta I don't understand what didn't work and what that text you posted is. – bolov Jan 26 '15 at 22:56
  • You made me doubt myself. I just compiled and tested it. It works. – bolov Jan 26 '15 at 23:05
  • ok actually it did work since "file" is test.ps1 while executing, but in my command i somehow have to write the fullpath to the file. how would i use GetCurrentDirectory() or anything else so i can add this to default_file? – Marabunta Jan 26 '15 at 23:06
  • 1
    @Marabunta c'mon this is trivial: `GetCurrentDirectory()` into a variable `path` and then concatenate `path` with `default_file`. If you still have problems, post another question, but I would suggest not giving up so easily and work on it until you manage to make it work. It is not that hard. – bolov Jan 26 '15 at 23:19