Click here to Skip to main content
13,665,251 members
Click here to Skip to main content
Add your own
alternative version


28 bookmarked
Posted 10 May 2002

Creating and Resolving shell links

, 10 May 2002
Rate this:
Please Sign up or sign in to vote.
An MC++ class that uses IJW to invoke the IShellLink interface
<!-- Download Links -->


ShortCut is a .NET class that allows you to create shortcuts to files as well as resolve existing shortcuts. The class was coded in Managed C++ and makes ample use of IJW. I have compiled the class as a Class Library under the namespace ShortCutLib. Thus you can now use it from your C# and VB .NET programs. Simply add a reference to the DLL. This is yet another example of how Managed C++ eases the transition between managed and unmanaged code so easily. It just works!

Sample Usage [C#]

using System;
using ShortCutLib;

namespace ShortCutTest
    class Class1
        static void Main(string[] args)
            ShortCut sc = new ShortCut();

            //Creating a shortcut
            sc.FilePath = "C:\\windows\\notepad.exe";
            sc.LnkPath = "c:\\abc.lnk";
            sc.LnkDesc = "This runs notepad";
            sc.WorkDir = "C:\\";

            //Resolving a shortcut
            sc.LnkPath = "C:\\xyz.lnk";

            Console.WriteLine("File is {0}",sc.FilePath);
            Console.WriteLine("Description is {0}",sc.LnkDesc);
            Console.WriteLine("Woirking Dir is {0}",sc.WorkDir);

Source Listing (only the main header and cpp file)

For those of you who only have beta 2, you can use the following two source listings:

// ShortCut.h

#pragma once

using namespace System;
using namespace System::Runtime::InteropServices;

namespace ShortCutLib
    public __gc class ShortCut
            String* m_filepath;
            String* m_workdir;
            String* m_lnkdesc;
            String* m_lnkpath;

            HRESULT _CreateLink(LPCWSTR FilePath, LPCWSTR LnkPath, 
                                LPCWSTR LnkDesc,LPCWSTR WorkDir);
            HRESULT _ResolveLink(LPCWSTR LnkFile, LPWSTR FilePath,
                                 LPWSTR LnkDesc,LPWSTR WorkDir);

            __property String* get_FilePath()
                return m_filepath;
            __property void set_FilePath(String *str)
                m_filepath = str;
            __property String* get_WorkDir()
                return m_workdir;
            __property void set_WorkDir(String *str)
                m_workdir = str;
            __property String* get_LnkDesc()
                return m_lnkdesc;
            __property void set_LnkDesc(String *str)
                m_lnkdesc = str;
            __property String* get_LnkPath()
                return m_lnkpath;
            __property void set_LnkPath(String *str)
                m_lnkpath = str;
            bool CreateLink()
                IntPtr strfilepath = Marshal::StringToCoTaskMemUni(FilePath);
                IntPtr strlnkpath = Marshal::StringToCoTaskMemUni(LnkPath);
                IntPtr strlnkdesc = Marshal::StringToCoTaskMemUni(LnkDesc);
                IntPtr strworkdir = Marshal::StringToCoTaskMemUni(WorkDir);
                HRESULT hres = _CreateLink((LPCWSTR)strfilepath.ToPointer(),


                return (hres==S_OK);

            bool ResolveLink()
                wchar_t strfilepath[MAX_PATH];
                wchar_t strworkdir[MAX_PATH];
                wchar_t strlnkdesc[INFOTIPSIZE];

                IntPtr strlnkpath = Marshal::StringToCoTaskMemUni(LnkPath);         
                HRESULT hres = _ResolveLink((LPCWSTR)strlnkpath.ToPointer(),
                    strfilepath, strlnkdesc, strworkdir);

                FilePath = strfilepath;
                LnkDesc = strlnkdesc;
                WorkDir = strworkdir;

                return (hres==S_OK);
// This is the main DLL file.

#include "stdafx.h"
#using <mscorlib.dll>
#include <Shlobj.h>
#include <crtdbg.h>
#include <atldef.h>
#include <atlconv.h>

#include "ShortCut.h"

HRESULT ShortCutLib::ShortCut::_CreateLink(LPCWSTR FilePath, 
    LPCWSTR LnkPath, LPCWSTR LnkDesc,LPCWSTR WorkDir) 
    IShellLink* psl;             
    HRESULT  hres = CoCreateInstance(CLSID_ShellLink, NULL, 
            CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *) &psl); 
    if (SUCCEEDED(hres)) 
        IPersistFile* ppf; 
        hres = psl->QueryInterface(IID_IPersistFile,(LPVOID*)&ppf); 
        if (SUCCEEDED(hres)) 
            hres = ppf->Save(LnkPath, TRUE); 
    return hres; 

HRESULT ShortCutLib::ShortCut::_ResolveLink(LPCWSTR LnkFile, LPWSTR FilePath,
                                            LPWSTR LnkDesc,LPWSTR WorkDir) 
    HRESULT hres; 
    IShellLink* psl; 
    WIN32_FIND_DATA wfd; 
    char strfilepath[MAX_PATH];     
    char strlnkdesc[INFOTIPSIZE];
    char strworkdir[MAX_PATH];

    hres = CoCreateInstance(CLSID_ShellLink, NULL, 
            CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *) &psl); 
    if (SUCCEEDED(hres)) 
        IPersistFile* ppf;         
        hres = psl->QueryInterface(IID_IPersistFile, (LPVOID *)&ppf); 
        if (SUCCEEDED(hres)) 
            hres = ppf->Load(LnkFile, STGM_READ); 
            if (SUCCEEDED(hres)) 
                hres = psl->Resolve(GetDesktopWindow(), 0); 
                if (SUCCEEDED(hres)) 
                    hres = psl->GetPath(strfilepath,MAX_PATH, &wfd, 
                                           SLGP_UNCPRIORITY );
                    if (SUCCEEDED(hres)) 
                        wcscpy(FilePath, A2W(strfilepath)); 
                        hres = psl->GetDescription(strlnkdesc,INFOTIPSIZE);

                    if (SUCCEEDED(hres)) 
                        hres = psl->GetWorkingDirectory(strworkdir,MAX_PATH);

                    if (SUCCEEDED(hres)) 
    return hres; 


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


About the Author

Nish Nishant
United States United States
Nish Nishant is a Principal Software Architect based out of Columbus, Ohio. He has over 17 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish was a Microsoft Visual C++ MVP between 2002 and 2015.

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored C++/CLI in Action for Manning Publications in 2005, and had previously co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on and another 250+ blog articles on his WordPress blog. Nish is vastly experienced in team management, mentoring teams, and directing all stages of software development.

Contact Nish : If you are interested in hiring Nish as a consultant, you can reach him via his google email id voidnish.

Company Website :

You may also be interested in...


Comments and Discussions

Generalrunning under 64 bit OS? Pin
Thomas Ramaglia7-Jun-11 11:04
memberThomas Ramaglia7-Jun-11 11:04 
QuestionWhat about LNK to installed app (not pointed to exe, created by MSI) Pin
Member 323162121-May-09 3:34
memberMember 323162121-May-09 3:34 
GeneralCorrection Pin
Member 74870021-May-08 21:51
memberMember 74870021-May-08 21:51 
GeneralA2W conversion Pin
sheshidar27-Jul-06 19:50
membersheshidar27-Jul-06 19:50 
GeneralLinking under VS2005 Pin
marad0124-May-06 4:43
membermarad0124-May-06 4:43 
GeneralRe: Linking under VS2005 [modified] Pin
thomasa88_26-Jun-06 12:51
memberthomasa88_26-Jun-06 12:51 
QuestionShortcutLib can't handle http links? Pin
dommer16-Feb-05 9:25
memberdommer16-Feb-05 9:25 
QuestionIs there a chance to get the library in native c# code? Pin
rleins3-Jun-04 3:06
memberrleins3-Jun-04 3:06 
AnswerRe: Is there a chance to get the library in native c# code? Pin
Nishant S3-Jun-04 3:52
staffNishant S3-Jun-04 3:52 
GeneralGreat Code, but where is the &quot;iconpath&quot; property Pin
softcell6-Mar-04 2:13
susssoftcell6-Mar-04 2:13 
GeneralCreate MSI Links Pin
Anonymous20-Oct-03 1:50
sussAnonymous20-Oct-03 1:50 
GeneralCPP ShortCut Source Pin
Russell Mangel15-Oct-03 16:42
memberRussell Mangel15-Oct-03 16:42 
GeneralRe: CPP ShortCut Source Pin
Nishant S15-Oct-03 17:42
staffNishant S15-Oct-03 17:42 
GeneralAccess is denied: 'ShortCut' Pin
w3home11-Apr-03 0:03
memberw3home11-Apr-03 0:03 
GeneralRe: Access is denied: 'ShortCut' Pin
Nishant S13-Apr-03 9:03
editorNishant S13-Apr-03 9:03 
GeneralRe: Access is denied: 'ShortCut' Pin
Marco Morreale30-Jun-03 2:19
memberMarco Morreale30-Jun-03 2:19 
GeneralShortcut file not created Pin
schmiddy25-Mar-03 17:20
memberschmiddy25-Mar-03 17:20 
GeneralRe: Shortcut file not created Pin
Nishant S25-Mar-03 18:05
editorNishant S25-Mar-03 18:05 
GeneralRe: Shortcut file not created Pin
Anonymous25-Mar-03 18:12
sussAnonymous25-Mar-03 18:12 
GeneralRe: Shortcut file not created Pin
Nishant S25-Mar-03 18:14
editorNishant S25-Mar-03 18:14 
Anonymous wrote:
Don't I feel stoopid

We all do at times Smile | :)


Author of the romantic comedy

Summer Love and Some more Cricket [New Win]

Review by Shog9
Click here for review[NW]

GeneralFans! Pin
Mustafa Demirhan11-May-02 14:11
memberMustafa Demirhan11-May-02 14:11 
GeneralRe: Fans! Pin
Nish [BusterBoy]11-May-02 14:19
memberNish [BusterBoy]11-May-02 14:19 
GeneralRe: Fans! Pin
Mustafa Demirhan11-May-02 14:24
memberMustafa Demirhan11-May-02 14:24 
GeneralRe: Fans! Pin
Nish [BusterBoy]11-May-02 14:36
memberNish [BusterBoy]11-May-02 14:36 
GeneralUse PtrToStringChars Pin
MCPPKing11-May-02 5:39
memberMCPPKing11-May-02 5:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04-2016 | 2.8.180810.1 | Last Updated 11 May 2002
Article Copyright 2002 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid