Click here to Skip to main content
15,885,051 members
Articles / Desktop Programming / MFC

CShellLink & CUrlShellLink v1.1

Rate me:
Please Sign up or sign in to vote.
3.73/5 (4 votes)
3 Mar 2000 115.3K   1.4K   24  
2 Freeware MFC classes to encapsulate shell shortcuts.
#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include "ShellLink.h"

void main()
{
  //Initialise the OLE subsystem
  HRESULT hRes = ::CoInitialize(NULL);
  if (!SUCCEEDED(hRes))
  {
    TRACE(_T("Failed to initialize OLE\n"));
    return;
  }


  //Test all the functionality of the classes

  //First Saving
  CShellLinkInfo sli1;
  sli1.m_sTarget = _T("notepad.exe");
  sli1.m_sArguments = _T("c:\\config.sys");
  sli1.m_sWorkingDirectory = _T("c:\\");
  sli1.m_sDescription = _T("Edit your config.sys");
  sli1.m_sIconLocation = _T("shell32.dll");
  sli1.m_nIconIndex = 21; //Use an interesting icon
  sli1.m_nShowCmd = SW_MAXIMIZE;
  CShellLink sl1;
  if (sl1.Create(sli1))
  {
    if (!sl1.Save(_T("c:\\Edit your config.sys.lnk")))
      TRACE(_T("Failed to save link\n"));
    else
      _tprintf(_T("A shortcut to edit your config.sys had been created on you C:\\ drive\n"));
  }
  else
    TRACE(_T("Failed to create link\n"));

  //Now loading
  CShellLink sl2;
  if (!sl2.Load(_T("c:\\Edit your config.sys.lnk")))
    TRACE(_T("Failed to open link\n"));

  //Now Resolving
  if (!sl2.Resolve(NULL, SLR_ANY_MATCH))
    TRACE(_T("Failed to resolve link\n"));
  
  //Now try saving a URL shortcut
  CShellLinkInfo sli2;
  sli2.m_sTarget = _T("http://indigo.ie/~pjn");
  sli2.m_sWorkingDirectory = _T("c:\\");
  sli2.m_sDescription = _T("Visit the authors Web Site");
  sli2.m_sIconLocation = _T("shell32.dll");
  sli2.m_nIconIndex = 14; //Use an interesting icon
  sli2.m_nShowCmd = SW_MAXIMIZE;
  CUrlShellLink sl4;
  if (sl4.Create(sli2))
  {
    if (!sl4.Save(_T("c:\\PJ Naughters Web Site.url")))
      TRACE(_T("Failed to save URL link\n"));
    else
      _tprintf(_T("A shortcut to my web site had been created on you C:\\ drive\n"));
  }
  else
    TRACE(_T("Failed to create URL link\n"));
  
  //and loading a URL shortcut
  CUrlShellLink sl5;
  if (!sl5.Load(_T("c:\\PJ Naughters Web Site.url")))
    TRACE(_T("Failed to load URL link\n"));

  //and loading a URL shortcut
  if (!sl5.Invoke(NULL, IURL_INVOKECOMMAND_FL_USE_DEFAULT_VERB, _T("")))
    TRACE(_T("Failed to invoke URL link\n"));
  else
    _tprintf(_T("Now conneting to my web site\n"));


  //Closedown the OLE subsystem
  ::CoUninitialize();

  _tprintf(_T("Till Next time!!!, Press any key to exit\n"));
  int ch = getch();
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions