• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4258
  • Last Modified:

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

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.
0
Nikhilesh_K
Asked:
Nikhilesh_K
5 Solutions
 
George TokasCommented:
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.
0
 
Jose ParrotGraphics ExpertCommented:
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
0
 
Jase-CoderCommented:
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.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
pmdwCommented:
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.
 
0
 
Jose ParrotGraphics ExpertCommented:
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.
0
 
Nikhilesh_KAuthor 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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now