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
calcanusAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

toesableCommented:
Have you tried:

function run_exe(const filename : pchar; const shownormal : boolean) : boolean;
var
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
toesableCommented:
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;

0
luisseixasCommented:
// First, create a function that will process the "Running" procedure:


function TForm1.RunProcess(const AppPath, AppParams: string;
  Visibility: Word; MustWait: Boolean): DWord;
var
  SI: TStartupInfo;
  PI: TProcessInformation;
  Proc: THandle;
begin
  FillChar(SI, SizeOf(SI), 0);
  SI.cb := SizeOf(SI);
  SI.wShowWindow := Visibility;
  if not CreateProcess(PChar(AppPath), PChar(AppParams), nil, nil, false, Normal_Priority_Class, nil, nil, SI, PI) then
    raise Exception.CreateFmt('Failed to excecute program. Error Code %d', [GetLastError]);

  Proc := PI.hProcess;
  CloseHandle(PI.hThread);

  if MustWait then
       if WaitForSingleObject(Proc, Infinite) <> Wait_Failed then
         GetExitCodeProcess(Proc, Result);

  CloseHandle(Proc);
end;


// Than, every time you want to "Run" an application, for example, Windows Explorer, call that function, like this:


RunProcess('c:\windows\explorer.exe',' C:\temp', SW_SHOWNORMAL, false);
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

BorlandManCommented:

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
0
landrockerCommented:
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
0
abrooksCommented:
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.

0
CleanupPingCommented:
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.
0
kacorretiredCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

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.