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


Comments

  • With Outlook verified and working

    Posted by Legacy on 02/18/2003 12:00am

    Originally posted by: Gangadaran M

    Dear sir,

    It's wright that will work under outlook only. Not from outlook express.

    It's fine. I have small query, NT or windows2000 does not have any tool to control the cunncurrent connections. Is there any code to control the no. of user in the Gigabit ethernet network.

    We have problem of users logging from several computers so we are experiencing the problems, let me know is there any solutions...

    Thanks,

    M.Gangadaran
    Bangalore

    Reply
  • an easier way

    Posted by Legacy on 05/28/2001 12:00am

    Originally posted by: Jocas

    //This code is a simpler version that does not require IE4.
    
    //
    //Make everything as simple as possible and no more. -Albert Einstein

    int CreateInterShortcut(const char *pszURL_c, const char *pszFileName_c,
    const char *pszIconFile_c = NULL, const int nIndex_c = -1)
    {
    //Create file in memory.
    CString sFileContents;
    sFileContents.Format("[InternetShortcut]\r\nURL=%s\r\n", pszURL_c);

    if(pszIconFile_c)
    {
    CString sTmp;
    sTmp.Format("IconFile=%s\r\n", pszIconFile_c);
    sFileContents += sTmp;
    }

    if(nIndex_c!=-1)
    {
    CString sTmp;
    sTmp.Format("IconIndex=%i\r\n", nIndex_c);
    sFileContents += sTmp;
    }

    //Write to disk.
    try
    {
    CFile vShortcut;
    if(!vShortcut.Open(pszFileName_c, CFile::modeCreate | CFile::modeWrite))
    return 0;
    vShortcut.Write(sFileContents, sFileContents.GetLength());
    vShortcut.Close();
    }
    catch(CFileException *err)
    {
    err->Delete();
    return 0;
    }

    return 1;
    };

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

  • You probably have several goals for your patient portal of choice. Is "community" one of them? With a bevy of vendors offering portal solutions, it can be challenging for a hospital to know where to start. Fortunately, YourCareCommunity helps ease the decision-making process. Read this white paper to learn more. "3 Ways Clinicians can Leverage a Patient Portal to Craft a Healthcare Community" is a published document owned by www.medhost.com

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds