• Blog
  • How to use ShellExecute?

How to use ShellExecute?

What is ShellExecute

Publish date:
Discover more of what matters to you

If you are asking yourself:

– How do I open this file with its default program without knowing where that program is installed?

– How do I launch common system actions through a simple interface without writing complex, low-level OS-specific code? 

– How do I perform basic file operations with minimal code? 

There is a solution you can use – it is called ShellExecute. 

What is ShellExecute?

ShellExecute is a Windows API function used to perform various operations related to file manipulation or system interactions by launching or executing a specified file or operation associated with the default action for the specified file or application. 

This function allows users to open files, folders, and URLs, and perform various actions associated with them, such as open, edit, print, explore, etc., from app’s menu, depending on the specified guidelines. In Delphi, it is used similarly to how it’s used in other programming languages for Windows.

It is a versatile function that serves several purposes:

  • Launching Applications: It can be used to start any applications. For example, you can use it to open Notepad, launch a web browser, or execute any executable file.
  • Opening Files: It can open files using the default program associated with their file types. For instance, it can open text files, documents, images, videos, etc., using their default associated applications.
  • Performing Actions on Files: It allows performing various actions associated with files, such as editing, printing, and exploring (opening a folder to view its contents), among others.
  • Handling URLs: It can open URLs in the default web browser. For instance, it can open a specific website link using the default web browser.

Developers use this function in Windows programming to initiate these kinds of operations and interactions within their applications. It relies on the default associations set up in the Windows Shell interface, allowing applications to leverage the default applications and behaviors set by the user for different file types and protocols.

Its flexibility makes it a widely used function in programming scenarios where there is a need to trigger various file-related operations or launch applications associated with specific file types or URLs.

How to use ShellExecute: Guide

Our team has rich expertise in Delphi development services which allowed us to accumulate a lot of valuable insights related to this technology. So, to use this function in Delphi, follow these steps:

  • Include ShellAPI unit: Ensure that the ShellAPI (or Winapi.ShellAPI in current versions of Delphi) unit is included in your uses clause.
123
uses
  Winapi.ShellAPI;
  • Call ShellExecute function: In Delphi, it can be called with the appropriate parameters to perform the desired action.

Here’s an example demonstrating how to use this function to open a text file:

1234567
procedure TForm1.Button1Click(Sender: TObject);
var
FilePath: string;
begin
FilePath := 'C:\Path\To\Your\File.txt';
ShellExecute(0, 'open', PChar(FilePath), nil, nil, SW_SHOWNORMAL);
end;

This code uses these to open the file located at ‘C:\Path\To\Your\File.txt’ using the default program associated with text files in the system (‘open’ operation). You can change the operation or file path according to your specific use case.

The parameters for ShellExecute in Delphi are:

  • hWnd: Handle to the window that will own the newly created window, or 0 if no window is to be created.
  • Operation: The operation to perform (‘open’, ‘explore’, ‘print’, etc.).
  • FileName: The name of the file or object on which to execute the specified operation.
  • Parameters: Parameters to be passed to the application specified by FileName.
  • Directory: The default directory for the application.
  • ShowCmd: Flags that specify how an application window is to be displayed.

Adjust the parameters according to your specific requirements and desired action. In Delphi, ShellExecute parameters could include opening files, launching applications, opening URLs, and more.

Additional examples

Here is one more example for developers:

12345678910111213141516
uses
ShellAPI;
procedure TForm1.Button1Click(Sender: TObject);
var
URL: string;
begin
URL := 'https://www.softacom.com/';
ShellExecute(0, 'open', PChar(URL), nil, nil, SW_SHOWNORMAL);
end;
procedure TForm1.Button2Click(Sender: TObject);
var
FileName: string;
begin
FileName := 'C:\Users\User\file.txt';
ShellExecute(0, 'print', PChar(FileName), nil, nil, SW_SHOWNORMAL);
end;

Explanation of the ShellExecute parameters:

  • hWnd: Handle to the window that will own the newly created window. Set to 0 to indicate no window association.
  • Operation: The operation to perform, such as ‘open’, ‘edit’, ‘print’, etc.
  • FileName: The file or object on which to execute the specified operation.
  • Parameters: Additional parameters passed to the application (can be nil if not needed).
  • Directory: The default directory for the application.
  • ShowCmd: Specifies how the window is to be displayed (e.g., SW_SHOWNORMAL, SW_HIDE, etc.).

Ensure you include the ShellAPI unit in your uses clause to access the ShellExecute function.

If you have any questions related to any ShellExecute Delphi example or you need to get more details on how to work with this function, we will be happy to answer your questions.

Subscribe to our newsletter and get amazing content right in your inbox.

This field is required
This field is required Invalid email address
By submitting data, I agree to the Privacy Policy

Thank you for subscribing!
See you soon... in your inbox!

confirm your subscription, make sure to check your promotions/spam folder

Get in touch
Our benefits
  • 17+ years of expertise in legacy software modernization
  • AI Migration Tool:
    faster timelines, lower costs, better accuracy (99.9%)
  • Accelerate release cycles by 30–50% compared to manual migration
  • 1–2 business day turnaround for detailed estimates
  • Trusted by clients across the USA, UK, Germany, and other European countries
Review
Thanks to Softacom's efforts, the solutions they delivered are already in use and have increased revenue streams.
  • Niels Thomassen
  • Microcom A/S
This field is required
This field is required Invalid email address Invalid business email address
This field is required
By submitting data, I agree to the Privacy Policy
We’ll reply within 24 hours — no sales talk, no spam