Partager via


SHCreateShortcut (Compact 2013)

3/28/2014

This function creates a shortcut.

Syntax

DWORD WINAPI SHCreateShortcut(
  LPTSTR szShortcut, 
  LPTSTR szTarget 
);

Parameters

  • szShortcut
    [in] Null-terminated string that contains the path and name of the shortcut to create. The shortcut is created at the location identified by the path.
  • szTarget
    [in] Null-terminated string that contains the target path and arguments for the shortcut. The size of the buffer is limited to 256 characters. Place quotes around the target path so that SHCreateShortcut can correctly parse the target file from any following arguments.

Return Value

TRUE indicates success. FALSE indicates failure. FALSE is also returned if the specified shortcut already exists.

Remarks

This function is not prototyped correctly in Shellapi.h. It returns a BOOL instead of a DWORD.

Use the szShortcut parameter to specify the location and display name for the shortcut and the szTarget parameter to specify the path to the application or file that you are creating a shortcut to. For example, if your platform supports Windows Media Player and you want to add a shortcut to it in the My Documents folder, set szShortcut to "\My Documents\Windows Media Player.lnk" and szTarget to "\Windows\ceplayer.exe". This is shown in the following code, which creates a shortcut titled "Windows Media Player" that launches the Ceplayer.exe application when activated.

SHCreateShortcut( _T("\\My Documents\\Windows Media Player.lnk"),
                  _T("\\Windows\\Ceplayer.exe"));

If you want to create a shortcut to a file that needs to launch an application to expose the file's contents, you can create a shortcut to the launching application and include the file name as an argument in szTarget. For example, to create a shortcut to play an introductory video named IntroVideo.wmv in Windows Media Player, you could set szShortcut to "\My Documents\Introductory Video.lnk" and szTarget to "\Windows\ceplayer.exe IntroVideo.wmv". When this shortcut is activated, the media player application launches and plays IntroVideo.wmv.

Alternately, if the launching application does not accept a file argument then you could create a shortcut to the file using an .lnk extension rather than its native file extension. To handle the previous example with this method, set szShortcut to "\My Documents\Introductory Video.lnk" and szTarget to "\Windows\IntroVideo.lnk". This has the same result as before.

Passing a NULL value for szShortcut will return ERROR_INVALID_PARAMETER.

Code Example

The following code example demonstrates how to use SHCreateShortcut.

Note

To make the following code example easier to read, security checking and error handling are not included. This code example should not be used in a release configuration unless it has been modified to include them.

BOOL SHCreateShortcutExample()
{
    // Create a shortcut called myAppShortcut.lnk, 
    // that links to the target file in \Windows\Program Files, named myApp.exe.
    // Place the shortcut in the folder \Windows.
    return SHCreateShortcut(TEXT("\\Windows\\myAppShortcut.lnk"), TEXT("\\Windows\\Program Files\\myApp.exe"));
}

Requirements

Header

shellapi.h

Library

coredll.lib

See Also

Reference

Shell Functions