rbw
rbw

Reputation: 187

Access command line parameters in C++ Windows App

I have a C++ Windows App written in Embarcadero C++Builder, and I would like to pass the arguments given in the command prompt to the application.

int WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR argv, int argc)
{
    try
    {
        Application->Initialize();
        Application->CreateForm(__classid(TFormMain), &FormMain);
        if (argc > 1)
        {
            // pass argv to app.
        }
        Application->Run();
    }
    catch (Exception &exception)
    {
        Application->ShowException(&exception);
    }
    catch (...)
    {
        try
        {
            throw Exception("");
        }
        catch (Exception &exception)
        {
            Application->ShowException(&exception);
        }
    }
    return 0;
}

How do I proceed from here?

Upvotes: 0

Views: 821

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596287

First off, the last 2 parameters of your _tWinMain entry point are wrong. They are actually defined as LPSTR lpCmdLine, int nShowCmd instead. argv/argc parameters are provided only in a console app's main()-style entry point, not in a GUI app's WinMain-style entry point. The lpCmdLine parameter is a pointer to a single string containing the entire raw command-line, it is not pre-parsed into an array of substrings, like main() does. And the nShowCmd parameter is not related to the command-line at all.

You can use the Win32 API GetCommandLineW() and CommandLineToArgvW() functions to get such an array.

Alternatively, C++Builder's RTL has several Command Line Support Routines of its own:

Upvotes: 5

Related Questions