Jump to content
PeterPanettone

Creating a Shell-Link with hotkey property

Recommended Posts

"A Shell link is a data object that contains information used to access another object in the Shell's namespace": Read the whole definition here:

 

https://docs.microsoft.com/en-us/windows/win32/shell/links

 

I was searching for a Delphi VCL library allowing me to easily create a Shell-Link with all properties described in the Microsoft documentation:

 

https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nn-shobjidl_core-ishelllinkw

 

I found a lot of libraries for this purpose, but most of them were outdated or had some flaws or bugs. Most of the libraries lacked the feature to create a Shell-Link with a Hotkey.

 

The ShellBrowser Delphi VCL Library from JamSoftware contains a method to easily create a Shell-Link:

 

https://www.jam-software.com/shellbrowser_delphi/index.shtml

 

Unfortunately, it has no easy method to configure the properties (especially the hotkey property) of the newly created Shell-Link.

 

Fortunately, the above mentioned Microsoft documentation provides all the information to succeed in the task:

 

image.thumb.png.23c56d7c314890fcaef26b2490c0451d.png

 

I use a standard THotKey control in the UI to allow the user to configure a hotkey for the newly created Shell-Link:

 

image.png.6eadbe8382a7ead5e34e7988a9439107.png

 

The THotKey control has these main properties - HotKey and Modifiers:

 

image.png.c9a662296ab79cc91a8de39850aee8f9.png     

 

image.png.4efd6fc84e0b93a3314945cd04206323.png

 

So the whole process of creating the Shell-Link with the Hotkey properties can be achieved with these steps:

 

1. Get the ItemIdList of the created Link:

 

image.thumb.png.fe5e629f2613365f16b3a4edb72ffd68.png

 

2. Provide the IShellLink Interface of the ItemIdList:

 

image.thumb.png.3b0e394de7d6f174101ebd8302940af9.png

 

3. Declare the IPersistFile interface for the Shell-Link file:

 

image.thumb.png.b1bb2333aeac87e9fa6262e703b4295c.png

 

4. Now we need (as mentioned in the Microsoft documentation) to store the virtual key code in the low-order byte and the modifier flags in the high-order byte of the Shell-Link Hotkey property. For this purpose we declare a record containing two byte-fields:

 

image.png.ed4f418dfb21d7e2c4919e27fb136cf6.png

 

Then we get the Hotkey from the THotKey control, assign it to the record and typecast the record to a Word:

 

image.png.d9ad2aa2bec53b8f1ba5dfe810129e3a.png

 

But the Modifiers value is still missing from the record. So next, we get the Modifier-keys from the THotKey control and assign them to the Modifier Byte of the record:

 

image.thumb.png.18d40741c0ea4e969282d67366dbb569.png

 

Now we can assign the record (again typecasted to a Word) to the Shell-Link interface:

 

image.png.ec49d57ee09ef21ed404f6d56162560b.png

 

5. The other Shell-Link properties are easy to set and are explained in the Microsoft documentation.

 

6. In the last step we can now save the configured Shell-Link:

 

image.thumb.png.24ec3696d44d1c35045cb25222c557f0.png

 

You can download the source code and learn from its implementation:

 

ShellLinkShortcutTest.zip

 

image.thumb.png.6a7f46ba0b71438d956aea4b9c514817.png

 

Here is a compiled exe demo of the program:

 

ShellLinkShortcutTest_CompiledExeDemo.zip

 

To compile the source code yourself you need the ShellBrowser library. You can download a free trial version here:

 

https://customers.jam-software.de/shellbrowser_delphi/download.shtml?language=EN

 

The many other features of the ShellBrowser library are explained here:

 

https://www.jam-software.com/shellbrowser_delphi/features.shtml

 

I am not affiliated with JamSoftware, but I recommend the ShellBrowser library as an excellent product.

 

I wish you a lot of fun with this demo!

  • Like 2

Share this post


Link to post

They killed Kenny! Strange, I just opened it 5 minutes ago 😄

Try it later again, its a thin unit to do the same. (As I understand your needs)

Share this post


Link to post
48 minutes ago, Attila Kovacs said:

Compiler (Delphi 10.3.1 Rio) unfortunately found these errors in the code:

 

[dcc32 Error] ShellLink.pas(93): E2188 Published property 'ItemIDList' cannot be of type POINTER
[dcc32 Error] ShellLink.pas(274): E2033 Types of actual and formal var parameters must be identical
[dcc32 Error] ShellLink.pas(337): E2033 Types of actual and formal var parameters must be identical
[dcc32 Error] ShellLink.pas(338): E2033 Types of actual and formal var parameters must be identical

Edited by PeterPanettone

Share this post


Link to post

And?

Change Published to Public and TWin32FindDataA to TWin32FindDataW or TWin32FindData, Dummy: Longint to Dummy: Cardinal

Edited by Attila Kovacs

Share this post


Link to post

This is how I'm using it.

    SCut := TWin32ShortCut.Create;
    try
      SCut.Location := ciDesktop;
      SCut.FileName := sp[0];
      SCut.AppPath := ResolvePath(sp[1]);
      SCut.WorkDir := ResolvePath(sp[2]);
      SCut.IconPath := ResolvePath(sp[3]);
      if l > 4 then
        if TryStrToInt(sp[4], i) then
          SCut.IconIndex := i;
      SCut.SaveShortCut;
    finally
      SCut.Free;
    end;

function ResolvePath(const Path: string): string;
var
  sp: TArray<string>;
  i: Integer;
begin
  sp := Path.Split(['%']);
  Result := '';
  for i := 0 to High(sp) do
    case i mod 2 of
      0:
        Result := Result + sp[i];
      1:
        Result := Result + GetEnvironmentVariable(sp[i]);
    end;
end;

 

Share this post


Link to post
4 minutes ago, Attila Kovacs said:

This is how I'm using it.

Loading a Shell-Link and reading its properties does not work with the above code:

 

1213759737_ClipboardImage(1).thumb.png.07dc1d357a9d4d9d4ac9ab6292ebf3b4.png

Share this post


Link to post
2 hours ago, PeterPanettone said:

I found a lot of libraries for this purpose, but most of them were outdated or had some flaws or bugs.

Confirmed.

Share this post


Link to post

I'm not sure if you missed the second line in the code "shortcut creation object".

I did not know that you want to edit existing shortcuts, sorry.

 

2 hours ago, PeterPanettone said:

I was searching for a Delphi VCL library allowing me to easily create a Shell-Link with all properties described in the Microsoft documentation:

 

 

I'm wasting here my time.

Confirmed.

 

Share this post


Link to post
2 minutes ago, Attila Kovacs said:

I did not know that you want to edit existing shortcuts

I just tried to use the linked unit. This has nothing to do with what I want. Sorry about that.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×