if(m_bLoadSuc && m_psl != NULL) { hr = m_psl->GetPath(lpszPath, nBufSize, &winfdata, SLGP_UNCPRIORITY); if(SUCCEEDED(hr)) return true; } return false; }CString CShellLinkFile::GetPath() { char szBuf[MAX_PATH + _MAX_FNAME+1]; if(GetPath(szBuf, MAX_PATH+_MAX_FNAME)) return szBuf; else return ""; }// // Reload new .lnk file! // BOOL CShellLinkFile::SetLnkFile(LPCTSTR strLnkFile) { if(lstrlen(strLnkFile) < MAX_PATH+_MAX_FNAME) lstrcpy(m_szBuffer, strLnkFile); else return FALSE; MultiByteToWideChar(CP_ACP, 0, m_szBuffer, -1, m_wszBuffer, MAX_PATH+_MAX_FNAME); if(SUCCEEDED(m_ppf->Load(m_wszBuffer, STGM_READ))) { m_bLoadSuc = true; return TRUE; } return FALSE; }// // Get Icon from lnk ExeModule1 // HICON CShellLinkFile::GetIcon(int iIcon) { if(m_bLoadSuc && m_psl != NULL) { HICON icon; /* Del in 19991214 if(iIcon == 1) ExtractIconEx(GetPath(), 0, &icon, NULL, 1); else ExtractIconEx(GetPath(), 0, NULL, &icon, 1); */ // Under code surport get the document icon! UINT uFlag = 0; if(iIcon == 1) uFlag = SHGFI_ICON; else uFlag = SHGFI_ICON|SHGFI_SMALLICON; SHFILEINFO sfi; int iRet = SHGetFileInfo(GetPath(), 0, &sfi, sizeof(SHFILEINFO), uFlag ); if(iRet != 0) icon = sfi.hIcon; return icon; } return NULL; }CString CShellLinkFile::GetDescription() { char szDescription[256+1]; if(m_bLoadSuc && m_psl != NULL) { m_psl->GetDescription(szDescription, 256); return szDescription; } return ""; }// CreateLink - uses the shell's IShellLink and IPersistFile interfaces // to create and store a shortcut to the specified object. // Returns the result of calling the member functions of the interfaces. // lpszPathObj - address of a buffer containing the path of the object. // lpszPathLink - address of a buffer containing the path where the // shell link is to be stored. // lpszDesc - address of a buffer containing the description of the // shell link. // Add by YBS 1999/6/24 HRESULT CShellLinkFile::CreateLink(LPCTSTR lpszPathObj, LPCTSTR lpszPathLink, LPCTSTR lpszDesc) { HRESULT hres; IShellLink* psl;
// Get a pointer to the IShellLink interface. hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **)(&psl)); if (SUCCEEDED(hres)) { IPersistFile* ppf;
// Set the path to the shortcut target and add the // description. psl->SetPath(lpszPathObj); psl->SetDescription(lpszDesc);
// Query IShellLink for the IPersistFile interface for saving the // shortcut in persistent storage. hres = psl->QueryInterface(IID_IPersistFile, (void **)&ppf);
if (SUCCEEDED(hres)) { WORD wsz[MAX_PATH]; // Ensure that the string is ANSI. MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1, wsz, MAX_PATH); // Save the link by calling IPersistFile::Save. hres = ppf->Save(wsz, TRUE); ppf->Release(); } psl->Release(); } return hres; }
{
if(strlen(m_sShortcut.szPath) <= 0 ) return FALSE;
HRESULT hRes = 0;
IShellLink * pSl;
BOOL bRet = FALSE; SECURITY_ATTRIBUTES sa;
memset(&sa,0,sizeof(sa));
sa.nLength = sizeof(sa); char szPath[MAX_PATH];
PathName(f,szPath);
if(f == Program){
CreateDirectory(szPath,&sa);
}
FullFileName(f,szPath,pszName);
strcpy(m_sShortcut.szPath,szPath);
hRes = ::CoCreateInstance(CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER,
IID_IShellLink,(void**)&pSl);
if(SUCCEEDED(hRes))
{
IPersistFile * ppf;
hRes = pSl->QueryInterface(IID_IPersistFile,(void**)&ppf);
if(SUCCEEDED(hRes))
{
WORD tsz[LONG_STRING_LENGTH];
::MultiByteToWideChar(CP_ACP,0,(LPCSTR)szPath,-1,tsz,LONG_STRING_LENGTH);
pSl->SetPath((LPCSTR)m_sShortcut.szTarget);
pSl->SetWorkingDirectory((LPCSTR)m_sShortcut.szStartDir);
pSl->SetIconLocation((LPCSTR)m_sShortcut.szIconLocation,m_sShortcut.nIconIndex);
pSl->SetDescription((LPCSTR)m_sShortcut.szDescription);
pSl->SetArguments((LPCSTR)m_sShortcut.szArgs);
pSl->SetHotkey(m_sShortcut.wHotkey);
pSl->SetShowCmd(m_sShortcut.nShowCmd);
hRes = ppf->Save(tsz,TRUE);
if(SUCCEEDED(hRes)) {
bRet = TRUE;
}
ppf->Release();
}
pSl->Release();
}
return bRet;
}
.cpp
// ShellLinkFile.cpp: implementation of the CShellLinkFile class.
//
//////////////////////////////////////////////////////////////////////#include "stdafx.h"
#include "ShellLinkFile.h"#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////CShellLinkFile::CShellLinkFile(char *lpszLinkFile)
{
if(lstrlen(lpszLinkFile) < MAX_PATH+_MAX_FNAME)
lstrcpy(m_szBuffer, lpszLinkFile);
m_bLoadSuc = false;
m_psl = NULL;
m_ppf = NULL; CoInitialize(NULL);
HRESULT hr;
hr = CoCreateInstance(CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER, IID_IShellLink, (void **)&m_psl);
if(SUCCEEDED(hr))
{
hr = m_psl->QueryInterface(IID_IPersistFile, (void **)&m_ppf);
if(SUCCEEDED(hr))
{
MultiByteToWideChar(CP_ACP, 0, m_szBuffer, -1, m_wszBuffer, MAX_PATH+_MAX_FNAME);
hr = m_ppf->Load(m_wszBuffer, STGM_READ);
if(SUCCEEDED(hr))
m_bLoadSuc = true;
}
}
}CShellLinkFile::~CShellLinkFile()
{
m_szBuffer[0] = 0;
m_bLoadSuc = false;
m_ppf->Release();
m_psl->Release();
CoUninitialize();
}
BOOL CShellLinkFile::GetPath(char *lpszPath, int nBufSize)
{
WIN32_FIND_DATA winfdata;
HRESULT hr;
if(m_bLoadSuc && m_psl != NULL)
{
hr = m_psl->GetPath(lpszPath, nBufSize, &winfdata, SLGP_UNCPRIORITY);
if(SUCCEEDED(hr))
return true;
}
return false;
}CString CShellLinkFile::GetPath()
{
char szBuf[MAX_PATH + _MAX_FNAME+1];
if(GetPath(szBuf, MAX_PATH+_MAX_FNAME))
return szBuf;
else
return "";
}//
// Reload new .lnk file!
//
BOOL CShellLinkFile::SetLnkFile(LPCTSTR strLnkFile)
{
if(lstrlen(strLnkFile) < MAX_PATH+_MAX_FNAME)
lstrcpy(m_szBuffer, strLnkFile);
else
return FALSE;
MultiByteToWideChar(CP_ACP, 0, m_szBuffer, -1, m_wszBuffer, MAX_PATH+_MAX_FNAME);
if(SUCCEEDED(m_ppf->Load(m_wszBuffer, STGM_READ)))
{
m_bLoadSuc = true;
return TRUE;
}
return FALSE;
}//
// Get Icon from lnk ExeModule1
//
HICON CShellLinkFile::GetIcon(int iIcon)
{
if(m_bLoadSuc && m_psl != NULL)
{
HICON icon;
/* Del in 19991214
if(iIcon == 1)
ExtractIconEx(GetPath(), 0, &icon, NULL, 1);
else
ExtractIconEx(GetPath(), 0, NULL, &icon, 1);
*/
// Under code surport get the document icon!
UINT uFlag = 0;
if(iIcon == 1)
uFlag = SHGFI_ICON;
else
uFlag = SHGFI_ICON|SHGFI_SMALLICON;
SHFILEINFO sfi;
int iRet = SHGetFileInfo(GetPath(),
0,
&sfi,
sizeof(SHFILEINFO),
uFlag );
if(iRet != 0)
icon = sfi.hIcon; return icon;
}
return NULL;
}CString CShellLinkFile::GetDescription()
{
char szDescription[256+1];
if(m_bLoadSuc && m_psl != NULL)
{
m_psl->GetDescription(szDescription, 256);
return szDescription;
}
return "";
}// CreateLink - uses the shell's IShellLink and IPersistFile interfaces
// to create and store a shortcut to the specified object.
// Returns the result of calling the member functions of the interfaces.
// lpszPathObj - address of a buffer containing the path of the object.
// lpszPathLink - address of a buffer containing the path where the
// shell link is to be stored.
// lpszDesc - address of a buffer containing the description of the
// shell link.
// Add by YBS 1999/6/24
HRESULT CShellLinkFile::CreateLink(LPCTSTR lpszPathObj, LPCTSTR lpszPathLink, LPCTSTR lpszDesc)
{
HRESULT hres;
IShellLink* psl;
// Get a pointer to the IShellLink interface.
hres = CoCreateInstance(CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER, IID_IShellLink, (void **)(&psl));
if (SUCCEEDED(hres))
{
IPersistFile* ppf;
// Set the path to the shortcut target and add the
// description.
psl->SetPath(lpszPathObj);
psl->SetDescription(lpszDesc);
// Query IShellLink for the IPersistFile interface for saving the
// shortcut in persistent storage.
hres = psl->QueryInterface(IID_IPersistFile, (void **)&ppf);
if (SUCCEEDED(hres))
{
WORD wsz[MAX_PATH];
// Ensure that the string is ANSI.
MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1,
wsz, MAX_PATH);
// Save the link by calling IPersistFile::Save.
hres = ppf->Save(wsz, TRUE);
ppf->Release();
}
psl->Release();
}
return hres;
}
//
//////////////////////////////////////////////////////////////////////#if !defined(AFX_SHELLLINKFILE_H__0671C6A3_68FD_11D2_8FD0_006008D4EFAA__INCLUDED_)
#define AFX_SHELLLINKFILE_H__0671C6A3_68FD_11D2_8FD0_006008D4EFAA__INCLUDED_#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000#include <shlobj.h>class CShellLinkFile
{
public:
HRESULT CreateLink(LPCTSTR lpszPathObj, LPCTSTR lpszPathLink, LPCTSTR lpszDesc);
CString GetDescription();
HICON GetIcon(int iIcon = 0);
BOOL SetLnkFile(LPCTSTR strLnkFile);
CShellLinkFile(char *lpszLinkFile);
virtual ~CShellLinkFile(); BOOL GetPath(char *lpszPath, int nBufSize);
CString GetPath();
protected:
IPersistFile *m_ppf;
IShellLink *m_psl; char m_szBuffer[MAX_PATH+_MAX_FNAME+1];
WORD m_wszBuffer[MAX_PATH+_MAX_FNAME+1]; BOOL m_bLoadSuc;
};#endif // !defined(AFX_SHELLLINKFILE_H__0671C6A3_68FD_11D2_8FD0_006008D4EFAA__INCLUDED_)用法:
//strApp为目标程序,strDesktop为快捷方式位置,如果要放入"开始"
//菜单,应为c:\\windows\\start menu\\programs\\*.lnk
lnkfile.CreateLink(strApp, strDesktop, "XXX");