News for hackers. Stuff that matters.
Re:Where do argc and argv come from? by ChrisSD on Monday August 10, 2020 @02:33PM (#24112476)
In the Windows world there is no argc/argv. There is only the commandline string. The C runtime will parse this to emulate the required arguments.

The commandline is stored in the process environment block and can be set to anything when the process is created.

Re:Where do argc and argv come from? by sumtechguy on Monday August 10, 2020 @03:03PM (#24112797)
https://docs.microsoft.com/en-us/windows/win32/learnwin32/wi...

You then pass it into https://docs.microsoft.com/en-us/windows/win32/api/shellapi/... to get it split out if you need it.

I can see why they did it that way. No reason to waste cycles on something if you do not need it. Though most frameworks go and call it for you anyway these days.

Re:Where do argc and argv come from? by ChrisSD on Monday August 10, 2020 @03:42PM (#24113218)
I'd also add that CommandLineToArgvW doesn't do exactly the same thing that the C runtime does to parse arguments. They used to be the same but the C runtime was tweaked in 2008 whereas CommandLineToArgvW was kept the same for backwards compatibility. See http://daviddeley.com/autohotkey/parameters/parameters.htm#W...
Re:Where do argc and argv come from? by gpvos on Tuesday August 11, 2020 @02:51AM (#24117813)
Re:Where do argc and argv come from? by Varriount on Monday August 10, 2020 @08:08PM (#24115756)
Huh. Wouldn't this have caused inter-operability problems for programs that spawn other processes?
Re:Where do argc and argv come from? by ChrisSD on Monday August 10, 2020 @08:22PM (#24115865)
Presumably they would have tested extensively for regressions. But I'm not sure what would break that wasn't already broken. It was an obscure undocumented case that was not intuitive.
Re:Where do argc and argv come from? by ChrisSD on Monday August 10, 2020 @03:17PM (#24112945)
Note that WinMain isn't the "real" entry point either. The C runtime calls WinMain after doing its startup. By convention the entry point is one of:

* mainCRTStartup

* wmainCRTStartup

* WinMainCRTStartup

* wWinMainCRTStartup

Conveniently these all have the exact same function signature (i.e. the real entry point signature). For example:

    extern "C" int WinMainCRTStartup()
Although obviously the C runtime implementations differ for each of the listed entry points.
Re:Where do argc and argv come from? by sumtechguy on Tuesday August 11, 2020 @09:28AM (#24120053)
https://docs.microsoft.com/en-us/cpp/build/reference/entry-e...

You can also set what it is using that compiler option if you want. You usually do not want to as the default versions do quite a bit of stuff. I think the winmain one was the 3.x default. But changed when they added in other bits (I want to say threading, and console setup but I could be wrong).

Windows is a weird one as it has this odd DOS/VMS/OS2/Win16/32 heritage so entry into the program is one that has some bagage. Winmain is one of those leftovers. The params passed in need to use the functions associated to them.

My advice is always with this stuff. read the docs. There is a ton of info in there. It is kind of picky to get just right.

Re:Where do argc and argv come from? by FreeFull on Monday August 10, 2020 @03:16PM (#24112931)
It's a carryover from the MS-DOS days. One unfortunate part is that each program has to implement globs and any similar feature itself.
Re:Where do argc and argv come from? by frutiger on Monday August 10, 2020 @03:23PM (#24113006)
Even in a modern Unix, globs are expanded by the shell before invoking exec(3) with the individual arguments split out. They are not split/expanded by the C runtime.
Re:Where do argc and argv come from? by userbinator on Monday August 10, 2020 @03:45PM (#24113238)
...and I'm pretty sure DOS inherited it from CP/M.
Re:Where do argc and argv come from? by Someone on Monday August 10, 2020 @05:13PM (#24114059)
‘Implement’ isn’t necessary. That code could be provided by a shared library.

That could be the superior option. Moving command line expansion into a shared library gets rid of the problem where the command line with globs expanded is too long, could have standardized option handling earlier (that system-provided library for globbing could easily grow to handle option flags), could better handle file names containing spaces, etc.

Re:Where do argc and argv come from? by jussij on Tuesday August 11, 2020 @12:17AM (#24117112)
The Windows applications can still access this information using the __argc and __argv global variables:

https://docs.microsoft.com/en-us/cpp/c-runtime-library/argc-...

Re:Where do argc and argv come from? by int_19h on Tuesday August 11, 2020 @03:10AM (#24117911)
These also come from the C runtime, not from Windows itself.

And if you are building the app with the C runtime, you may as well just use regular main().

Re:Where do argc and argv come from? by kevin_thibedeau on Monday August 10, 2020 @02:59PM (#24112755)
You can make Windows pre-process the command line args and expand globs so that main() works like normal on Unix. It just isn't the default.
Re:Where do argc and argv come from? by ChrisSD on Monday August 10, 2020 @03:11PM (#24112880)
The shell can do whatever processing it likes but at the end of the day all that's passed to the application is a string. The string may or may not be what a user typed but the application has no knowledge of that.
Re:Where do argc and argv come from? by kevin_thibedeau on Monday August 10, 2020 @03:36PM (#24113156)
It isn't done by the shell on Windows.
Re:Where do argc and argv come from? by ChrisSD on Monday August 10, 2020 @03:50PM (#24113283)
The shell is whatever you use as a shell. Be that cmd, powershell or your own invention. My point is that it doesn't matter. The shell can process the command line however it likes but all the application receives is a single string.

The application can then do whatever it likes with the string. Sure you can use an entry point that parses the arguments in different ways but that's still the application parsing the string. You're just using a library function to do the parsing instead of doing it manually.

At no point does "Windows" do anything. As far as its concerned it's just a string being passed in at process creation.

Re:Where do argc and argv come from? by mortehu on Monday August 10, 2020 @04:48PM (#24113810)
If it's not the shell and not the application, are you saying there's a kernel setting that enables glob expansion?

I suspect you're really talking about linking to setargv.obj?

Re:Where do argc and argv come from? by kevin_thibedeau on Monday August 10, 2020 @05:31PM (#24114228)
You link an alternate library into the application and it will expand args for you.