// ********************************************************************** // // ** This file was downloaded from Jeff Heaton's Source Code Examples ** // // ** www.heat-on.com/source ** // // ********************************************************************** // // Copyright 1999 by Jeff Heaton // ********************************************************************** // // Permission is granted for royaltyfree use in compiled form. No // additional licensing is required. This code may not be distributed // in source form without written premission by Jeff Heaton. Generally // I will grant it. Just ask first. // // Basically I do not care how you distribute this code in compiled form. // But if your going to publish my source code somewhere I'd like to hear // about it first. // ********************************************************************** // // CreateLink V1.0 // Written 2/18/1999 by Jeff Heaton // // Create a Windows shortcut(link). // // This source file presents a very simple function that can be used to create // shortcuts(.lnk files) in Microsoft Windows. // CreateLink uses the shell's IShellLink and IPersistFile interfaces // to create and store a shortcut to the specified object. // Function Name: CreateLink // Parameters: // lpszPathObj - The path to the origional filename // lpszPathLink - The path to the newly created link // lpszDesc - The description for the link // Returns: // The HRESULT for the operation. // Purpose: // This function create Windows shortcuts. // Author // Jeff Heaton HRESULT CreateLink(LPCSTR lpszPathObj, LPSTR lpszPathLink, LPSTR lpszDesc) { HRESULT hres; IShellLink* psl; IPersistFile* ppf; WORD wsz[MAX_PATH]; // Get a pointer to the IShellLink interface. hres = CoCreateInstance(CLSID_ShellLink, NULL,CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&psl); if (SUCCEEDED(hres)) { // Set the path to the shortcut target and add the // description. psl->SetPath(lpszPathObj); psl->SetDescription(lpszDesc); // Query IShellLink for the IPersistFile interface for saving the // shortcut in persistent storage. hres = psl->QueryInterface( IID_IPersistFile, (void**)&ppf); if (SUCCEEDED(hres)) { // Ensure that the string is ANSI. MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1, wsz, MAX_PATH); // Save the link by calling IPersistFile::Save. hres = ppf->Save( wsz, TRUE); ppf->Release(); } psl->Release(); } return hres; }