C++Builder: pass command line argument to MainForm from WinMain

93 Views Asked by At

Borland C++ Builder 6

WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
        try
        {
                 AnsiString s = ParamStr(0); // I want to pass this argument
                 Application->Initialize();
                 Application->CreateForm(__classid(TForm1), &Form1);  // Can't pass here?
                 Application->Run();  // Can't pass here either???
        }
        catch (Exception &exception)

How do I pass a command line argument/parameter to Form1?

CreateForm doesn't allow overloaded constructors.

1

There are 1 best solutions below

4
Fruchtzwerg On

Creating forms with CreateForm does not allow to pass additional arguments directly in Borland C++ Builder 6.

You can solve this by creating an additional method to set the arguments:

Unit1.h

class TForm1 : public TForm
{
    //...

public:
    void SetCommandLineArguments(const AnsiString& arguments);
    //...
};

your form then can be initialized like:

Project1.cpp

#include "Unit1.h"
...
AnsiString s = ParamStr(0); // Get the command line argument
Application->Initialize();
Application->CreateForm(__classid(TForm1), &Form1);
Form1->SetCommandLineArguments(s);
Application->Run();