If you’re writing ultra-small Windows utilities—like tiny launchers or wrappers—you may want to avoid both the C runtime (CRT) and the C++ runtime. That means no printf, no strlen, and no mainCRTStartup scaffolding. The snippet below shows a practical pattern: advance a command-line pointer past argv[0] and then print the progressively “shifted” command line, all using only Win32 APIs.
-
ShiftCmdline(no CRT / no C++ runtime): Walks through the process command line returned byGetCommandLineA()and returns a pointer to the first character of the next token afterargv[0]. It accurately follows Windows quoting rules:- Tracks whether we’re inside a double-quoted section.
- Handles backslash escaping before a quote (
\"), including doubled backslashes. - Detects the end of
argv[0]at the first unquoted space or tab. - Stops at
'\0'if the command line ends.
-
WriteLineA: A tiny helper to write ASCII text to stderr usingWriteFile. It computes the string length manually and appends\r\n. No CRT involved. -
main:- Gets the full command line with
GetCommandLineA(). - Prints the original line.
- Repeatedly calls
ShiftCmdlineand prints the shifted view until the string ends. This is a convenient way to visualize how Windows tokenization would move through arguments.
- Gets the full command line with
- No CRT dependency: Reduces binary size and avoids pulling in
ucrtbase.dll/msvcp*.dll. Useful for tiny tools, recovery environments, or specialized launchers. - Accurate Windows parsing: Windows command-line parsing is quirky (quotes, backslashes, spaces). This routine mirrors the core behavior closely enough for most launcher tasks.
- Pointer math, not copies: It avoids allocations or token splitting—just returns a pointer into the existing command line buffer.
- Inside quotes:
A
"toggles quote state unless it’s escaped by a preceding backslash. The code tracks abackslashPrecedingflag to decide whether to treat"literally or as a delimiter. - Outside quotes:
The first unquoted space/tab marks the end of
argv[0]. Subsequent leading spaces/tabs are skipped to land on the next argument. - Termination:
On
'\0', the routine returns a pointer to the terminator—callers can detect end-of-line immediately.
- Compile with options that omit CRT startup if you want a truly minimal artifact (e.g., custom entry point and
ExitProcess), or keep a normalmainas shown. - Replace any
printf/putscalls withWriteFileto standard handles obtained viaGetStdHandle.
- Wrapper executables: Strip
argv[0]and forward the remainder to another process without paying CRT costs. - Argument peeking: Inspect or log raw user arguments as Windows sees them.
- Tiny utilities: Ideal for bootstrappers, installers, or shims where dependency footprint matters.
This pattern keeps your executable lean while respecting Windows’ real-world command-line rules—no CRT required.