?
Solved

run a program from within a delphi application

Posted on 2003-02-24
9
Medium Priority
?
650 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:calcanus
[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
9 Comments
 

Accepted Solution

by:
toesable earned 100 total points
ID: 8011069
Have you tried:

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

Expert Comment

by:toesable
ID: 8011102
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
 

Assisted Solution

by:luisseixas
luisseixas earned 100 total points
ID: 8011191
// 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
Industry Leaders: 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

Expert Comment

by:BorlandMan
ID: 8013251

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
 

Expert Comment

by:landrocker
ID: 8014616
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
 

Expert Comment

by:abrooks
ID: 8032623
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
 

Expert Comment

by:CleanupPing
ID: 9317088
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
 
LVL 10

Expert Comment

by:kacor
ID: 9956489
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

762 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