HOWTO: Create Shortcuts to URLs with IUniformResourceLocator

ID: Q229092


The information in this article applies to:


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;
} 

Additional query words:


Keywords          : kbActiveX kbLinks kbGrpShell 
Version           : WINDOWS:4.0,4.01,4.01 SP1,4.01 SP2,5.0; winnt:4.0,4.0 SP4,5.0
Platform          : WINDOWS winnt 
Issue type        : kbhowto 

Last Reviewed: May 26, 1999