I am trying to use wget for windows from a c++ program to download 6 files from a website and concatenate the results as one file. This is not problem from a batch file. The command would just be:
wget -O bits.bin --wait=20 --random-wait --user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0" "http://url/to/the/file1" "http://url/to/the/file2" "http://url/to/the/file3" "http://url/to/the/file4" "http://url/to/the/file5" "http://url/to/the/file6"
This is tested and works as expected when executed from a .bat file. Using --output-file= instead of -O also works from a batch file.
But when I try to call wget with CreateProcessA() those first options are interpreted as URLs. wget tries to download bits.bin for example as an ftp address. It ends up ignoring these nonsensical URLs and just downloading and storing 6 separate files as [URL1], [URL2].1, [URL3].2 etc with long ugly url-as-names.
I think maybe the way CreateProcess presents params to the called program is not compatible with how wget for windows is expecting them, but I don't really understand why there should be any difference.
Note that the parameter string is almost 900 characters long because the 6 URLs are long. So this somewhat limits my options I think. I cannot show the actual URLs because they include passwords and other personal data.
I've tried adding quotes to different parts of the param string with no luck. If anything it seems to make things worse. I use sprintf because in the actual code I have to insert a passkey and other options into each URL at runtime.
For now, unless someone here has a solution, I am giving up on using CreateProcess() to run wget. Next I will try ShellExecute() and then if necessary System(). Wondering if MAXPATH may become a problem though. I may have to call wget 6 times instead of just once and manage the random delays myself. I just tried:
ShellExecuteA(NULL, NULL,"wget.exe", params, NULL, SW_SHOW);
It popped up a confirmation diolog box and when I allowed it to run wget did not seem to actually do anything anyway. Maybe System() will be my only option.
Note: CreateProcess() is working now that I have included wget.exe in the second parameter too. Thanks guys. Weird how ShellExecute seemed not to work at all, but I guess it doesn't matter. Maybe it was some implementation detail like MAX_PATH limitations.
I am aware that I can set the first param in CreateProcess to NULL and include the program name in the second CreateProcess() param but then I introduce potential MAX_PATH problems as well. Although I suppose this is no worse than ShellExecute(). I was hoping to take advantage of the generous 32768 char limit of CreateProcess params.
char params[1024];
sprintf(params,"--output-file=bits.new --wait=20 --random-wait --user-agent=\"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0\" \"url/to/file1\" \"url/to/file2\" \"url/to/file3\"");
STARTUPINFOA startstruct = {0};
startstruct.cb = sizeof(startstruct);
PROCESS_INFORMATION procinfo;
CreateProcessA("wget.exe",params,0,0,0,CREATE_NEW_CONSOLE,0,0,&startstruct, &procinfo);
CreateProcess. Ifcmd.execan invokewgetas you want it done, then you can do the same. It may not be easy, but it's certainly possible nonetheless. - Jerry Coffin