Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C++ / CLI » COM Interop » Downloads
 
Add your own
alternative version

Creating and Resolving shell links

, 10 May 2002
An MC++ class that uses IJW to invoke the IShellLink interface
// ShortCut.h

#pragma once

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

namespace ShortCutLib
{
	public __gc class ShortCut
	{
		private:
			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);

		public:	 
			__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(),(LPCWSTR)strlnkpath.ToPointer(),
					(LPCWSTR)strlnkdesc.ToPointer(),(LPCWSTR)strworkdir.ToPointer());

				Marshal::FreeCoTaskMem(strfilepath);
				Marshal::FreeCoTaskMem(strlnkpath);
				Marshal::FreeCoTaskMem(strlnkdesc);
				Marshal::FreeCoTaskMem(strworkdir);

				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;
					
				Marshal::FreeCoTaskMem(strlnkpath);		 

				return (hres==S_OK);
			}
	};
}

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

Share

About the Author

Nish Sivakumar

United States United States
Nish is a real nice guy who has been writing code since 1990 when he first got his hands on an 8088 with 640 KB RAM. Originally from sunny Trivandrum in India, he has been living in various places over the past few years and often thinks it’s time he settled down somewhere.
 
Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - www.voidnish.com where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff - blog.voidnish.com.
 
Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket as well as a programming book – Extending MFC applications with the .NET Framework.
 
Nish's latest book C++/CLI in Action published by Manning Publications is now available for purchase. You can read more about the book on his blog.
 
Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150301.1 | Last Updated 11 May 2002
Article Copyright 2002 by Nish Sivakumar
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid