Creating Internet Shortcuts | CodeGuru

Creating Internet Shortcuts

Environment: VC6 SP2, 95/98/NT This article shows you how to create an Internet shortcut for Windows 95, 98, and NT computers. Creating internet shortcut is little different than the general shortcut, so the code for Internet shortcut creation is shown. Before you see the code, you should understand the variables I’m using: pszURL – url […]

Written By
CodeGuru Staff
CodeGuru Staff
Jan 25, 2000
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Environment: VC6 SP2, 95/98/NT

This article shows you how to create an Internet shortcut for Windows 95, 98, and NT
computers. Creating internet shortcut is little different than the general shortcut, so
the code for Internet shortcut creation is shown.

Before you see the code, you should understand the variables I’m using:
pszURL – url location e.g ‘http://www.sample.com/index/html’ Note: You can use a syntax such as file://Arguments for
local files.
pszURLFileName – shortcut filename, should have extension of .url otherwise
does not work. e.g ;"C:test.url"
LPCSTR szDescription – description of the shortcut
LPCTSTR szIconFile – icon file(could any .exe ot .dll)
int index – index of the icon resource in above .exe or .dll

HRESULT CreateInterShortcut (LPCSTR pszURL, LPCSTR pszURLfilename,
	LPCSTR szDescription,LPCTSTR szIconFile = NULL,int nIndex = -1)
{
 HRESULT hres;

 CoInitialize(NULL);

 IUniformResourceLocator *pHook;

 hres = CoCreateInstance (CLSID_InternetShortcut, NULL, CLSCTX_INPROC_SERVER,
  IID_IUniformResourceLocator, (void **)&pHook);

 if (SUCCEEDED (hres))
 {
  IPersistFile *ppf;
  IShellLink *psl;

  // Query IShellLink for the IPersistFile interface for
  hres = pHook->QueryInterface (IID_IPersistFile, (void **)&ppf);
  hres = pHook->QueryInterface (IID_IShellLink, (void **)&psl);

  if (SUCCEEDED (hres))
  {
   WORD wsz [MAX_PATH]; // buffer for Unicode string

   // Set the path to the shortcut target.
   pHook->SetURL(pszURL,0);

   hres = psl->SetIconLocation(szIconFile,nIndex);

   if (SUCCEEDED (hres))
   {
    // Set the description of the shortcut.
    hres = psl->SetDescription (szDescription);

    if (SUCCEEDED (hres))
    {
     // Ensure that the string consists of ANSI characters.
     MultiByteToWideChar (CP_ACP, 0, pszURLfilename, -1, wsz, MAX_PATH);

     // Save the shortcut via the IPersistFile::Save member function.
     hres = ppf->Save (wsz, TRUE);
    }
   }

   // Release the pointer to IPersistFile.
   ppf->Release ();
   psl->Release ();
  }

  // Release the pointer to IShellLink.
  pHook->Release ();

 }
 return hres;
}

Downloads

Download function source code – 1 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.