Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper use of CommandLineToArgvW?

Tags:

c++

winapi

When parsing the command line arguments of a process whose path contains a space, the path is split up into multiple pieces by CommandLineToArgvW.

There exists a folder on all Windows' C: drives called "Program Files."

...wat

The MSDN page mentions nothing about this being a problem. Am I just using the function wrong? How am I supposed to predict what words are part of the path otherwise?

like image 893
NmdMystery Avatar asked Sep 15 '25 13:09

NmdMystery


1 Answers

CommandLineToArgvW splits the line on spaces - if you need an argument (or the program name) to contain spaces you need to surround it with quotation marks.

  • C:\Program Files - argv[0] = C:\Program, argv[1] = Files
  • "C:\Program Files" - argv[0] = C:\Program Files

Note that you can get the file path of your process using GetModuleFileName - you can do this to determine whether the path contains a space, and insert the quotes in the command line if needed before parsing it.

like image 149
Jonathan Potter Avatar answered Sep 17 '25 06:09

Jonathan Potter