解析和创建快捷方式



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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值