首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

解析和创建快捷方式

解析和创建快捷方式

#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;

}
返回列表