Thank you both for your helpful answers. I wrote the following, which works as well as we need it for now. Only our code will be calling this executable, and never with quotes, although that wouldn't be too hard to add. Also, it might not do well if there is more than one space between arguments, but again, we don't have to worry about other people using this program, it's just for academic purposes. If you think improvements are necessary, edit this post, and justify it in your comment.
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow ) {
assert(lpCmdLine != NULL);
int argc = 1, ret = 0;
std::vector<char*> args;
// Copy the entire array to a regular cstr
int cmdLineLen = _tcslen(lpCmdLine);
char *argv = new char[cmdLineLen];
wcstombs(argv,lpCmdLine,cmdLineLen);
args.push_back(&argv[0]);
// Replace spaces with nulls to effectively create array of cstr
for(int i=0; i<cmdLineLen; i++){
if(argv[i] == ' '){
argv[i] = '\0';
args.push_back(&argv[i+1]); // Keep track of the first char in each word
argc++;
}
}
// argv[argc] should be NULL.
args.push_back(NULL);
try{ // Run the program
ret = main(argc,&args[0]);
}
catch(...){
// TODO: Report error here. Commented code works OK for WinCE .NET
// delete argv;
// throw;
ret = -1;
}
delete argv;
return ret;
}
Also, for those interested, running this at the command line
>myprogam.exe -a shortargument -b -c
will put the following in lpCmdLine
"-a shortargument -b -c"
Also, my first guess was that argv needed to be delete[]'d (because I new char[]'d), but when I did this, the program had a fatal error. When I switched to the above, it worked. Doesn't that violate the new->delete/new[]->delete[] rule?