Click here to Skip to main content
15,885,216 members
Articles / Desktop Programming / Win32

A simple method to create a shortcut to an executable file

Rate me:
Please Sign up or sign in to vote.
1.77/5 (5 votes)
19 Feb 2008CPOL 62.7K   424   9   3
A simple method to create a shortcut to an executable file.

Introduction

It is not easy to create a shortcut for an executable file in program. William Rayer has a good article to introduce a method about it; you could see that here. But, that solution may be complex to some people. There is a simple method which can do the same thing.

Background

As we all know, a shortcut is a *.lnk file which has a complex content. *.URL has the same function as *.lnk, but its content is very simple. For example:

======================================== 
[InternetShortcut] 
URL=C:\MyFolder\Test.exe 
IconIndex=0 
IconFile=C:\MyFolder\Test.exe 
========================================

Save the the file, and we can get a shortcut to Test.exe.

Sample program

We will see how to create a shortcut using code. We can use 'WritePrivateProfileString' to create a *.URL easily.

C++
#include <windows.h>
#include <stdio.h>

BOOL CreateShortcut(char *ptchExecutableFileName, char *ptchShortcutName)
{
    BOOL Res = TRUE;
    FILE *fp;
    
    if(NULL == (fp = fopen(ptchExecutableFileName, "r")))
    {
        printf("\nCan`t find executable file!\n");
        return FALSE;
    }
    fclose(fp);
    
    Res &= WritePrivateProfileString("InternetShortcut", 
               "URL", ptchExecutableFileName, ptchShortcutName);
    Res &= WritePrivateProfileString("InternetShortcut", 
               "IconIndex", "0", ptchShortcutName);
    Res &= WritePrivateProfileString("InternetShortcut", 
               "IconFile", ptchExecutableFileName, ptchShortcutName);
    
    return Res;
}


/* 
    For example: create a shortcut on desktop for e:\Test.exe 
    CreateShortcut("E:\\Test.exe", 
      "C:\\Documents and Settings\\Administrator\\Desktop\\Test.url");
//*/

Existing issue

The method can create a shortcut to an executable file normally. But I don't know how to create a shortcut to a folder. If there is a folder c:\MyFolder, we make the following file:

======================================== 
[InternetShortcut] 
URL=C:\MyFolder
IconIndex=0 
IconFile=C:\MyFolder 
========================================

Save it. When you click the icon of the file, you can open the folder, but the display of the icon is abnormal!

If you known a solution to this, please share it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
China China
^_^

Comments and Discussions

 
GeneralMy vote of 5 Pin
Frank T. Clark3-Jun-15 11:27
professionalFrank T. Clark3-Jun-15 11:27 
QuestionHow to get the folder icon path and icon index for a given folder name Pin
Stephan Poirier19-Feb-08 17:59
Stephan Poirier19-Feb-08 17:59 
Hi June,

This is one of the many way you can use to retrieve one folder's icon location.
You may use the SHGetFileInfo API.

As an example, on my computer, I'd set the icon of a folder to something more "attractive" than a plain Windows looking folder. So I knew therefore that my icon should not be located in the shell32.dll resources.
<code>CString szPath = "F:\\Codes\\";

SHFILEINFO shfi;
memset(&shfi,0,sizeof(shfi));

SHGetFileInfo(  szPath , 
                FILE_ATTRIBUTE_DIRECTORY,
                &shfi, 
                sizeof(shfi),
                SHGFI_ATTRIBUTES | SHGFI_ICONLOCATION  );
</code>


SHFILEINFO::szDisplayName gives the iconFile location path
SHFILEINFO::iIcon gives the icon index

So in my current example, if I give a look inside the local SHFILEINFO variable, I'll find that shfi.szDisplayName is "C:\Icons\FauxS-XP\Program Folders.ico" and that shfi.iIcon = 0.

Any uncustomized folder will point to "%systemdir%\\SHELL32.dll" with iconindex equal to 3.

Cheers,
Stef

If progamming's like taking drugs...
I think I'll do an overdose. Poke tongue | ;-P

AnswerRe: How to get the folder icon path and icon index for a given folder name Pin
Jude Deng20-Feb-08 13:22
Jude Deng20-Feb-08 13:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.