CommandLineToArgvW() doe not work with mingw

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

CommandLineToArgvW() doe not work with mingw

Thomas Steinbach
Hello,

if I try to run the following code with mingw (compile as non unicode
version)
and run the binary like "prog.exe -file test.txt" I get no vector of
arguments in argv.

BOOL GetCommandLineArgs(void)
{
    TCHAR szCmdLine[MAX_PATH] = _T("");
    LPTSTR lpszCmdLine = NULL;
    LPTSTR *argv = NULL;
    int argc = 0;
    int i = 0;
    TCHAR szFileName[MAX_PATH] = _T("");

    lpszCmdLine = (LPTSTR)szCmdLine;
    _tcscpy_s(lpszCmdLine, sizeof(szCmdLine)/sizeof(TCHAR),
GetCommandLine());

    argv = (LPTSTR*)CommandLineToArgvW((LPCWSTR)lpszCmdLine, &argc);

    if( argv == NULL ) {
        MessageBox(NULL, _T("Error getting arguments"), _T("Message"),
MB_OK);
        return FALSE;
    }else {
        for(i=1; i<argc; i++) {
            MessageBox(NULL, argv[i], _T("Message"), MB_OK);
            if( !_tcscmp(argv[i], _T("-file")) )
                _tcscpy_s(szFileName, sizeof(szFileName)/sizeof(TCHAR),
argv[i+1]);
        }
    }
    LocalFree(argv);

    return TRUE;
}

With cl.exe (visual c compiler 9.x) the code is working as expected and
I hvae access to the argv[i] arguments,
With a mingw compiled binary, it seems there is no vector of the
commadnline arguments in the argv vector and argc is only "1". Why?
And how can I get this arguments in a vector?

Thomas


------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables unlimited
royalty-free distribution of the report engine for externally facing
server and web deployment.
http://p.sf.net/sfu/businessobjects
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.

Most annoying abuses are:
1) Top posting
2) Thread hijacking
3) HTML/MIME encoded mail
4) Improper quoting
5) Improper trimming
_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: CommandLineToArgvW() doe not work with mingw

JonY-2
On 6/18/2009 12:48, Thomas Steinbach wrote:

> Hello,
>
> if I try to run the following code with mingw (compile as non unicode
> version)
> and run the binary like "prog.exe -file test.txt" I get no vector of
> arguments in argv.
>
> BOOL GetCommandLineArgs(void)
> {
>      TCHAR szCmdLine[MAX_PATH] = _T("");
>      LPTSTR lpszCmdLine = NULL;
>      LPTSTR *argv = NULL;
>      int argc = 0;
>      int i = 0;
>      TCHAR szFileName[MAX_PATH] = _T("");
>
>      lpszCmdLine = (LPTSTR)szCmdLine;
>      _tcscpy_s(lpszCmdLine, sizeof(szCmdLine)/sizeof(TCHAR),
> GetCommandLine());
>
>      argv = (LPTSTR*)CommandLineToArgvW((LPCWSTR)lpszCmdLine,&argc);
>
>      if( argv == NULL ) {
>          MessageBox(NULL, _T("Error getting arguments"), _T("Message"),
> MB_OK);
>          return FALSE;
>      }else {
>          for(i=1; i<argc; i++) {
>              MessageBox(NULL, argv[i], _T("Message"), MB_OK);
>              if( !_tcscmp(argv[i], _T("-file")) )
>                  _tcscpy_s(szFileName, sizeof(szFileName)/sizeof(TCHAR),
> argv[i+1]);
>          }
>      }
>      LocalFree(argv);
>
>      return TRUE;
> }
>
> With cl.exe (visual c compiler 9.x) the code is working as expected and
> I hvae access to the argv[i] arguments,
> With a mingw compiled binary, it seems there is no vector of the
> commadnline arguments in the argv vector and argc is only "1". Why?
> And how can I get this arguments in a vector?
>
> Thomas
>

Hi Thomas,

it works for me on mingw, what command line option did you use for the
compile?

btw, I used _tcscpy instead of _tcscpy_s, I don't have msvcr90 runtime.


------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables unlimited
royalty-free distribution of the report engine for externally facing
server and web deployment.
http://p.sf.net/sfu/businessobjects
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.

Most annoying abuses are:
1) Top posting
2) Thread hijacking
3) HTML/MIME encoded mail
4) Improper quoting
5) Improper trimming
_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: CommandLineToArgvW() doe not work with mingw

Chris Sutcliffe-2
> btw, I used _tcscpy instead of _tcscpy_s, I don't have msvcr90 runtime.

I'm curious how the problem code was compiled as well, given that
_tcscpy_s is not in any MinGW headers.  Where custom headers used?
Was MinGW64 used?

Chris

--
Chris Sutcliffe
http://emergedesktop.org

------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables unlimited
royalty-free distribution of the report engine for externally facing
server and web deployment.
http://p.sf.net/sfu/businessobjects
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.

Most annoying abuses are:
1) Top posting
2) Thread hijacking
3) HTML/MIME encoded mail
4) Improper quoting
5) Improper trimming
_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: CommandLineToArgvW() doe not work with mingw

ColdShine
In reply to this post by Thomas Steinbach
2009/6/18 Thomas Steinbach <[hidden email]>
> if I try to run the following code with mingw (compile as non unicode
> version)

So, you say you compile as non Unicode, but then...


>    TCHAR szCmdLine[MAX_PATH] = _T("");
>    LPTSTR lpszCmdLine = NULL;
>    LPTSTR *argv = NULL;
>    int argc = 0;
>    int i = 0;
>    TCHAR szFileName[MAX_PATH] = _T("");
>
>    lpszCmdLine = (LPTSTR)szCmdLine;
>    _tcscpy_s(lpszCmdLine, sizeof(szCmdLine)/sizeof(TCHAR),
> GetCommandLine());
>
>    argv = (LPTSTR*)CommandLineToArgvW((LPCWSTR)lpszCmdLine, &argc);

...this means that here you're converting a char* to wchar_t* on input
to CommandLineToArgvW, and then you cast its return back to a "narrow"
string (actually, a vector of strings - what matters is how you'll end
up accessing the characters).


> With a mingw compiled binary, it seems there is no vector of the
> commadnline arguments in the argv vector and argc is only "1". Why?
> And how can I get this arguments in a vector?

Here's the thing: CLTAW will read your input string as pairs of bytes,
thus never finding any spaces (0x20); rather, it'll find "wide"
characters with 0x20 in either byte, which won't count as spaces.
Then, I guess out of luck, it will find a pair of narrow NUL chars at
the end of the string it will interpret as a wide NUL character, and
it'll simply copy that single long argument to the buffer it
allocates, returning that to you. You then interpret that string back
as a narrow string, so finding all the characters intact, but as a
single long argument. That explains your count of one.

I'm not sure how you actually compiled this with Visual Studio, so I'm
not gonna try to understand why that works for you.

--
CS

------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables unlimited
royalty-free distribution of the report engine for externally facing
server and web deployment.
http://p.sf.net/sfu/businessobjects
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.

Most annoying abuses are:
1) Top posting
2) Thread hijacking
3) HTML/MIME encoded mail
4) Improper quoting
5) Improper trimming
_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: CommandLineToArgvW() doe not work with mingw

JonY-2
In reply to this post by Chris Sutcliffe-2
On 6/18/2009 21:47, Chris Sutcliffe wrote:
>> btw, I used _tcscpy instead of _tcscpy_s, I don't have msvcr90 runtime.
>
> I'm curious how the problem code was compiled as well, given that
> _tcscpy_s is not in any MinGW headers.  Where custom headers used?
> Was MinGW64 used?
>
> Chris

Hi,
currently mingw-w64 has the header defines, but not the import libs, so
it doesn't link yet. mingw.org libs are missing the imports as well.

I only noticed that it was in msvcr90.dll because of Dependency walker.

------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables unlimited
royalty-free distribution of the report engine for externally facing
server and web deployment.
http://p.sf.net/sfu/businessobjects
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.

Most annoying abuses are:
1) Top posting
2) Thread hijacking
3) HTML/MIME encoded mail
4) Improper quoting
5) Improper trimming
_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: CommandLineToArgvW() doe not work with mingw

Thomas Steinbach
In reply to this post by JonY-2
Hello Jon,

>> [..]
>> With cl.exe (visual c compiler 9.x) the code is working as expected and
>> I hvae access to the argv[i] arguments,
>> With a mingw compiled binary, it seems there is no vector of the
>> commadnline arguments in the argv vector and argc is only "1". Why?
>> And how can I get this arguments in a vector?

> it works for me on mingw, what command line option did you use for the
> compile?

I was still a little bit confused with Unicode vs. Multibyte/ANSI.
In this case I think the first mistake was to mix both version and to try
to code for both versions. But I have fixed that and using now
#ifde UNICODE to catch every case.

> btw, I used _tcscpy instead of _tcscpy_s, I don't have msvcr90 runtime.

That is just a simple wrapper function to compile in both compiler with
the additon of the _s functions if compiled by cl. At the end and for the
mingw compiler it is just a _tcscpy(...) funtcion.

btw: Since a while you can use (also for commercial projects *)
the newest "cl" compiler when you downlaod and install the
VC++ 2008 Express Edition (IDE+Compiler,Linker,etc and IMHO
SDK 6.0A ) and the Windows SDK (6.1) which hast additional tools
like ResourceCompiler, etc. and all the newest headers and libs.

*)  A MVPs in a german microsoft vc NewsGroup told me that
See Message-ID: <#[hidden email]>
and should be explained in the FAQs of VC++ Express, but didn't
proofed that by myself until now.

Thomas


------------------------------------------------------------------------------
Are you an open source citizen? Join us for the Open Source Bridge conference!
Portland, OR, June 17-19. Two days of sessions, one day of unconference: $250.
Need another reason to go? 24-hour hacker lounge. Register today!
http://ad.doubleclick.net/clk;215844324;13503038;v?http://opensourcebridge.org
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.

Most annoying abuses are:
1) Top posting
2) Thread hijacking
3) HTML/MIME encoded mail
4) Improper quoting
5) Improper trimming
_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: CommandLineToArgvW() doe not work with mingw

Thomas Steinbach
In reply to this post by ColdShine
Hello ColdShine,

>> [...]
> ...this means that here you're converting a char* to wchar_t* on input
> to CommandLineToArgvW, and then you cast its return back to a "narrow"
> string (actually, a vector of strings - what matters is how you'll end
> up accessing the characters).

> [...]
> allocates, returning that to you. You then interpret that string back
> as a narrow string, so finding all the characters intact, but as a
> single long argument. That explains your count of one.
>
> I'm not sure how you actually compiled this with Visual Studio, so I'm
> not gonna try to understand why that works for you.

Yes, I still tried to mix ANSI and Unicode code, which doesn't work.
I fixed that and now using #ifdef UNICODE etc. and now it compiles
in VS as Unicode and Ansi version and with mignw as ANSI Version.
Thanks for your comment.

Thomas


------------------------------------------------------------------------------
Are you an open source citizen? Join us for the Open Source Bridge conference!
Portland, OR, June 17-19. Two days of sessions, one day of unconference: $250.
Need another reason to go? 24-hour hacker lounge. Register today!
http://ad.doubleclick.net/clk;215844324;13503038;v?http://opensourcebridge.org
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.

Most annoying abuses are:
1) Top posting
2) Thread hijacking
3) HTML/MIME encoded mail
4) Improper quoting
5) Improper trimming
_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users