Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • Last Modified:

Command Line Starts Diffrent Parts of The Program.

hi, need some help, lets say we have a program..

in this program we have diffrent events..

now, I would like to call these events externally than internally with a command line..

like '"C:\App.exe" /event1' or '"C:\App.exe" /event2' to co-aside with the app.

so if the command line was run with '"C:\App.exe" /event1' then the app with do the event within the program like display a form for that event or do anything that is in the command.

so I need to run the program from a external line which will change the internal effects of the program.


Thanks
Sal.
0
SaLz
Asked:
SaLz
  • 5
  • 3
  • 3
  • +2
5 Solutions
 
pritaeasSoftware EngineerCommented:
You can use ParamStr() to check if something was passed on the command-line.
Or do you need other info?
0
 
SaLzAuthor Commented:
could u post an example of it?
0
 
DragonSlayerCommented:
procedure TForm1.Form1Create(Sender: TObject);
begin
  if ParamStr(1) = '/showabout' then
    doShowAbout
  else if ParamStr(2) = '/max' then
    WindowState := wsMaximised
  else
    doSomethingElse;
end;
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
pritaeasSoftware EngineerCommented:
Don't have Delphi here to check, but ParamStr(0) returns a string containing the path and executable started. ParamStr(1) returns the first parameter passed. Check ParamCount first to see if there are any parameters.

See here:

http://www.howtodothings.com/showarticle.asp?article=341
0
 
DragonSlayerCommented:
Oops, should be:

procedure TForm1.Form1Create(Sender: TObject);
begin
  if ParamStr(1) = '/showabout' then
    doShowAbout
  else if ParamStr(1) = '/max' then // <---- typo here just now
    WindowState := wsMaximised
  else
    doSomethingElse;
end;
0
 
Mohammed NasmanSoftware DeveloperCommented:
Hello

As ParamStr suggest you to use ParamStr, Use ParamCount to loop for all the parameters passed and ParamStr to check the parameter

here's an example

procedure TForm1.FormCreate(Sender: TObject);
var
  I : Integer;
begin
  for I := 1 to ParamCount do
    if UpperCase(ParamStr(I)) = '/EVENT1' then
      CallEvent1
    else if UpperCase(ParamStr(I)) = '/EVENT2' then
      CallEvent2

end;

Regards,
Mohammed
0
 
DragonSlayerCommented:
>> As ParamStr suggest you to use ParamStr
*sweat*...
0
 
SaLzAuthor Commented:
nice 1 guys, very fast posts, Im gonna split the points.

Thxs
Sal.


0
 
Wim ten BrinkCommented:
Awww... Why did no one even suggest to use FindCmdLineSwitch()???

if FindCmdLineSwitch('Event1', ['/'], True) then ... // Found it.

The first parameter is the switch you're looking for on the commandline. The second parameter are the characters that could be in front of the switch, in this case just '/' but you could use ['/', '-'] so the user could also use "Command.exe -Event1" instead of "Command.exe /Event1". The last parameter just tells the system that it's not case-sensitive.

Come on, guys... I thought we were all experts! Shame on you for forgetting this function! :-P
0
 
DragonSlayerCommented:
oh... felt so ashamed... lol ;-)
0
 
DragonSlayerCommented:
but ParamStr works without the additional overhead of SysUtils :-p
0
 
Wim ten BrinkCommented:
True, but almost all Delphi applications will use the SysUtils unit anyway. Even if it's just because you want to use the Exception class. (Which, as we've discovered before isn't really needed to raise exceptions!)
I have created several applications that don't use SysUtils, though. But hey, you could always go inside the SysUtils unit and copy&paste this function in your own project. It's such an easy function to use. :-)
0
 
SaLzAuthor Commented:
u think we could do that with all the uses files that we see at the top of the form?
what if we was 2 take out the uses  functions from them files and then put them into 1 file instead,

like at the moment a normal empty exe is around 350k, if one was 2 take out the functions that they need, the exe could be around 100k? or something, if we was 2 pull out all the other stuff in there, u think it would make a diffrence?
0
 
Wim ten BrinkCommented:
My exe's can be as small as 20k! I created a desktop background changer that changes my desktop background with an image from some list. It's a trayicon application too that pops up a menu with several options. And it's size? Only 24k...
But if you do want to create applications this small you have to do a lot of research for the Windows API and "steal" code from the VCL units. This just to strip away all the overhead of these units.
The main reason that this much overhead is created is because these units have initialization/finalization sections. Thus this code is always executed in your application even if you don't need it. This code will often use several classes and variables which in their turn will be included too into your final binary. Adding SysUtils makes your application grow from 20k to around the 45-50k. Using classes will get you near 100h. Using forms will increase the size to about 300k, even if you don't do anything with these forms!

Sometimes it's just easier to see if you need some function and if you do, copy it from the other source and include it in your own unit, instead of using the other unit and thus adding a lot more overhead.

Even the use of COM objects in your code will add overhead, if you use an imported type library in your code. In these cases, you need the unit ComObj, which uses ActiveX, which uses SysUtils. As a result, it will increase the size of the binaty to between 60 and 80k. If you also told the import wizard to create component wrappers for you then you're doomed with a binary of about 200 to 300k. (Which is why I avoid component wrappers around type libraries.)

If you're not careful, any unit you add to your small application might increase the final size quite drastically. Thus, if you really need to keep it small, extract what you need. But in most cases size doesn't matter (yeah, right...) and thus you can do it the easy way.

Besides, the function FindCmdLineSwitch is a great example of how to use the ParamStr() and ParamCount() functions. :-)
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 5
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now