Solved

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

Posted on 2006-10-26
6
4,168 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
[X]
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
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:Jose Parrot
Jose Parrot 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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:
Jose Parrot 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
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…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

752 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