Darkbyte Library

Nonstandard Web Browser

It appears that your browser does not comply with the W3C web standards which define how web pages are encoded, transmitted, and rendered. This site would look much better in a standards-compliant web browser, but its content is accessible to any browser or Internet device.

CreateShortcut() :: Generate Shortcuts using the COM IShellLink interface.

Download

License : LGPL

Description :
Since I could only find MFC, .NET, VB or C++ code samples to create shortcuts I decided to write a pure C version for Win32 API programmers.
To create a shortcut we need to go through 7 basic steps ...
  1. Initialise COM via CoInitialize();
  2. Create and IShellLink instance using CoCreateInstance();
  3. Set the path of the file we're going to link to using SetPath();
  4. Retrieves a pointer to the IPersistFile interface using QueryInterface();
  5. Create the Shortcut using Save();
  6. Release() the IShellLink interface.
  7. Free up the COM session using CoUninitialize();
The demo application shows hoe to create a Shortcut from the commandline.

Prototype :

BOOL CreateShortcut( PCHAR pcDestPath, PCHAR pcStartInPath, PCHAR pcShortcutName )
Definitions :
PCHAR pcDestPath
Pointer to character array containing the path to create the shortcut in.
E.g. "C:\Windows\Desktop"
PCHAR pcStartInPath
Pointer to character array containing the path to start the Shortcut in.
E.g. "C:\Program Files\MyApp"
PCHAR cShortcutName
Pointer to character array containing the name of the shortcut to create ( without .lnk ).
E.g. "Shortcut To My App"

Source Code :


/**************************************************************************
 * createshortcut.c
 * ----------------
 * Author: Alan Doyle (alan.doyle@darkbyte.com)
 * Copyright: (c) 2005 Darkbyte Software
 * Release Version: 1.2
 * Date Started: 2005/03/04
 *
 * Create Shortcut ... Win32 API Version.
 *
 **************************************************************************
 *
 *   createshortcut.c is free software; you can redistribute it and/or modify
 *   it under the terms of the Lesser General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   createshortcut.c is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the Lesser General Public License
 *   along with createshortcut.c; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 *   USA
 *
 *************************************************************************/
 
#include <stdio.h>
#include <shlobj.h>
 
/*  ---------------------------------------------------------------------[<]-
	Function: CreateShortcut
 
	Synopsis: Create 'pcShortcut' with start path 'pcDestFile' in folder
			  'pcDestDir'.
	Returns : TRUE if successful, otherwise FALSE.
	---------------------------------------------------------------------[>]-*/
 
BOOL CreateShortcut( PCHAR pcDestDir, PCHAR pcDestFile, PCHAR pcShortcut )
{
	IShellLink*   psl;
	HRESULT       hres;
	CHAR          cLinkFile[MAX_PATH];
	IPersistFile* ppf;
	BOOL          bRetVal = FALSE;
 
	hres = CoInitialize(NULL);
	if (!SUCCEEDED(hres))
	{
		/* Could not open the COM library */
		return bRetVal;
	}
	sprintf(cLinkFile,"%s\\%s.lnk", pcDestDir, pcShortcut);
 
	hres = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
	                        &IID_IShellLink, (LPVOID *)&psl);
	if (SUCCEEDED(hres))
	{
		hres = psl->lpVtbl->SetPath(psl,pcDestFile);
		if (SUCCEEDED(hres))
		{
			hres = psl->lpVtbl->QueryInterface(psl, &IID_IPersistFile, &ppf);
			if (SUCCEEDED(hres))
			{
				WORD wsz[MAX_PATH];
 
				MultiByteToWideChar(CP_ACP, 0, cLinkFile, -1, wsz, MAX_PATH);
 
				hres = ppf->lpVtbl->Save(ppf, wsz, STGM_READ);
				if (SUCCEEDED(hres))
					bRetVal = TRUE;
				ppf->lpVtbl->Release(ppf);
			}
		}
		psl->lpVtbl->Release(psl);
	}
 
	CoUninitialize();
	return bRetVal;
}
Copyright © 2000-2008 Darkbyte Software, All Rights Reserved. 
Please direct your questions or comments to

CCBot/1.0 (+http://www.commoncrawl.org/bot.html) [38.103.63.57 ( )]