Lawrence D'Anna
Lawrence D'Anna

Reputation: 3124

How does UCRT parse command line arguments?

When using msvcrt, argv agrees with CommandLineToArgvW.

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shellapi.h>
#include <stdio.h>


int main (int argc, char **argv) { 
    wchar_t * cmdline = GetCommandLineW();
    wprintf(L"cmdline: %ls\n\n", cmdline);

    for (int i = 0; i < argc; i++) { 
        printf("argv[%d]: %s\n", i, argv[i]);
    }

    printf("\n");

    int wargc;
    wchar_t **wargv = CommandLineToArgvW(cmdline, &wargc);
    for (int i = 0; i < wargc; i++) { 
        wprintf(L"CommandLineToArgvW(cmdline)[%d]: %ls\n", i, wargv[i]);
    }

    return 0;
}
z:\src\mslex>.\args_msvcrt.exe """""
cmdline: .\args_msvcrt.exe  """""

argv[0]: .\args_msvcrt.exe
argv[1]: "

CommandLineToArgvW(cmdline)[0]: .\args_msvcrt.exe
CommandLineToArgvW(cmdline)[1]: "

But, with UCRT, they can disagree:

z:\src\mslex>.\args_ucrt.exe """""
cmdline: .\args_ucrt.exe  """""

argv[0]: .\args_ucrt.exe
argv[1]: ""

CommandLineToArgvW(cmdline)[0]: .\args_ucrt.exe
CommandLineToArgvW(cmdline)[1]: "

What function is UCRT using to parse command line arguments?

Can anyone point me to documentation that explains the difference, or even better, a description or source code of the algorithm UCRT is using.

Thank you.

edit: This document seems to explain it well enough to write an equivalent parser. It would still be nice to be able to call the actual function though.

Upvotes: 1

Views: 67

Answers (0)

Related Questions