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.
#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;
}
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.