Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2006-10-26
6
4,163 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.
0
Comment
Question by:Nikhilesh_K
6 Comments
 
LVL 16

Assisted Solution

by:George Tokas
George Tokas earned 50 total points
ID: 17814848
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
 
LVL 18

Assisted Solution

by:JoseParrot
JoseParrot earned 100 total points
ID: 17814942
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
 
LVL 11

Assisted Solution

by:Jase-Coder
Jase-Coder earned 50 total points
ID: 17817893
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 2

Assisted Solution

by:pmdw
pmdw earned 50 total points
ID: 17822294
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
 
LVL 18

Accepted Solution

by:
JoseParrot earned 100 total points
ID: 17824079
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
 

Author Comment

by:Nikhilesh_K
ID: 17824483
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

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
eclipse ide not starting 16 85
populate value from mongo db in java 98 358
I could not build boost code, 10 94
java. non-English characters encoding problem. intellij idea 3 88
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

766 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question