Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C++/CLI

Creating and Resolving shell links

Rate me:
Please Sign up or sign in to vote.
4.92/5 (19 votes)
10 May 2002 263K   1.4K   27  
An MC++ class that uses IJW to invoke the IShellLink interface
// 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) 
{ 
	USES_CONVERSION;
	CoInitialize(NULL);
    IShellLink* psl; 		    
    HRESULT  hres = CoCreateInstance(CLSID_ShellLink, NULL, 
        CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *) &psl); 
    if (SUCCEEDED(hres)) 
	{ 
        IPersistFile* ppf; 
         
        psl->SetPath(W2A(FilePath)); 
		psl->SetWorkingDirectory(W2A(WorkDir));
        psl->SetDescription(W2A(LnkDesc)); 
	        
        hres = psl->QueryInterface(IID_IPersistFile,(LPVOID*)&ppf); 
 
        if (SUCCEEDED(hres)) 
		{    
            hres = ppf->Save(LnkPath, TRUE); 
            ppf->Release(); 
        } 
        psl->Release(); 
    }	
	CoUninitialize();
    return hres; 
} 

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

	USES_CONVERSION;
 
    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)) 
					{
						wcscpy(LnkDesc,A2W(strlnkdesc));
						hres = psl->GetWorkingDirectory(strworkdir,MAX_PATH);							
					}

					if (SUCCEEDED(hres)) 
					{
						wcscpy(WorkDir,A2W(strworkdir));
					}
                } 
            }         
        ppf->Release(); 
        }     
    psl->Release(); 
    } 
	CoUninitialize();
    return hres; 
} 

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
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.

Nish authored C++/CLI in Action for Manning Publications in 2005, and co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.

Nish's Technology Blog : voidnish.wordpress.com

Comments and Discussions