Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ArgvToCommandLine, the reverse of CommandLineToArgvW #499

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

dunhor
Copy link
Member

@dunhor dunhor commented Jan 22, 2025

A separate project I was working on required this functionality; thought I'd copy it here. The general gist behind its purpose is that CreateProcess* requires a string for the command line as opposed to an argv array; when you're in a scenario where you have an argv array and need to call CreateProcess*, this function is useful. The documentation for the function mentions two specific scenarios in which this is useful, but I'm copying it here for quick reference:

  1. When implementing a "driver" application. That is, an application that consumes some command line arguments, translates others into new arguments, and preserves the rest, "forwarding" the resulting command line to a separate application.
  2. When reading command line arguments from some data storage, e.g. from a JSON array, which then need to get compiled into a command line string that's used for creating a new process.

Note that the documentation for CommandLineToArgvW describes how arguments are parsed; this just does the reverse. The goal of this function is to ensure that the target application's argv array is identical to the one passed into this function. This is potentially problematic if the target application does not use its argv array (e.g. if it gets the command line string directly, e.g. with GetCommandLineW) and instead attempts to perform its own command line processing, which may not handle quotes, etc. identically to CommandLineToArgvW. One notable example is cmd.exe which does not handle some quoted arguments as one would logically expect. For example, cmd.exe "/c" "echo foo" does not behave as you would expect it to:

>cmd "/c" "echo foo"
'"echo foo' is not recognized as an internal or external command,
operable program or batch file.

This is the motivating factor behind ArgvToCommandLineFlags::ForceQuotes. Ideally, we would always surround all arguments with quotes as that's the more efficient thing to do, however as you can see from the above, this is not always safe to do.

include/wil/win32_helpers.h Outdated Show resolved Hide resolved
include/wil/win32_helpers.h Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant