How To Create Shortcuts to URLs with IUniformResourceLocator (229092)



The information in this article applies to:

  • Microsoft Windows 98, when used with:
    • the operating system: Microsoft Windows 95
    • the operating system: Microsoft Windows 98
  • Microsoft Windows NT Server 4.0
  • Microsoft Windows NT Server 4.0 SP4
  • Microsoft Windows NT Workstation 4.0
  • Microsoft Windows NT Workstation 4.0 SP4
  • Microsoft Internet Explorer (Programming) 4.0
  • Microsoft Internet Explorer (Programming) 4.01
  • Microsoft Internet Explorer (Programming) 4.01 SP1
  • Microsoft Internet Explorer (Programming) 4.01 SP2

This article was previously published under Q229092

SUMMARY

IShellLink is used to create shortcuts to files. Similarily IUniformResourceLocator can be used to create shortcuts to URLs.

MORE INFORMATION

The following sample code shows how to use the interface IUniformResourceLocator to create a shortcut to a URL. It creates a URL shortcut file at "C:\Mssupport.url" that points to the URL "http://support.microsoft.com."
#include <windows.h>
#include <intshcut.h>

HRESULT CreateShortcutToURL(LPCSTR pszURL, LPCSTR pszLinkFile);

int main()
{
  CoInitialize(NULL);
  HRESULT hRes = CreateShortcutToURL("http://support.microsoft.com","c:\\mssupport.url");
  if (SUCCEEDED(hRes))
  {
    // do something...
  }
  CoUninitialize();
  return 0;
}

HRESULT CreateShortcutToURL(LPCSTR pszURL, LPCSTR pszLinkFile)
{
  HRESULT hRes;
   IUniformResourceLocator *pURL = NULL;

  // Create an IUniformResourceLocator object
  hRes = CoCreateInstance (CLSID_InternetShortcut, NULL, 
    CLSCTX_INPROC_SERVER, IID_IUniformResourceLocator, (LPVOID*) &pURL);
  if (SUCCEEDED(hRes))
  {
    IPersistFile *pPF = NULL;

    hRes = pURL->SetURL(pszURL, 0);

    if (SUCCEEDED(hRes))
    {
      WCHAR wsz [MAX_PATH];   // buffer for Unicode string

      // Ensure that the string consists of ANSI characters.
      MultiByteToWideChar (CP_ACP, 0, pszLinkFile, -1, wsz, MAX_PATH);

      hRes = pURL->QueryInterface (IID_IPersistFile, (void **)&pPF);
      if (SUCCEEDED(hRes))
      {   
        // Save the shortcut via the IPersistFile::Save member function.
        hRes = pPF->Save (wsz, TRUE);

        // Release the pointer to IPersistFile.
        pPF->Release ();
      }
    }
    // Release the pointer to IUniformResourceLocator
    pURL->Release ();
  }
  return hRes;
}
				

Modification Type:MinorLast Reviewed:12/21/2004
Keywords:kbhowto KB229092