Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 663
  • Last Modified:

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
0
calcanus
Asked:
calcanus
2 Solutions
 
toesableCommented:
Have you tried:

function run_exe(const filename : pchar; const shownormal : boolean) : boolean;
var
0
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now