ShellExecute in Delphi – Launch external applications.




Executing external applications from Delphi can be very useful for a programmer.

Imagine you want to call other applications from Delphi code, like backup utilities, text editors, music players, video players, word and excel documents, etc..

You can execute any external file by using the ShellExecute function present in the ShellApi library of Delphi and its very simple to use.

This function is linked to the ShellExecute Windows API function.

The function returns an integer that corresponds to an error code which is very useful if you need to know if the function worked or not, these error codes are explained below at the end of the post.

In order to use the function, you first need to add the ShellApi to your uses clause, like this :

uses ShellApi;

Here are some commonly used examples of the function:

Run an executable and show it:

filename := 'c:\program.exe';
ShellExecute(handle,'open',PChar(filename), '','',SW_SHOWNORMAL);

Run an executable and minimize it:

filename := 'c:\program.exe';
ShellExecute(handle,'open',PChar(filename), '','',SW_MINIMIZE);

Run an executable and maximize it:

filename := 'c:\program.exe';
ShellExecute(handle,'open',PChar(filename), '','',SW_MAXIMIZE);

Run an executable and hide it:

filename := 'c:\program.exe';
ShellExecute(handle,'open',PChar(filename), '','',SW_HIDE);

Run an executable with parameters:

filename := 'c:\program.exe';
parameters := '-c -i -v';
ShellExecute(handle,'open',PChar(filename), PChar(parameters),'',SW_SHOWNORMAL);

 

Return Values from ShellExecute function.

If the return value of ShellExecute is greater than 32, the application was executed successfully.
If its less than 33 then the function failed.

Here is a complete list of the possible return values of ShellExecute:

0 = The operating system is out of memory or resources.
2 = The specified file was not found
3 = The specified path was not found.
5 = Windows 95 only: The operating system denied access to the specified file
8 = Windows 95 only: There was not enough memory to complete the operation.
10 = Wrong Windows version
11 = The .EXE file is invalid (non-Win32 .EXE or error in .EXE image)
12 = Application was designed for a different operating system
13 = Application was designed for MS-DOS 4.0
15 = Attempt to load a real-mode program
16 = Attempt to load a second instance of an application with non-readonly data segments.
19 = Attempt to load a compressed application file.
20 = Dynamic-link library (DLL) file failure.
26 = A sharing violation occurred.
27 = The filename association is incomplete or invalid.
28 = The DDE transaction could not be completed because the request timed out.
29 = The DDE transaction failed.
30 = The DDE transaction could not be completed because other DDE transactions were being processed.
31 = There is no application associated with the given filename extension.
32 = Windows 95 only: The specified dynamic-link library was not found.

If you like this topic than check our other Delphi tips.

This entry was posted in Delphi, Programming.

11 Responses to ShellExecute in Delphi – Launch external applications.

  1. Luis Legarreta says:

    How do I Getafe the “handle” para meter?

  2. Álvaro Ortiz says:

    Thanks a lot for this information,

    ¡The example is very useful!

    Best regards,
    Álvaro.

  3. John says:

    When I try to compile it gives me an error message saying ‘incompatible types ‘integer’ and ‘PChar” Help me fix this.

  4. ali says:

    thanks a lot

  5. yeah says:

    Can I use ShellExecute to close down an application like Notepad ?

  6. Nice article and informative too.
    Thanks.

    Just to add few more info:

    The ShellExecute function opens or prints a specified file. The file can be an executable file or a document file. Delphi’s help says below info:

    HINSTANCE ShellExecute(
    HWND hwnd, // handle to parent window
    LPCTSTR lpOperation, // pointer to string that specifies operation to perform
    LPCTSTR lpFile, // pointer to filename or folder name string
    LPCTSTR lpParameters, // pointer to string that specifies executable-file parameters
    LPCTSTR lpDirectory, // pointer to string that specifies default directory
    INT nShowCmd // whether file is shown when opened
    );

    You can use ShellExecute to open or explore a shell folder. To open a folder, use either of the following calls:

    ShellExecute(handle, NULL, path_to_folder, NULL, NULL, SW_SHOWNORMAL);
    Or
    ShellExecute(handle, “open”, path_to_folder, NULL, NULL, SW_SHOWNORMAL);

    To explore a folder, use the following call:

    ShellExecute(handle, “explore”, path_to_folder, NULL, NULL, SW_SHOWNORMAL);

  7. Chris Elens says:

    I’m looking for the option to show the external program on a component(panel eg) of my own application? Could you help me?

  8. Mark Knight says:

    Thanks for the information. Can we use IF on the shellexecute command?

    IF ShellExecute(handle,’open’,PChar(filename), ”,”,SW_SHOWNORMAL) =<32 THEN MessageDlg('An error has occurred.', mtError, [mbOk], 0, mbOk) ELSE ShellExecute(handle,'open',PChar(filename), '','',SW_SHOWNORMAL);

    If not, what is the trapping procedure?

Leave a Reply

Your email address will not be published. Required fields are marked *


3 + nine =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">