首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

C++怎么取到桌面其中一个快捷方式图标里的起始位置

2013-08-29 
C++如何取到桌面其中一个快捷方式图标里的起始位置比如说安装了个360杀毒软件,并在桌面上创建了360杀毒软

C++如何取到桌面其中一个快捷方式图标里的起始位置
比如说安装了个360杀毒软件,并在桌面上创建了360杀毒软件的快捷方式。它的属性里有个“起始位置”,如"C:\Program Files\360\360sd"。   我要写一个C++程序,只知道360杀毒软件快捷图标的名称,要取到“起始位置”的字符串"C:\Program Files\360\360sd"。  如何写?桌面路径取当前用户的桌面路径。也就是说:如果一台机子上开了好几个用户账号,那就取登陆的那个的桌面地址。假设每一个用户都安装了360杀毒,并生成桌面图标.
[解决办法]

void QdSetup::GetLinkPath(wchar_t *lpszLink, wchar_t *szPath, wchar_t *szParam)   
{   
HRESULT   hres;     
IShellLink*   psl;     
wchar_t   szGotPath[MAX_PATH];   
wchar_t   szArguement[MAX_PATH];   
WIN32_FIND_DATA   wfd;   

*szPath   =   0;   //   assume   failure   
CoInitialize(0);   
//   Get   a   pointer   to   the   IShellLink   interface.   
hres   =   CoCreateInstance(CLSID_ShellLink,   NULL,   
CLSCTX_INPROC_SERVER,   IID_IShellLink,   (LPVOID   *)   &psl);   
if   (SUCCEEDED(hres))   {   
IPersistFile*   ppf;   

//   Get   a   pointer   to   the   IPersistFile   interface.   
hres   =   psl->QueryInterface(IID_IPersistFile,   
(void**)&ppf);   
if   (SUCCEEDED(hres))   {   
//WCHAR   wsz[MAX_PATH];   

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

//   Load   the   shortcut.   


hres   =   ppf->Load(lpszLink,   STGM_READ);   
if   (SUCCEEDED(hres))   {   

//   Resolve   the   link.   
hres   =   psl->Resolve(0,   0);   
if   (SUCCEEDED(hres))   {   

//   Get   the   path   to   the   link   target.   
hres   =   psl->GetPath(szGotPath,   
MAX_PATH,   (WIN32_FIND_DATA   *)&wfd,   
SLGP_SHORTPATH   );   
if   (SUCCEEDED(hres))   
lstrcpy(szPath,   szGotPath);   
hres   =   psl->GetArguments(szArguement, 256);
if(SUCCEEDED(hres))
lstrcpy(szParam, szArguement);
}   
}   
//   Release   the   pointer   to   the   IPersistFile   interface.   
ppf->Release();   
}   
//   Release   the   pointer   to   the   IShellLink   interface.   
psl->Release();   
}   
if(hres)   
lstrcpy(szPath,   lpszLink);   
CoUninitialize();   
}   


[解决办法]
这里有个封装好的类
http://www.cnblogs.com/phinecos/archive/2008/07/06/1236968.html

我这编译时,好像没有IUniformResourceLocator这个东西,把CUrlShellLink注释了就是了。

使用
CoInitialize(0);
CShellLink obj;
obj.Load(_T("C:/Documents and Settings/bizuser/桌面/金山词霸2007.lnk"));
CString str = obj.GetPath();
AfxMessageBox(str);
CoUninitialize(); 

热点排行