您的位置:首页 > 其它

解析和创建快捷方式

2017-12-15 21:28 295 查看
#include <objbase.h>
#include <Shlobj.h>

BOOL ResolveShortCut(LPCSTR pszShortcutFile,
CHAR pszPath[MAX_PATH+1], CHAR szDescription[MAX_PATH+1])
{
BOOL bRet = FALSE;

*pszPath = 0; // assume failure
*szDescription = 0;

IShellLink* psl = NULL;
IPersistFile* ppf = NULL;

//init COM
//CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

do
{
HRESULT hres;

// Get a pointer to the IShellLink interface.
if(FAILED(hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
IID_IShellLink, (void**)&psl)))
{
break;
}

// Get a pointer to the IPersistFile interface.
if(FAILED(hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf)))
{
break;
}

WCHAR wsz[MAX_PATH];

// Ensure string is Unicode.
MultiByteToWideChar(CP_ACP, 0, pszShortcutFile, -1, wsz, MAX_PATH);

// Load the shell link.
if(FAILED(hres = ppf->Load(wsz, STGM_READ)))
{
break;
}

// Resolve the link.
if(FAILED(hres = psl->Resolve(NULL, SLR_ANY_MATCH | SLR_NO_UI)))
{
break;
}

char szGotPath[MAX_PATH + 1];
strcpy_s(szGotPath, pszShortcutFile);
// Get the path to the link target.
WIN32_FIND_DATA wfd = {0};
if(SUCCEEDED(hres = psl->GetPath(szGotPath, MAX_PATH, (WIN32_FIND_DATA *)&wfd, 0)))
{
strcpy_s(pszPath, szGotPath);
}

// Get the description of the target.
if(SUCCEEDED(hres = psl->GetDescription(szDescription, MAX_PATH)))
{
}

bRet = TRUE;
}while(0);

// Release pointer to IPersistFile interface.
if(ppf)
{
ppf->Release();
}
// Release pointer to IShellLink interface.
if(psl)
{
psl->Release();
}

//release com
//CoUninitialize();

return bRet;
}

BOOL CreateShortCut(LPCSTR pszShortcutFile, LPSTR pszLink, LPSTR pszDesc)
{
BOOL bRet = FALSE;

IShellLink * psl = NULL;
IPersistFile * ppf = NULL;

//init COM
//CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

do
{
HRESULT hres;

// Get a pointer to the IShellLink interface.
if(FAILED(hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
IID_IShellLink, (void**)&psl)))
{
break;
}

// Query IShellLink for the IPersistFile interface for
// saving the shell link in persistent storage.
if(FAILED(hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf)))
{
break;
}

WCHAR wsz[MAX_PATH];

// Set the path to the shell link target.
if(FAILED(hres = psl->SetPath(pszShortcutFile)))
{
break;
}

//set icon
psl->SetIconLocation(pszShortcutFile, 1);

// Set the description of the shell link.
if(FAILED(hres = psl->SetDescription(pszDesc)))
{
break;
}

// Ensure string is ANSI.
MultiByteToWideChar(CP_ACP, 0, pszLink, -1, wsz, MAX_PATH);

// Save the link via the IPersistFile::Save method.
if(FAILED(hres = ppf->Save(wsz, TRUE)))
{
break;
}

//Resolves a shell link
if(FAILED(hres = psl->Resolve(NULL, SLR_NO_UI | SLR_UPDATE)))
{
break;
}

bRet = TRUE;
}while(0);

// Release pointer to IPersistFile.
if(ppf)
{
ppf->Release();
}
// Release pointer to IShellLink.
if(psl)
{
psl->Release();
}
//release com
//CoUninitialize();

return bRet;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: