We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

how to call an exe file on click of a button?

Nikhilesh_K
Nikhilesh_K asked
on
Medium Priority
4,290 Views
Last Modified: 2013-11-17
hi
how can i call an exe file, say i have an exe file "nikhil.exe" in C:\
how can i call it on click of a button?

nikhil.
Comment
Watch Question

Check out:
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus_Builder/Q_21043034.html

Put the code inside TButton's OnClick() and its working.

George Tokas.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Jose ParrotGraphics Expert
CERTIFIED EXPERT
Commented:
Hi Nikhil,

We again... Nice see you around.
You can run your program from a Borland C++ program by using the
    CreateProcess (the help inside Builder is a good start point)
or one of the variants of the
    exec
statement.

This second approach is more flexible and powerful, so ltes examine it in detail.
exec command has variable syntaxes, depending on the suffixes:

e       envp, array of pointers to environment settings.

l        Command-line arguments to pass to exec function.
         Used when number of parameters to new process is known.

p       PATH environment variable is used to find file to execute.

v       argv, array of pointers to command-line arguments, is passed to _exec.
         Used when number of parameters to new process is variable.

You can combine these suffixes, to obtain the commands:
         execl, execv, execle, execve, execlp, execvp, execlpe, execvpe.

Follows a console program to show how to use the exec family of commands.


/*  Sample use of exec family
 *      execl          execle         execlp          execlpe
 *      execv         execve         execvp         execvpe
 */

#include <stdio.h>
#include <conio.h>
#include <process.h>

char *my_env[] =       // this simulates the parameters passed in argv[]
{
   "THIS=environment will be",
   "PASSED=to new process by",
   "the EXEC=functions",
   NULL                       // this is a terminator
};

void main()
{
   char *args[4], prog[80];
   int ch;

   printf( "Enter name of program to exec: " );
   gets( prog );
   printf( " 1. execl  2. execle  3. execlp  4. execlpe\n" );
   printf( " 5. execv  6. execve  7. execvp  8. execvpe\n" );
   printf( "Type a number from 1 to 8 (or 0 to quit): " );
   ch = _getche();
   if( (ch < '1') || (ch > '8') )
       exit( 1 );
   printf( "\n\n" );

   /* Arguments for execv? */
   args[0] = prog;
   args[1] = "exec??";
   args[2] = "two";
   args[3] = NULL;

   switch( ch )
   {
   case '1':
      execl( prog, prog, "execl", "two", NULL );
      break;
   case '2':
      execle( prog, prog, "execle", "two", NULL, my_env );
      break;
   case '3':
      execlp( prog, prog, "execlp", "two", NULL );
      break;
   case '4':
      _execlpe( prog, prog, "execlpe", "two", NULL, my_env );
      break;
   case '5':
      execv( prog, args );
      break;
   case '6':
      execve( prog, args, my_env );
      break;
   case '7':
      execvp( prog, args );
      break;
   case '8':
      execvpe( prog, args, my_env );
      break;
   default:
      break;
   }

Enjoy.

Jose
Your should use ShellExecute rather than execl. I think the exe* functions were designed for windows older than Windows 95. To use ShellExecute do

#include <shellapi.h>

ShellExecute(NULL, "open", "MyProgram", NULL, NULL, SW_SHOW);

The 3rd parameter is any command line arguments your program takes.
Commented:
This may be a bit OTT but assuming you want to tie this to a menu or button.

On the component bar Select Additional then drag and drop an ActionManager
( form with a triangle icon) onto your form.

Right click and select customize.
In the customizing form
Select the Actions tab them right click on the catagories
Select new standard action
Select TfileRun
Select Run in the actions list
Go to the object inspector and set the properties as required
IE Expand actions and set browse to false and the file name to the exe required
Many, many, lots of fun options here :-)

To use the ActionManager in a button or menu simply choose the run action in the properties of the  menu or button.
 
Graphics Expert
CERTIFIED EXPERT
Commented:
Hi, Nikhil,

To execute the program "c:\nikhil.exe" from your Builder C++ program you can create a TButton, say Button1 in your Form1 (I'm using default names as Builder does automaticately).
Now double click on this button, to go to the editor. Then complete the code:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    char *program = "c:\\windows\\notepad.exe";  // you can substitute it by "c:\\nikhil.exe"
    char *arguments = "";   // you can pass arguments if needed
/*
    // you can use variables for path/filename and arguments
    execl(program,NULL,arguments,NULL);
*/

    // or literal arguments to exec
    execl("c:\\windows\\notepad.exe","test.txt",NULL);
}

The code above executes notepad.exe and finish your program.

If you wan to execute notepad and NOT finish your program, then, as suggested by Jase-coder, use:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    char *program = "c:\\windows\\notepad.exe";
    ShellExecute(NULL, "open", program, NULL, NULL, SW_SHOW);
}

Another interesting feature of ShellExecute is that if you use a registered document as argument, the proper program is invoked. For example:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    char *document = "c:\\document.doc";
    ShellExecute(NULL, "open", document, NULL, NULL, SW_SHOW);
}
will execute Word and load document.doc. if the document is "sheet.xls", it opens Excel and loads the file.

You have another option, which is to use the spawn family of commands.
These commands are used the same way as the exec family. The difference between them is that spawn don't close your program, returning to it when you close the called executable.

For example:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    char *program = "c:\\windows\\notepad.exe";
    spawnl(P_WAIT, program, program, NULL);
}
will execute notepad and, when you close notepad, your program resumes and continues.

So, we can conclude that all three approaches are valid, but aren't equal, as they have different functions:
exec    closes your program if nikhil.exe is found and executed.
spawn  returns to your program after nikhil.exe is found and executed.
ShellExecute    doesn't close your program if nikhil.exe is found and executed. And starts registered programs as we doubleclicked a registered document or image file.

Is up to you.

Jose

P.S. Yes, Jase-Coder, exec and spawn are from the DOS era and still supported by Windows xp and Unix. BTW, all these cited commands aren't ANSI compliant.

Author

Commented:
hi
hey guys , nice to see you again.
it will be injutice if i dont award points to each and every reply, cos every one has helped in solving the problem.
so i m raising the points.
 thanks Guys.

nikhil.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.