Powerbasic Museum 2020-B

IT-Consultant: Patrice Terrier => C++ programming (SDK style) => Topic started by: Patrice Terrier on February 07, 2014, 11:08:28 AM

Title: Easy parsing of the command line (UNICODE)
Post by: Patrice Terrier on February 07, 2014, 11:08:28 AM
To parse a UNICODE command line, i would recommend the use of the built-in CommandLineToArgW API

See the MSDN documentation here. (http://msdn.microsoft.com/en-us/library/windows/desktop/bb776391(v=vs.85).aspx)

...
Title: Re: Easy parsing of the command line (UNICODE)
Post by: James C. Fuller on February 07, 2014, 02:08:39 PM
Patrice,
  Where would one use this?
I tested first just from the command line and then with another app shelling to it.
This is one of the few instances where I use globals. It makes calling the API functions unecessary.
James
test02

#ifndef UNICODE
#define UNICODE
#endif
#ifndef _UNICODE
#define _UNICODE
#endif

#include <iostream>
#include <sstream>
#include <tchar.h>
using namespace std;
// *************************************************
//            User Global Variables
// *************************************************

static _TCHAR*   *g_argv;
static int     g_argc;

// *************************************************
//                 Runtime Functions
// *************************************************

void Pause(void)
{
    _tsystem(_T("pause"));
}



// ************************************
//       User Subs and Functions
// ************************************

void TestIt ()
{
    int      i = {0};
    for(i = 1; i <= g_argc; i += 1)
    {
        wcout << g_argv[i - 1] << endl;
    }

}


int _tmain (int argc, _TCHAR** argv)
{
    g_argc = argc;
    g_argv = argv;
    TestIt();
    Pause();
}



test02test


#ifndef UNICODE
#define UNICODE
#endif
#ifndef _UNICODE
#define _UNICODE
#endif

#include <iostream>
#include <sstream>
#include <tchar.h>
using namespace std;

// *************************************************
//            User Global Variables
// *************************************************

static _TCHAR*   *g_argv;
static int     g_argc;



int _tmain (int argc, _TCHAR** argv)
{
    g_argc = argc;
    g_argv = argv;
    wstring  ws;
    ws = _T("test02.exe one two three");
    _tsystem((ws.c_str()));
}

Title: Re: Easy parsing of the command line (UNICODE)
Post by: Patrice Terrier on February 07, 2014, 05:58:47 PM
Then what if you need the extra parameters provided by WinMain,
aka: hInstance, hPrevInstance, lpCmdLine, nCmdShow.

I shall show the use of CommandLineToArgvW in context in the next BB64 build.

And also how to browse folders recursively searching for audio files, based on the parsing of the command line.

...

Title: Re: Easy parsing of the command line (UNICODE)
Post by: James C. Fuller on February 08, 2014, 10:48:49 AM
Patrice,
    Ok I get it.
Your msdn link was of  console app and with a console app I do it as shown.

James