Pelles C forum

Pelles C => General discussions => Topic started by: czerny on October 13, 2011, 12:24:16 am

Title: Command Line
Post by: czerny 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
Title: Re: Command Line
Post by: czerny on October 25, 2011, 07:00:33 pm
Have others tested my examples?

Is the question to silly to answer or to difficult?

czerny
Title: Re: Command Line
Post by: CommonTater 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?
Title: Re: Command Line
Post by: czerny 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
Title: Re: Command Line
Post by: CommonTater 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.

Title: Re: Command Line
Post by: czerny 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
Title: Re: Command Line
Post by: TimoVJL 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
Title: Re: Command Line
Post by: CommonTater 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!


Title: Re: Command Line
Post by: czerny 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
Title: Re: Command Line
Post by: TimoVJL 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);
}
Title: Re: Command Line
Post by: czerny 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
Title: Re: Command Line
Post by: czerny 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