Link to home
Start Free TrialLog in
Avatar of calcanus
calcanus

asked on

run a program from within a delphi application

hi,

i want to be able to recreate the run program featured in the windows start menu using the edit and button components.

ive tried various things using shellAPI but i havent been able to get it working, does anyone have any suggestions?

thanks in advance, mike
ASKER CERTIFIED SOLUTION
Avatar of toesable
toesable

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of toesable
toesable

try again...

function run_exe(const filename:pchar; const shownormal : boolean) : boolean;
var
k, sh : integer;
begin
 sh := SW_SHOWNORMAL;
 if not shownormal then
  sh := SW_MAXIMIZED;
 k := ShellExecute(application.handle, nil, filename, nil, nil, sh);
 result := (k > 32);
end;

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

How about ShellExecute()
Here's a code snippet you can drop in a sample app to try out.

sorry about word wrapping, simply paste this code into your ide and all will be well.

also don't forget to add to your uses clause ShellApi
or it won't compile.



procedure TForm1.Button1Click(Sender: TObject);
const
     ksFilename = 'http://www.borland.com' ;
     ksFilename2 = 'c:\winnt\notepad.exe';
var
     iResult : LongInt ;

begin
  iResult := ShellExecute( self.Handle,     // parent window
                              nil,             // operation
                              ksFilename2,      // file to open
                              nil,             // cmd line args
                              nil,             // directory
                              SW_SHOWNORMAL    // display style
                              );

     if ( iResult < 32 ) then
        ShowMessage ('Error encountered');

end;


hth,
J
hey calcanus,

one way you could run a prog would be to use the WinExec() function.

the syntax is...:

-------------------------

WinExec(PChar("C:\somecommandyouwannarun.exe"), SHOWFLAG);

-------------------------

Where somecommandyouwannarun.exe is the command and SHOWFLAG is one of:
- SW_SHOWNORMAL - Run as normal

- SW_MAXIMISED - Run Maximised

- SW_HIDE - Run in the Background

I hope this helps,
landrocker
JEDI Code Library Content | Index | Reference

ShellExecAndWait
See Also, Miscellaneous
Performs an action on a file.
function ShellExecAndWait(const FileName: string; const Parameters: string = ''; const Verb: string = ''; CmdShow: Integer = SW_SHOWNORMAL): Boolean;
Unit
JclShell
Parameters
const FileName: string
Name of the file or object on which to perform the action.
const Parameters: string = ''
Application parameters. The parameters must be separated by spaces. To include double quotation marks, enclose each mark in a pair of quotation marks. This parameter defaults to an empty string.
const Verb: string = ''
Specifies the action to be performed. The available verbs depend on the type of object specified in the FileName parameter but some of the more commonly used ones are 'open', 'print' and 'edit'. See the Platform SDK documentation for more information. This parameter defaults to an empty string which causes the default verb to be executed (usually 'open').
CmdShow: Integer = SW_SHOWNORMAL
Flags that specify how the application should display when it is opened. See the Platform SDK documentation on ShellExecute for more information. Commonly used values are SW_HIDE and SW_SHOW. This parameter defaults to SW_SHOWNORMAL.
Return Value
If the function succeeds it returns True, otherwise it returns False. You can call GetLastError to get more detailed information about the reason of failure.
Description
ShellExexAndWait is a wrapper for the Win32 API function ShellExecuteEx which alleviates some of the complexities of this function by providing defaults for some of its lesser used parameters. This function performs an action specified on a file which can be anything from executing the file to opening it using the associated editor. This routine is identical to ShellExec except that this function waits for the action to be completed. For example, when using this file to open a HTML document, the function will launch the default webbrowser and then wait until the browser terminates, only then will this function call return to its caller.
Quick Info
Unit: JclShell
Category: Shell

Donator: Petr Vones

Delphi Versions: 4,5,6

Kylix Versions: 1

Free Pascal Compiler Versions: 1.x

Windows NT/2000: Requires Windows NT 4 or later.

Windows 95/98: Requires Windows 95 or later.

Requirements: Shell32 Version 4.00 or later.

calcanus:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:
       to split points between toesable, luisseixas, BorlandMan, landrocker and abrooks
Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Some days and here is the Christmas Time. I wish good luck and good health for you all and for your loved ones

kacor
EE Cleanup Volunteer