Author Topic: Backslash in quoted command line  (Read 1458 times)


  • Guest
Backslash in quoted command line
« on: May 16, 2012, 06:25:35 pm »
The interpretation of command line parameters in the ide does not match the cmd interpretation:

This is the test code:
Code: [Select]
#define UNICODE
#define _UNICODE

#include <windows.h>
#include <wchar.h>
#include <stdio.h>
#include <shellapi.h>

int __cdecl wmain(void)
LPWSTR *szArglist;
    int nArgs;
    int i;

printf("%ls\n", GetCommandLineW());
    szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
    if( NULL == szArglist )
      wprintf(L"CommandLineToArgvW failed\n");
      return 1;
    else for( i=0; i<nArgs; i++) printf("[%d] [%ls]\n", i, szArglist[i]);



This is a test call with 4 parameters.

Code: [Select]
D:\C\CMDLIN~1>cmdlinetst2 /1 "c:\pa\"th with blanks"\ /3 /4

The second parameter should have

a literal doublequote after the first 'a'. This has to be protected with the backslash


a backslash after the closing doublequote, to get a literal backslash at the parameters end.

If I execute this from CMD, I get:
Code: [Select]
cmdlinetst2 /1 "c:\pa\"th with blanks"\ /3 /4
[0] [cmdlinetst2]
[1] [/1]
[2] [c:\pa"th with blanks\]
[3] [/3]
[4] [/4]
This is exactly what I want!

From Pelles IDE I get:
Code: [Select]
D:\C\cmdlinetst\cmdlinetst2.exe /1 "c:\pa"th with blanks\" /3 /4
[0] [D:\C\cmdlinetst\cmdlinetst2.exe]
[1] [/1]
[2] [c:\path]
[3] [with]
[4] [blanks"]
[5] [/3]
[6] [/4]
The return value from GetCommandLineW() is not any more the real command line.
The \" is replaced by a single " and the "\ is turned to an \".