获取桌面路径+桌面文件名+桌面快捷方式路径
转载请注明来源:https://www.cnblogs.com/hookjc/
void CGetDesktopPathDlg::OnOK()
{
// TODO: Add extra validation here
CString strPath="Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders";//注册表子键路径
CRegKey regkey;//定义注册表类对象
LONG lResult;//LONG型变量-反应结果
lResult=regkey.Open(HKEY_CURRENT_USER,LPCTSTR(strPath),KEY_ALL_ACCESS); //打开注册表键
if (lResult!=ERROR_SUCCESS)
{
AfxMessageBox("错误:无法查询有关的注册表信息");
return;
}
char chDesktop[50];
DWORD dwSize=50;
regkey.QueryValue(chDesktop,"Desktop",&dwSize);//获取 //ProductName字段值
m_strPath=chDesktop;
UpdateData(FALSE);
regkey.Close();//关闭注册表
GetFileName(m_strPath);
//CDialog::OnOK();
}
void CGetDesktopPathDlg::GetFileName(CString strPath)
{
CFileFind tempFind;
BOOL bFound;
strPath.Replace("\\","\\\\");
bFound=tempFind.FindFile(strPath+"\\*.*");
CString strTmp;
while(bFound)
{
bFound=tempFind.FindNextFile();
if(tempFind.IsDots()) continue;
if(tempFind.IsDirectory()) continue;
strTmp=tempFind.GetFileName();//tempFind.GetFilePath();
if(strTmp.Find(".lnk")!=-1)
{
char szBuf[MAX_PATH]={0};
unsigned short szPath[MAX_PATH]={0};
strTmp=tempFind.GetFilePath();
USES_CONVERSION;
WCHAR *t = T2OLE(LPCTSTR(strTmp));
ReadShortcut(t,szBuf);
strTmp.Format("%s",szBuf);
}
AfxMessageBox(strTmp);
}
tempFind.Close();
}
bool CGetDesktopPathDlg::ReadShortcut(LPWSTR lpwLnkFile, LPSTR lpDescFile)
{
bool bReturn = true;
IShellLink *pShellLink;
if(bReturn)
{
bReturn = (CoInitialize(NULL) == S_OK);
if(bReturn)
{
bReturn = CoCreateInstance (CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,IID_IShellLink, (void **)&pShellLink) >= 0;
if(bReturn)
{
IPersistFile *ppf;
bReturn = pShellLink->QueryInterface(IID_IPersistFile, (void **)&ppf) >= 0;
if(bReturn)
{
bReturn = ppf->Load(lpwLnkFile, TRUE) >= 0;
if(bReturn)
{
pShellLink->GetPath(lpDescFile, MAX_PATH, NULL, 0);
}
ppf->Release ();
}
pShellLink->Release ();
}
CoUninitialize();
}
}
return bReturn;
}