NO

Author Topic: Command Line  (Read 7801 times)

czerny

  • Guest
Command Line
« on: October 13, 2011, 12:24:16 AM »
Hallo,

I have a problem with the direct execution of a console programm from the IDE:

This is the 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]);

    LocalFree(szArglist);

    return(0);
}

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

and

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 \".

So I get garbage.

What exactly is happening here?
What is the function (sort of  ExecProgram()) used to execute exe's from Pelles IDE?

czerny

czerny

  • Guest
Re: Command Line
« Reply #1 on: October 25, 2011, 07:00:33 PM »
Have others tested my examples?

Is the question to silly to answer or to difficult?

czerny

CommonTater

  • Guest
Re: Command Line
« Reply #2 on: October 25, 2011, 07:56:31 PM »
This is the code:
Code: [Select]
/1 "c:\pa\"th with blanks"\ /3 /4
/1 "c:\pa"th with blanks\" /3 /4

Look closely... see any difference between your first and second command line?

czerny

  • Guest
Re: Command Line
« Reply #3 on: November 01, 2011, 03:28:48 PM »
The command I typed is allways the same.

What is different is the commandline GetCommandLineW() is returning.

That is the point!!!

czerny

CommonTater

  • Guest
Re: Command Line
« Reply #4 on: November 01, 2011, 04:08:46 PM »
No actually the point is that the two example command lines you typed are different... and in both cases the returned value is correct but, of course, different.

« Last Edit: November 01, 2011, 05:09:06 PM by CommonTater »

czerny

  • Guest
Re: Command Line
« Reply #5 on: November 02, 2011, 08:29:41 AM »
How do you know, what I have typed?

I have typed both times the same (first code in my post).

First: on the command line
Second: in the IDE

And I got two different results.

Try it!

czerny

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2152
Re: Command Line
« Reply #6 on: November 02, 2011, 03:10:38 PM »
Only in the console program.
Code: [Select]
#define UNICODE
#define _UNICODE
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#pragma comment(lib, "user32")

//  /1 "c:\pa\"th with blanks"\ /3 /4  <- copy that to project Command line arguments:
//int __cdecl WinMainCRTStartup(void)
int __cdecl mainCRTStartup(void)
{
TCHAR szTmp[260];
lstrcpy(szTmp, L"/1 \"c:\\pa\\\"th with blanks\"\\ /3 /4\n");
lstrcat(szTmp, GetCommandLineW());
OutputDebugString(szTmp);
MessageBoxW(0, szTmp, 0, MB_OK);
ExitProcess(0);
}
EDIT:
Console:
Code: [Select]
ODS: /1 "c:\pa\"th with blanks"\ /3 /4
C:\code\PellesC\Forum\TestGetCommandLineW3.exe /1 "c:\pa"th with blanks\" /3 /4
Windows:
Code: [Select]
ODS: /1 "c:\pa\"th with blanks"\ /3 /4
"C:\code\PellesC\Forum\TestGetCommandLineW3.exe" /1 "c:\pa\"th with blanks"\ /3 /4
« Last Edit: November 02, 2011, 05:32:52 PM by timovjl »
May the source be with you

CommonTater

  • Guest
Re: Command Line
« Reply #7 on: November 02, 2011, 04:10:11 PM »
How do you know, what I have typed?

I'm going by the examples you gave in your first message...

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]


and
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]

Quote
I have typed both times the same (first code in my post).

Nope... you didn't.  Seriously... look again!



czerny

  • Guest
Re: Command Line
« Reply #8 on: November 08, 2011, 12:40:03 PM »
Ok, I tried it once more.

This is was I had in the project options in the IDE:

Code: [Select]
/1 "c:\pa\"th with blanks"\ /3 /4

and I got:

Code: [Select]
D:\C\cmdlinetst\cmdlinetst2.exe /1 "c:\pa"th with blanks\" /3 /4
[0] [D:\C\wwl\cmdlinetst\cmdlinetst2.exe]
[1] [/1]
[2] [c:\path]
[3] [with]
[4] [blanks"]
[5] [/3]
[6] [/4]
Press any key to continue...

then I added the command name and copied and pasted the result to the commandline

Code: [Select]
cmdlinetst2 /1 "c:\pa\"th with blanks"\ /3 /4

and I got:

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]

Isn't that the same I posted the first time?

czerny

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2152
Re: Command Line
« Reply #9 on: November 08, 2011, 07:46:46 PM »
Test this example in POIDE with commandline:
/1 "c:\pa\"th with blanks"\ /3 /4
with and without #define CON
and see what czerny has found
Code: [Select]
#define UNICODE
#define _UNICODE

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

#pragma comment(lib, "user32.lib")
#pragma comment(lib, "shell32.lib")

//#define CON
#ifdef CON
#pragma comment(linker, "/SUBSYSTEM:CONSOLE")
int __cdecl mainCRTStartup(void)
#else
#pragma comment(linker, "/SUBSYSTEM:WINDOWS")
int __cdecl WinMainCRTStartup(void)
#endif
{
LPWSTR *szArglist;
int nArgs;
int i, idx;
TCHAR sTmp[1024];

idx = wsprintf(sTmp, TEXT("%ls\n"), GetCommandLineW());
OutputDebugString(sTmp);
szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
if (NULL == szArglist)
{
OutputDebugString(L"CommandLineToArgvW failed\n");
return 1;
}
else
for (i = 0; i < nArgs; i++)
idx += wsprintf(&sTmp[idx], TEXT("[%d] [%ls]\n"), i, szArglist[i]);
OutputDebugString(sTmp);
MessageBox(0, sTmp, 0, MB_OK);
LocalFree(szArglist);

return (0);
}
May the source be with you

czerny

  • Guest
Re: Command Line
« Reply #10 on: November 10, 2011, 05:33:18 PM »
Hmm,

what does this mean?

One would expect, that the CON variant behaves like the cmd, but this is not the case.

Can someone explain this?

czerny

czerny

  • Guest
Re: Command Line
« Reply #11 on: December 03, 2011, 01:10:42 PM »
Hallo timovjl and CommonTater and others,

I do not understand your response.

Don't you see that
the commandline tipped in and
the commandline shown from GetCommandLineW
are different
if you run this from within the IDE?

czerny