获取快捷方式的目标和参数
【打印文章】
我看了Michael Taupitz关于建立快捷方式的方法,我认为有很多时候当我
们在命令行模式中偶然发现一个快捷方式的时候,我们不知道这个.lnk文件指
向什么地方。
我写了一个函数,它以CString的形式返回了.lnk文件的目标和参数。
CString GetShortcutTarget(const CString LinkFileName)
{
HRESULT hres;
CString Link, Temp = LinkFileName;
Temp.MakeLower();
if (Temp.Find(".lnk")==-1) //Check if the name ends with .lnk
Link = LinkFileName + ".lnk"; //if not, append it
else
Link = LinkFileName;
CString Info;
Info.Empty();
IShellLink* psl;
//Create the ShellLink object
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
IID_IShellLink, (LPVOID*) &psl);
if (SUCCEEDED(hres))
{
IPersistFile* ppf;
//Bind the ShellLink object to the Persistent File
hres = psl->QueryInterface( IID_IPersistFile, (LPVOID *) &ppf);
if (SUCCEEDED(hres))
{
WORD wsz[MAX_PATH];
//Get a UNICODE wide string wsz from the Link path
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, Link, -1, wsz, MAX_PATH);
//Read the link into the persistent file
hres = ppf->Load(wsz, 0);
if (SUCCEEDED(hres))
{
//Read the target information from the link object
//UNC paths are supported (SLGP_UNCPRIORITY)
psl->GetPath(Temp.GetBuffer(1024), 1024, NULL, SLGP_UNCPRIORITY);
Temp.ReleaseBuffer();
Info = Temp;
//Read the arguments from the link object
psl->GetArguments(Temp.GetBuffer(1024), 1024);
Temp.ReleaseBuffer();
Info += " " + Temp;
}
}
}
psl->Release();
//Return the Target and the Argument as a CString
return Info;
}
们在命令行模式中偶然发现一个快捷方式的时候,我们不知道这个.lnk文件指
向什么地方。
我写了一个函数,它以CString的形式返回了.lnk文件的目标和参数。
CString GetShortcutTarget(const CString LinkFileName)
{
HRESULT hres;
CString Link, Temp = LinkFileName;
Temp.MakeLower();
if (Temp.Find(".lnk")==-1) //Check if the name ends with .lnk
Link = LinkFileName + ".lnk"; //if not, append it
else
Link = LinkFileName;
CString Info;
Info.Empty();
IShellLink* psl;
//Create the ShellLink object
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
IID_IShellLink, (LPVOID*) &psl);
if (SUCCEEDED(hres))
{
IPersistFile* ppf;
//Bind the ShellLink object to the Persistent File
hres = psl->QueryInterface( IID_IPersistFile, (LPVOID *) &ppf);
if (SUCCEEDED(hres))
{
WORD wsz[MAX_PATH];
//Get a UNICODE wide string wsz from the Link path
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, Link, -1, wsz, MAX_PATH);
//Read the link into the persistent file
hres = ppf->Load(wsz, 0);
if (SUCCEEDED(hres))
{
//Read the target information from the link object
//UNC paths are supported (SLGP_UNCPRIORITY)
psl->GetPath(Temp.GetBuffer(1024), 1024, NULL, SLGP_UNCPRIORITY);
Temp.ReleaseBuffer();
Info = Temp;
//Read the arguments from the link object
psl->GetArguments(Temp.GetBuffer(1024), 1024);
Temp.ReleaseBuffer();
Info += " " + Temp;
}
}
}
psl->Release();
//Return the Target and the Argument as a CString
return Info;
}
本栏文章均来自于互联网,版权归原作者和各发布网站所有,本站收集这些文章仅供学习参考之用。任何人都不能将这些文章用于商业或者其他目的。( Pfan.cn )
【编程爱好者论坛】