//ClasswhichcontainsalltheparametersrelatedtoshortcutclassCShellLinkInfo:publicCObject{public://Constructors/DestructorsCShellLinkInfo();CShellLinkInfo(constCShellLinkInfo&sli);~CShellLinkInfo();//MethodsCShellLinkInfo&operator=(constCShellLinkInfo&sli);//Diagnosticsupport#ifdef_DEBUGvirtualvoidDump(CDumpContext&dc);#endif//VariablesCStringm_sTarget;LPITEMIDLISTm_pidl;CStringm_sArguments;CStringm_sDescription;WORDm_wHotkey;CStringm_sIconLocation;intm_nIconIndex;intm_nShowCmd;CStringm_sWorkingDirectory;};//Classwhichwrapsstandardshortcutsi.e.IShellLinkclassCShellLink{public://Constructors/DestructorsCShellLink();virtual~CShellLink();//MethodsBOOLCreate(constCShellLinkInfo&sli);BOOLLoad(constCString&sFilename);BOOLSave(constCString&sFilename);BOOLResolve(CWnd*pParentWnd,DWORDdwFlags);//AccessorsCStringGetPath()const;LPITEMIDLISTGetPathIDList()const;CStringGetArguments()const;CStringGetDescription()const;WORDGetHotKey()const;CStringGetIconLocation()const;intGetIconLocationIndex()const;intGetShowCommand()const;CStringGetWorkingDirectory()const;//MutatorsvoidSetPath(constCString&sPath);voidSetPathIDList(LPITEMIDLISTpidl);voidSetArguments(constCString&sArguments);voidSetDescription(constCString&sDescription);voidSetHotKey(WORDwHotkey);voidSetIconLocation(constCString&sIconLocation);voidSetIconLocationIndex(intnIconIndex);voidSetShowCommand(intnShowCmd);voidSetWorkingDirectory(constCString&sWorkingDirectory);protected:BOOLInitialise();CShellLinkInfom_sli;IShellLink*m_psl;IPersistFile*m_ppf;BOOLm_bAttemptedInitialise;};//Classwhichwrapsinternetshortcutsi.e.IUniformResourceLocatorclassCUrlShellLink:publicCShellLink{public://Constructors/DestructorsCUrlShellLink();virtual~CUrlShellLink();//MethodsBOOLCreate(constCShellLinkInfo&sli);BOOLLoad(constCString&sFilename);BOOLSave(constCString&sFilename);BOOLInvoke(CWnd*pParentWnd,DWORDdwFlags,constCString&sVerb);//Following4functionsjustASSERTifcalledCStringGetArguments()const;LPITEMIDLISTGetPathIDList()const;voidSetArguments(constCString&sArguments);voidSetPathIDList(LPITEMIDLISTpidl);protected:BOOLInitialise();IUniformResourceLocator*m_pURL;}; 实现文件<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->/**/////////////////Includes////////////////////////////////////////////#include"stdafx.h"#include"ShellLink.h"/**/////////////////Implementation//////////////////////////////////////CShellLinkInfo::CShellLinkInfo(){//Setupsomereasonabledefaultsm_pidl=NULL;m_wHotkey=0;m_nIconIndex=0;m_nShowCmd=SW_SHOW;}CShellLinkInfo::CShellLinkInfo(constCShellLinkInfo&sli){*this=sli;}CShellLinkInfo::~CShellLinkInfo(){//Gettheshell'sallocator.IMalloc*pMalloc;HRESULThRes=SHGetMalloc(&pMalloc);if(!SUCCEEDED(hRes)){TRACE(_T("CShellLinkInfo::~CShellLinkInfo,Failedtogettheshell'sIMallocinterface,HRESULTwas%x/n"),hRes);return;}//Freethepidlif(m_pidl){pMalloc->Free(m_pidl);m_pidl=NULL;}//ReleasethepointertoIMallocpMalloc->Release();}CShellLinkInfo&CShellLinkInfo::operator=(constCShellLinkInfo&sli){m_sTarget=sli.m_sTarget;m_pidl=sli.m_pidl;m_sArguments=sli.m_sArguments;m_sDescription=sli.m_sDescription;m_wHotkey=sli.m_wHotkey;m_sIconLocation=sli.m_sIconLocation;m_nIconIndex=sli.m_nIconIndex;m_nShowCmd=sli.m_nShowCmd;m_sWorkingDirectory=sli.m_sWorkingDirectory;return*this;}#ifdef_DEBUGvoidCShellLinkInfo::Dump(CDumpContext&dc){CObject::Dump(dc);dc<<_T("/nm_sTarget=")<<m_sTarget<<_T("/nm_pidl=")<<m_pidl<<_T("/nm_sArguments=")<<m_sArguments<<_T("/nm_sDescription=")<<m_sDescription<<_T("/nm_wHotkey=")<<m_wHotkey<<_T("/nm_sIconLocation=")<<m_sIconLocation<<_T("/nm_nIconIndex=")<<m_nIconIndex<<_T("/nm_nShowCmd=")<<m_nShowCmd<<_T("/nm_sWorkingDirectory=")<<m_sWorkingDirectory;}#endifCShellLink::CShellLink(){m_psl=NULL;m_ppf=NULL;m_bAttemptedInitialise=FALSE;}CShellLink::~CShellLink(){if(m_ppf){m_ppf->Release();m_ppf=NULL;}if(m_psl){m_psl->Release();m_psl=NULL;}}BOOLCShellLink::Initialise(){BOOLbSuccess=FALSE;if(m_bAttemptedInitialise)bSuccess=(m_psl!=NULL);else{//InstantiatetheCOMclassHRESULThRes=::CoCreateInstance(CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER,IID_IShellLink,(LPVOID*)&m_psl);if(SUCCEEDED(hRes)){//AlsogetapointertoIPersistFilehRes=m_psl->QueryInterface(IID_IPersistFile,(LPVOID*)&m_ppf);bSuccess=SUCCEEDED(hRes);if(!bSuccess)TRACE(_T("CShellLink::Initialise,FailedincalltoQueryInterfaceforIPersistFile,HRESULTwas%x/n"),hRes);}elseTRACE(_T("CShellLink::Initialise,FailedincalltoCoCreateInstanceforIShellLink,HRESULTwas%x/n"),hRes);m_bAttemptedInitialise=TRUE;}returnbSuccess;}BOOLCShellLink::Create(constCShellLinkInfo&sli){if(!Initialise())returnFALSE;m_sli=sli;returnTRUE;}BOOLCShellLink::Save(constCString&sFilename){if(!Initialise())returnFALSE;//Validateourparametersif(m_sli.m_pidl==NULL)ASSERT(!m_sli.m_sTarget.IsEmpty());ASSERT(!sFilename.IsEmpty());ASSERT(m_psl);ASSERT(m_ppf);BOOLbSuccess=FALSE;HRESULThRes;//ConvertthepathtoaUNICODEstringWCHARwszPath[MAX_PATH];#ifndef_UNICODE::MultiByteToWideChar(CP_ACP,0,sFilename,-1,wszPath,MAX_PATH);#else_tcscpy(wszPath,sFilename);#endif//Setthevariouslinkvaluesif(m_sli.m_pidl){hRes=m_psl->SetIDList(m_sli.m_pidl);if(!SUCCEEDED(hRes))TRACE(_T("CShellLink::Save,FailedincalltoIShellLink::SetIDList,HRESULTwas%x/n"),hRes);}else{hRes=m_psl->SetPath(m_sli.m_sTarget);if(!SUCCEEDED(hRes))TRACE(_T("CShellLink::Save,FailedincalltoIShellLink::SetPath,HRESULTwas%x/n"),hRes);}hRes=m_psl->SetWorkingDirectory(m_sli.m_sWorkingDirectory);if(!SUCCEEDED(hRes))TRACE(_T("CShellLink::Save,FailedincalltoIShellLink::SetWorkingDirectory,HRESULTwas%x/n"),hRes);hRes=m_psl->SetIconLocation(m_sli.m_sIconLocation,m_sli.m_nIconIndex);if(!SUCCEEDED(hRes))TRACE(_T("CShellLink::Save,FailedincalltoIShellLink::SetIconLocation,HRESULTwas%x/n"),hRes);hRes=m_psl->SetDescription(m_sli.m_sDescription);if(!SUCCEEDED(hRes))TRACE(_T("CShellLink::Save,FailedincalltoIShellLink::SetDescription,HRESULTwas%x/n"),hRes);hRes=m_psl->SetArguments(m_sli.m_sArguments);if(!SUCCEEDED(hRes))TRACE(_T("CShellLink::Save,FailedincalltoIShellLink::SetArguments,HRESULTwas%x/n"),hRes);hRes=m_psl->SetHotkey(m_sli.m_wHotkey);if(!SUCCEEDED(hRes))TRACE(_T("CShellLink::Save,FailedincalltoIShellLink::SetHotkey,HRESULTwas%x/n"),hRes);hRes=m_psl->SetShowCmd(m_sli.m_nShowCmd);if(!SUCCEEDED(hRes))TRACE(_T("CShellLink::Save,FailedincalltoIShellLink::SetShowCmd,HRESULTwas%x/n"),hRes);//SavethelinktofilehRes=m_ppf->Save(wszPath,TRUE);if(SUCCEEDED(hRes))bSuccess=TRUE;elseTRACE(_T("CShellLink::Save,FailedincalltoIPersistFile::Save,HRESULTwas%x/n"),hRes);returnbSuccess;}BOOLCShellLink::Load(constCString&sFilename){if(!Initialise())returnFALSE;//ValidateourparametersASSERT(!sFilename.IsEmpty());ASSERT(m_psl);ASSERT(m_ppf);BOOLbSuccess=FALSE;//ConvertthepathtoaUNICODEstringWCHARwszPath[MAX_PATH];#ifndef_UNICODE::MultiByteToWideChar(CP_ACP,0,sFilename,-1,wszPath,MAX_PATH);#else_tcscpy(wszPath,sFilename);#endif//LoadthelinkfromfileHRESULThRes=m_ppf->Load(wszPath,STGM_READ);if(SUCCEEDED(hRes)){//GetthevariouslinkvaluesTCHARszBuf[_MAX_PATH];WIN32_FIND_DATAfd;hRes=m_psl->GetPath(szBuf,_MAX_PATH,&fd,SLGP_UNCPRIORITY);if(SUCCEEDED(hRes))m_sli.m_sTarget=szBuf;elseTRACE(_T("CShellLink::Load,FailedincalltoIShellLink::GetPath,HRESULTwas%x/n"),hRes);hRes=m_psl->GetIDList(&m_sli.m_pidl);if(!SUCCEEDED(hRes))TRACE(_T("CShellLink::Load,FailedincalltoIShellLink::GetIDList,HRESULTwas%x/n"),hRes);hRes=m_psl->GetWorkingDirectory(szBuf,_MAX_PATH);if(SUCCEEDED(hRes))m_sli.m_sWorkingDirectory=szBuf;elseTRACE(_T("CShellLink::Load,FailedincalltoIShellLink::GetWorkingDirectory,HRESULTwas%x/n"),hRes);hRes=m_psl->GetIconLocation(szBuf,_MAX_PATH,&m_sli.m_nIconIndex);if(SUCCEEDED(hRes))m_sli.m_sIconLocation=szBuf;elseTRACE(_T("CShellLink::Load,FailedincalltoIShellLink::GetIconLocation,HRESULTwas%x/n"),hRes);hRes=m_psl->GetDescription(szBuf,_MAX_PATH);if(SUCCEEDED(hRes))m_sli.m_sDescription=szBuf;elseTRACE(_T("CShellLink::Load,FailedincalltoIShellLink::GetDescription,HRESULTwas%x/n"),hRes);hRes=m_psl->GetArguments(szBuf,_MAX_PATH);if(SUCCEEDED(hRes))m_sli.m_sArguments=szBuf;elseTRACE(_T("CShellLink::Load,FailedincalltoIShellLink::GetArguments,HRESULTwas%x/n"),hRes);hRes=m_psl->GetHotkey(&m_sli.m_wHotkey);if(!SUCCEEDED(hRes))TRACE(_T("CShellLink::Load,FailedincalltoIShellLink::GetHotkey,HRESULTwas%x/n"),hRes);hRes=m_psl->GetShowCmd(&m_sli.m_nShowCmd);if(!SUCCEEDED(hRes))TRACE(_T("CShellLink::Load,FailedincalltoIShellLink::GetShowCmd,HRESULTwas%x/n"),hRes);bSuccess=TRUE;}elseTRACE(_T("CShellLink::Load,FailedincalltoIPersistFile::Load,HRESULTwas%x/n"),hRes);returnbSuccess;}BOOLCShellLink::Resolve(CWnd*pParentWnd,DWORDdwFlags){if(!Initialise())returnFALSE;//ValidateourparametersASSERT(!m_sli.m_sTarget.IsEmpty());ASSERT(m_psl);ASSERT(m_ppf);BOOLbSuccess=FALSE;//DotheactuallinkresolveHWNDhWnd=NULL;if(pParentWnd)hWnd=pParentWnd->GetSafeHwnd();HRESULThRes=m_psl->Resolve(hWnd,dwFlags);if(SUCCEEDED(hRes))bSuccess=TRUE;elseTRACE(_T("CShellLink::Resolve,FailedincalltoIShellLink::Resolve,HRESULTwas%x/n"),hRes);returnbSuccess;}CStringCShellLink::GetPath()const{returnm_sli.m_sTarget;}CStringCShellLink::GetArguments()const{returnm_sli.m_sArguments;}CStringCShellLink::GetDescription()const{returnm_sli.m_sDescription;}WORDCShellLink::GetHotKey()const{returnm_sli.m_wHotkey;}CStringCShellLink::GetIconLocation()const{returnm_sli.m_sIconLocation;}intCShellLink::GetIconLocationIndex()const{returnm_sli.m_nIconIndex;}LPITEMIDLISTCShellLink::GetPathIDList()const{returnm_sli.m_pidl;}intCShellLink::GetShowCommand()const{returnm_sli.m_nShowCmd;}CStringCShellLink::GetWorkingDirectory()const{returnm_sli.m_sWorkingDirectory;}voidCShellLink::SetPath(constCString&sPath){m_sli.m_sTarget=sPath;}voidCShellLink::SetArguments(constCString&sArguments){m_sli.m_sArguments=sArguments;}voidCShellLink::SetDescription(constCString&sDescription){m_sli.m_sDescription=sDescription;}voidCShellLink::SetHotKey(WORDwHotkey){m_sli.m_wHotkey=wHotkey;}voidCShellLink::SetIconLocation(constCString&sIconLocation){m_sli.m_sIconLocation=sIconLocation;}voidCShellLink::SetIconLocationIndex(intnIconIndex){m_sli.m_nIconIndex=nIconIndex;}voidCShellLink::SetPathIDList(LPITEMIDLISTpidl){m_sli.m_pidl=pidl;}voidCShellLink::SetShowCommand(intnShowCmd){m_sli.m_nShowCmd=nShowCmd;}voidCShellLink::SetWorkingDirectory(constCString&sWorkingDirectory){m_sli.m_sWorkingDirectory=sWorkingDirectory;}CUrlShellLink::CUrlShellLink(){m_pURL=NULL;}BOOLCUrlShellLink::Create(constCShellLinkInfo&sli){//ValidateourparametersASSERT(sli.m_sArguments.IsEmpty());//ArgumentsarenotsupportedforInternetshortcutsASSERT(sli.m_pidl==NULL);//pidlsarenotsupportedforInternetshortcutsif(!Initialise())returnFALSE;m_sli=sli;returnTRUE;}CUrlShellLink::~CUrlShellLink(){if(m_psl){m_psl->Release();m_psl=NULL;}if(m_ppf){m_ppf->Release();m_ppf=NULL;}if(m_pURL){m_pURL->Release();m_pURL=NULL;}}BOOLCUrlShellLink::Initialise(){BOOLbSuccess=FALSE;if(m_bAttemptedInitialise)bSuccess=(m_pURL!=NULL);else{//InstantiatetheCOMclassHRESULThRes=::CoCreateInstance(CLSID_InternetShortcut,NULL,CLSCTX_INPROC_SERVER,IID_IUniformResourceLocator,(LPVOID*)&m_pURL);if(SUCCEEDED(hRes)){//AlsogetapointertoIPersistFilehRes=m_pURL->QueryInterface(IID_IPersistFile,(LPVOID*)&m_ppf);if(SUCCEEDED(hRes)){//AlsogetapointertoIShellLinkhRes=m_pURL->QueryInterface(IID_IShellLink,(LPVOID*)&m_psl);if(SUCCEEDED(hRes))bSuccess=TRUE;elseTRACE(_T("CUrlShellLink::Initialise,FailedincalltoQueryInterfaceforIShellLink,HRESULTwas%x/n"),hRes);}elseTRACE(_T("CUrlShellLink::Initialise,FailedincalltoQueryInterfaceforIPersistFile,HRESULTwas%x/n"),hRes);}elseTRACE(_T("CUrlShellLink::Initialise,FailedincalltoCoCreateInstanceforIInternetShortcut,HRESULTwas%x/n"),hRes);m_bAttemptedInitialise=TRUE;}returnbSuccess;}BOOLCUrlShellLink::Save(constCString&sFilename){USES_CONVERSION;if(!Initialise())returnFALSE;//ValidateourparametersASSERT(!sFilename.IsEmpty());ASSERT(!m_sli.m_sTarget.IsEmpty());//URLLinksdonotsupportarguments,everthingshouldbe//includedinm_sli.m_sTargetandm_sli.m_sArgumentsshould//alwaysbeemptyASSERT(m_sli.m_sArguments.IsEmpty());ASSERT(m_pURL);ASSERT(m_psl);ASSERT(m_ppf);BOOLbSuccess=FALSE;//ConvertthepathtoaUNICODEstringWCHARwszPath[_MAX_PATH];#ifndef_UNICODEMultiByteToWideChar(CP_ACP,0,sFilename,-1,wszPath,_MAX_PATH);#else_tcscpy(wszPath,sFilename);#endif//SetthevariousargumentsHRESULThRes=m_pURL->SetURL(m_sli.m_sTarget,0);if(!SUCCEEDED(hRes))TRACE(_T("CUrlShellLink::Save,FailedincalltoIUniformResourceLocator::SetURL,HRESULTwas%x/n"),hRes);hRes=m_psl->SetIconLocation(m_sli.m_sIconLocation,m_sli.m_nIconIndex);if(!SUCCEEDED(hRes))TRACE(_T("CUrlShellLink::Save,FailedincalltoIShellLink::SetIconLocation,HRESULTwas%x/n"),hRes);hRes=m_psl->SetDescription(m_sli.m_sDescription);if(!SUCCEEDED(hRes))TRACE(_T("CUrlShellLink::Save,FailedincalltoIShellLink::SetDescription,HRESULTwas%x/n"),hRes);hRes=m_psl->SetHotkey(m_sli.m_wHotkey);if(!SUCCEEDED(hRes))TRACE(_T("CUrlShellLink::Save,FailedincalltoIShellLink::SetHotkey,HRESULTwas%x/n"),hRes);//SavethelinktofilehRes=m_ppf->Save(wszPath,TRUE);if(SUCCEEDED(hRes))bSuccess=TRUE;elseTRACE(_T("CUrlShellLink::Save,FailedincalltoIPersistFile::Save,HRESULTwas%x/n"),hRes);returnbSuccess;}BOOLCUrlShellLink::Load(constCString&sFilename){if(!Initialise())returnFALSE;//ValidateourparametersASSERT(!sFilename.IsEmpty());ASSERT(m_pURL);ASSERT(m_psl);ASSERT(m_ppf);BOOLbSuccess=FALSE;//ConvertthepathtoaUNICODEstringWCHARwszPath[MAX_PATH];#ifndef_UNICODE::MultiByteToWideChar(CP_ACP,0,sFilename,-1,wszPath,MAX_PATH);#else_tcscpy(wszPath,sFilename);#endif//LoadthelinkfromfileHRESULThRes=m_ppf->Load(wszPath,STGM_READ);if(SUCCEEDED(hRes)){//GetthevariouslinkvaluesLPTSTRlpTemp=NULL;hRes=m_pURL->GetURL(&lpTemp);if(SUCCEEDED(hRes)){m_sli.m_sTarget=lpTemp;IMalloc*pMalloc;hRes=SHGetMalloc(&pMalloc);if(SUCCEEDED(hRes)){pMalloc->Free(lpTemp);pMalloc->Release();}}elseTRACE(_T("CUrlShellLink::Load,FailedincalltoIUniformResourceLocator::GetURL,HRESULTwas%x/n"),hRes);TCHARszBuf[_MAX_PATH];hRes=m_psl->GetWorkingDirectory(szBuf,_MAX_PATH);if(SUCCEEDED(hRes))m_sli.m_sWorkingDirectory=szBuf;elseTRACE(_T("CUrlShellLink::Load,FailedincalltoIShellLink::GetWorkingDirectory,HRESULTwas%x/n"),hRes);hRes=m_psl->GetIconLocation(szBuf,_MAX_PATH,&m_sli.m_nIconIndex);if(SUCCEEDED(hRes))m_sli.m_sIconLocation=szBuf;elseTRACE(_T("CUrlShellLink::Load,FailedincalltoIShellLink::GetIconLocation,HRESULTwas%x/n"),hRes);//WINBUG:URLshortcutsalwaysseemtoreturnadesciptionthesameasthenameof//fileinwhichtheshortcutisstoredhRes=m_psl->GetDescription(szBuf,_MAX_PATH);if(SUCCEEDED(hRes))m_sli.m_sDescription=szBuf;elseTRACE(_T("CUrlShellLink::Load,FailedincalltoIShellLink::GetDescription,HRESULTwas%x/n"),hRes);hRes=m_psl->GetHotkey(&m_sli.m_wHotkey);if(!SUCCEEDED(hRes))TRACE(_T("CUrlShellLink::Load,FailedincalltoIShellLink::GetHotkey,HRESULTwas%x/n"),hRes);hRes=m_psl->GetShowCmd(&m_sli.m_nShowCmd);if(!SUCCEEDED(hRes))TRACE(_T("CUrlShellLink::Load,FailedincalltoIShellLink::GetShowCmd,HRESULTwas%x/n"),hRes);bSuccess=TRUE;}elseTRACE(_T("CUrlShellLink::Load,FailedincalltoIPersistFile::Load,HRESULTwas%x/n"),hRes);returnbSuccess;}BOOLCUrlShellLink::Invoke(CWnd*pParentWnd,DWORDdwFlags,constCString&sVerb){//ValidateourparametersASSERT(!m_sli.m_sTarget.IsEmpty());ASSERT(m_pURL);ASSERT(m_psl);ASSERT(m_ppf);BOOLbSuccess=FALSE;URLINVOKECOMMANDINFOurlicmi;urlicmi.dwcbSize=sizeof(URLINVOKECOMMANDINFO);urlicmi.dwFlags=dwFlags;urlicmi.hwndParent=NULL;if(pParentWnd)urlicmi.hwndParent=pParentWnd->GetSafeHwnd();urlicmi.pcszVerb=sVerb;//InvoketheverbontheURLHRESULThRes=m_pURL->InvokeCommand(&urlicmi);if(SUCCEEDED(hRes))bSuccess=TRUE;elseTRACE(_T("CUrlShellLink::Invoke,FailedincalltoIUniformResourceLocator::Invoke,HRESULTwas%x/n"),hRes);returnbSuccess;}voidCUrlShellLink::SetArguments(constCString&/**//*sArguments*/){//ArgumentsarenotsupportedforInternetshortcutsASSERT(FALSE);}CStringCUrlShellLink::GetArguments()const{ASSERT(FALSE);//ArgumentsarenotsupportedforInternetshortcutsreturnCString();}LPITEMIDLISTCUrlShellLink::GetPathIDList()const{//pidlsarenotsupportedforInternetshortcutsASSERT(FALSE);returnNULL;}voidCUrlShellLink::SetPathIDList(LPITEMIDLIST/**//*pidl*/){//pidlsarenotsupportedforInternetshortcutsASSERT(FALSE);}