// ShortCut.cpp : Defines the entry point for the application.
// Coded by: Mehedi Shams Rony, mehedishams@yahoo.com
// This program creates shortcut for a program on desktop and on Start Menu -> Programs.
// For simplicity and ease of use from any other programs coded in any other language,
// I have provided the provision of using a text file that holds the paths.
// Assuemd hereby, there is a file named "Paths.txt" in C: drive that holds the following
// two entries:
// C:My Program.exe
// y
// The first line is file name to be run when the shortcut is double-clicked.
// The second entry indicates that a desktop shortcut is also needed. If not needed, simply
// omit the second line.
// This program is only a prototype that creates only one shortcut. If there are more than
// one shortcut needed, please modify the code providing enough fgets, and enough CreateLink
// functions.
#include "stdafx.h"
#include <windows.h>
#include <string.h>
#include <shlobj.h>
#include <stdio.h>
void CreateLink(int, char *, char *);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
char ProgramPath[_MAX_PATH-1];
char DeskTopShortcutNeeded[2];
// Obtaining shortcut names and target paths from the text file.
FILE *fp = fopen("C:Paths.txt", "r");
// Assumed shortcut for only one file is needed.
fgets(ProgramPath, _MAX_PATH-1, fp);
// ToDo: add code here to get paths to other files.
fscanf(fp, "%s", DeskTopShortcutNeeded);
DeleteFile("C:Paths.txt");
fclose(fp);
if (ProgramPath[strlen(ProgramPath)-1] == 'n')
ProgramPath[strlen(ProgramPath)-1] = NULL;
char LinkName[_MAX_PATH-1] = "My Program";
CreateLink(CSIDL_COMMON_PROGRAMS, LinkName, ProgramPath);
// ToDo: add code here to make link to other files if more than one shortcut is needed.
if (!strcmp(DeskTopShortcutNeeded, "y")) // Create the link on the desktop if required.
{
strcpy(LinkName, "My Program");
CreateLink(CSIDL_COMMON_DESKTOPDIRECTORY, LinkName, ProgramPath);
}
return 0;
}
void CreateLink(int SpecialFolderName, char *LinkName, char *FilePath)
{
CoInitialize(NULL); // Initialize the COM library on the current thread.
// File system directory that contains the directories for the
// common program groups that appear on the Start menu for all users.
LPITEMIDLIST pidl;
// Get a pointer to an item ID list that represents the path of a special folder.
HRESULT hr = SHGetSpecialFolderLocation(NULL, SpecialFolderName, &pidl);
// Convert the item ID list's binary representation into a file system path.
char SpecialFolderPath[_MAX_PATH-1];
// SpecialFolderPath is now Start Menu -> Programs path.
BOOL f = SHGetPathFromIDList(pidl, SpecialFolderPath);
// Allocate a pointer to an IMalloc interface
LPMALLOC pMalloc;
// Get the address of our task allocator's IMalloc interface
hr = SHGetMalloc(&pMalloc);
// Free the item ID list allocated by SHGetSpecialFolderLocation
pMalloc->Free(pidl);
// Free our task allocator
pMalloc->Release();
if (SpecialFolderName == CSIDL_COMMON_PROGRAMS)
{
char MyDirectory[_MAX_PATH-1];
// SpecialFolderPath = Start Menu -> Programs or Desktop path.
strcpy(MyDirectory, SpecialFolderPath);
// MyDirectory = Start Menu -> Programs -> My Program or Desktop Path -> My Program
strcat(MyDirectory, "My Program");
// Create Directory as Start Menu -> Programs -> My Program or Desktop Path -> My Program
CreateDirectory(MyDirectory, NULL);
char szTemp[_MAX_PATH-1];
strcpy(szTemp, MyDirectory);
strcat(szTemp, "");
strcat(szTemp, LinkName );
strcpy(LinkName, szTemp);
strcat(LinkName, ".lnk"); // LinkName should have .lnk extension."
}
else
{
char szTemp[_MAX_PATH-1];
strcpy(szTemp, SpecialFolderPath);
strcat(szTemp, "");
strcat(szTemp, LinkName );
strcpy(LinkName, szTemp);
strcat(LinkName, ".lnk"); // LinkName should have .lnk extension."
}
HRESULT hres = NULL;
IShellLink* psl = NULL;
// Get a pointer to the IShellLink interface.
hres = CoCreateInstance(CLSID_ShellLink,
NULL,
CLSCTX_INPROC_SERVER,
IID_IShellLink,
reinterpret_cast<void**>(&psl));
if (SUCCEEDED(hres))
{
IPersistFile* ppf = NULL;
psl->SetPath(FilePath); // Set the path to the shortcut target
char buf[_MAX_PATH-1];
::GetCurrentDirectory(_MAX_PATH, buf);
strcat(buf,"IconDLL.dll");
psl->SetIconLocation(buf, 0);
// Query IShellLink for the IPersistFile interface for saving
// the shortcut in persistent storage.
hres = psl->QueryInterface(IID_IPersistFile, reinterpret_cast<void**>(&ppf));
if (SUCCEEDED(hres))
{
WCHAR WideCharacterBuffer[_MAX_PATH-1];
// Ensure that the string is ANSI.
MultiByteToWideChar(CP_ACP, 0, LinkName, -1, WideCharacterBuffer, MAX_PATH);
// Save the link by calling IPersistFile::Save.
hres = ppf->Save(WideCharacterBuffer, TRUE);
ppf->Release();
}
psl->Release();
}
}