I need to pass command line arguments from A.exe to B.exe. If A.exe with multi-args like
A.exe -a="a" -b="b"'
and I can use
BeginProcess("B.exe", **args!**)
to start B.exe. How can I get the raw command line arguments like
'-a="a" -b="b"'
If you are on Windows, you use GetCommandLine to get the raw command line.
Note that GetCommandLine also includes argv[0]. So you will have to go beyond argv[0] from the output of GetCommandLine before passing it to B.
This is some non-error checked code to do that
#include <string.h>
#include <windows.h>
#include <iostream>
#include <ctype.h>
int main(int argc, char **argv)
{
LPTSTR cmd = GetCommandLine();
int l = strlen(argv[0]);
if(cmd == strstr(cmd, argv[0]))
{
cmd = cmd + l;
while(*cmd && isspace(*cmd))
++cmd;
}
std::cout<<"Command Line is : "<<cmd;
}
When I run the above program as A.exe -a="a" -b="b"
, I get the following output
A.exe -a="a" -b="b"
Command Line is : -a="a" -b="b"
lpApplicationName
argument, the executable specified in the lpCommandLine
argument is ignored.
Apr 26, 2016 at 21:26
Here is the one and only correct way to skip the executable name, based on Wine's implementation of CommandLineToArgvW:
char *s = lpCmdline;
if (*s == '"') {
++s;
while (*s)
if (*s++ == '"')
break;
} else {
while (*s && *s != ' ' && *s != '\t')
++s;
}
/* (optionally) skip spaces preceding the first argument */
while (*s == ' ' || *s == '\t')
s++;
Note! Current Wine implementation, as of Feb 19 2'20 - git commit a10267172
, is now moved from dlls/shell32/shell32_main.c
to dlls/shcore/main.c
.
The standard definition of main
is
int main(int argc, char* argv[])
The argv
variable contains the command-line arguments. The argc
variable indicates how many entries in the argv
array are used.
strcat()
function.
Jan 4, 2013 at 2:51
std::string(argv[1])+argv[2]
preserves whatever spaces are in the arguments. Do you mean the new process can't reliably separate the arguments again? If so, that was exactly my criticism in the comments to PotatoSwatter's answer ... passing argv
preserves individual arguments, concatenating and resplitting doesn't.
Jan 7, 2013 at 14:28
The raw string typed into the shell is converted by the shell into argv
before your program begins running. I've never heard of an operating system or shell providing a "raw" command-line in addition to argv
.
What if the user used quotes to pass a space character into your arguments? What if they used a backslash to escape a quote inside the quotes? Different shells may even have different quoting rules.
If you have a list like argv
, you should try to find an API that accepts that rather than attempting to implement string processing which is only auxiliary to the actual goal. Microsoft is serious about security and they certainly provide something that doesn't require adding a security hole to your application.
I can't find documentation about any C/C++ API BeginProcess
; I'm kind of assuming this is Windows but in any case you should double check your platform's reference manual for an alternative system call.
argv
. Win32 provides it via GetCommandLine
(and presumably you have to parse it yourself ... eurgh! how primitive!) I assume BeginProcess
is meant to be CreateProcess
Jan 4, 2013 at 3:13
execv()
family of functions allow you to pass an array of arguments that are already split into separate words, which is much safer (no quoting or escaping problems possible.) It was a solved problem long before Windows existed.
Jan 4, 2013 at 3:25
If you're on Windows, I believe the correct solution is to call GetCommandLine() to get your full commandline, and then PathGetArgs(CommandLine) to remove arg0 (your exe path) from the beginning.
This is how I turn the command line back into shell args. Sometime this is nice to echo into an output file, to save "what arguments were used" along with the output. The escaping is rudimentary, and sufficient for most situations.
I started the output at the command (i=0). You can change to (i=1) if you want arguments only, etc.
//you have to free() the result!, returns null if no args
char *arg2cmd(int argc, char** argv) {
char *buf=NULL;
int n = 0;
int k, i;
for (i=0; i <argc;++i) {
int k=strlen(argv[i]);
buf=( char *)realloc(buf,n+k+4);
char *p=buf+n;
char endq=0;
// this is a poor mans quoting, which is good enough for anything that's not rediculous
if (strchr(argv[i], ' ')) {
if (!strchr(argv[i], '\'')) {
*p++='\'';
endq='\'';
} else {
*p++='\"';
endq='\"';
}
}
memcpy(p, argv[i], k);
p+=k;
if (i < (argc-1)) *p++=' ';
if (endq) *p++=endq;
*p='\0';
n = p-buf;
}
return buf;
}
And a simple cpp wrapper:
std::string arg2string(int argc, char **argv) {
char *tmp=arg2cmd(argc, argv);
std::string ret=tmp;
free(tmp);
return ret;
}