Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Trying to launch program with command line parameters: Windows


This question is answered.


Permlink Replies: 6 - Last Post: Oct 17, 2016 2:27 AM Last Post By: Richard Williams
Richard Williams

Posts: 23
Registered: 6/4/05
Trying to launch program with command line parameters: Windows  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 13, 2016 6:29 AM
I am writing a little program to act as a file viewer for my photo to cross stitch chart conversion program. Ultimately I want to have it so it can be used as the registered viewer program in the 'open' dialog box, but I obviously have missed something.

The program is launched with the command line:
c:\rww\viewer.exe c:\rww\example.bs

In the 'On Create' event for the form I have the following code (#include <stdlib.h> is included in the source):

String filename;
int j = _argc;
if (j > 1) {
filename = String(_argv[1]);
}

if (ExtractFileExt(filename).LowerCase() == ".bs") {
if (FileExists(ChangeFileExt(filename,".bmp"))) {
Image1->Picture->LoadFromFile(ChangeFileExt(filename,".bmp"));
}
}

When doing this, _argc = 4, but when using the evaluate/modify tool on a step through, _argv[0] = ???? as are all other values. Consequently the code bombs out at 'filename = String(_argv[1]);'

I have a feeling I'm looking at this problem through the wrong end of a telescope.
Can anyone show me what I'm doing wrong or point me to a C++ Builder windows example that does actually respond to command line parameters?

Version of RAD Studio = 10.1Berlin

Thanks,

Richard.

Edited by: Richard Williams on Oct 13, 2016 2:29 PM

Martin van der ...

Posts: 57
Registered: 7/14/02
Re: Trying to launch program with command line parameters: Windows
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 13, 2016 6:54 AM   in response to: Richard Williams in response to: Richard Williams
Not sure if _argc and _argv are even supposed to work for Windows applications since they don't have a main() function, but you could use ParamCount() and ParamStr() instead to fetch the command-line options.
Richard Williams

Posts: 23
Registered: 6/4/05
Re: Trying to launch program with command line parameters: Windows  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 13, 2016 7:10 AM   in response to: Martin van der ... in response to: Martin van der ...
Martin van der Plas wrote:
Not sure if _argc and _argv are even supposed to work for Windows applications since they don't have a main() function, but you could use ParamCount() and ParamStr() instead to fetch the command-line options.
Well, just shows I've been asking the wrong questions of the help file. Thanks very much. Haven't got it quite to work yet, but it's heading that way.

Greatly appreciated

Richard
Richard Williams

Posts: 23
Registered: 6/4/05
Re: Trying to launch program with command line parameters: Windows  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 13, 2016 7:38 AM   in response to: Richard Williams in response to: Richard Williams
Got it to work now. Thanks.

Richard
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Trying to launch program with command line parameters: Windows  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 13, 2016 11:45 AM   in response to: Martin van der ... in response to: Martin van der ...
Martin wrote:

Not sure if _argc and _argv are even supposed to work for
Windows applications since they don't have a main() function

Yes, they do (as evident by the fact that _argc has a non-zero value). However,
you have to take into account that _argv[] contains Ansi strings. In C++Builder
2009+, where Unicode strings are being used, you have to use _wargv[] instead
of _argv[]. So, in this case, _argv is NULL, but _wargv[] is not NULL and
contains the expected command parameter strings.

Alternatively, use ParamStr() instead, or even GetCommandLineW() and CommandLineToArgvW().

--
Remy Lebeau (TeamB)
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Trying to launch program with command line parameters: Windows [Edit]
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 13, 2016 11:38 AM   in response to: Richard Williams in response to: Richard Williams
Richard wrote:

When doing this, _argc = 4, but when using the evaluate/modify tool
on a step through, _argv[0] = ???? as are all other values. Consequently
the code bombs out at 'filename = String(_argv[1]);'

Try using ParamStr() instead of _argv[], eg:

String filename = ParamStr(1);
if (!filename.IsEmpty())
{
    if (SameText(ExtractFileExt(filename), _D(".bs")))
    {
        String bmpfile = ChangeFileExt(filename, _D(".bmp"));
        if (FileExists(bmpfile))
            Image1->Picture->LoadFromFile(bmpfile);
    }
}


--
Remy Lebeau (TeamB)
Richard Williams

Posts: 23
Registered: 6/4/05
Re: Trying to launch program with command line parameters: Windows [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 17, 2016 2:27 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
Richard wrote:

When doing this, _argc = 4, but when using the evaluate/modify tool
on a step through, _argv[0] = ???? as are all other values. Consequently
the code bombs out at 'filename = String(_argv[1]);'

Try using ParamStr() instead of _argv[], eg:

String filename = ParamStr(1);
if (!filename.IsEmpty())
{
    if (SameText(ExtractFileExt(filename), _D(".bs")))
    {
        String bmpfile = ChangeFileExt(filename, _D(".bmp"));
        if (FileExists(bmpfile))
            Image1->Picture->LoadFromFile(bmpfile);
    }
}


--
Remy Lebeau (TeamB)
Thanks Remy,
I keep forgetting the _D with type String. As I am mostly working with legacy stuff from BCB4, most of what I do uses AnsiString, and I've not got out of the habit yet.

Regards,

Richard
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02