创建开始菜单启动项

 PFNSHGETFOLDERPATHA GetFuncPtr_SHGetFolderPathA()
{
 static HMODULE hMod = NULL;
 PFNSHGETFOLDERPATHA pSHGetFolderPath = NULL;

 // Load SHFolder.dll only once
 if (!hMod)
  hMod = LoadLibrary("SHFolder.dll");

 // Obtain a pointer to the SHGetFolderPathA function
 if (hMod)
  pSHGetFolderPath = (PFNSHGETFOLDERPATHA)GetProcAddress(hMod, "SHGetFolderPathA");
    return pSHGetFolderPath;
}


/********************************************************************/
/*                 */
/* Function name : CreateStartMenuShortcut       */  
/* Description   : Create a shortcut using the IShellLink interface.*/
/*                 */
/********************************************************************/
HRESULT CreateStartMenuShortcut(LPSTR lpszShortcutFile, LPSTR lpszDescription, LPTSTR lpszRelativeFolder)
{
 HRESULT hr;
 PFNSHGETFOLDERPATHA pSHGetFolderPath = NULL;
 TCHAR lpszLink[MAX_PATH];
 BOOL bFound = FALSE;
    pSHGetFolderPath = GetFuncPtr_SHGetFolderPathA();

 // Find the current user's Start Menu Programs folder
 if (pSHGetFolderPath)
  bFound = SUCCEEDED(pSHGetFolderPath(NULL, CSIDL_PROGRAMS, NULL, 0, lpszLink));

 if (bFound)
 {
  // Proceed to create the shortcut
  IShellLink *pIShellLink = NULL;
  IPersistFile *ppf = NULL;
  WCHAR pLinkUnicode[MAX_PATH];

  CoInitialize(NULL);

  // Get a pointer to the IShellLink interface.
  hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
                            IID_IShellLink, (void **)&pIShellLink);

  if (SUCCEEDED(hr))
  {
   // Get a pointer to IPersistFile interface for saving shortcut
   hr = pIShellLink->QueryInterface(IID_IPersistFile, (void **)&ppf);

   if (SUCCEEDED(hr))
   {  
             hr = pIShellLink->SetPath(lpszShortcutFile);
          hr = pIShellLink->SetDescription(lpszDescription);

    if (SUCCEEDED(hr))
    {
     // Add the target folder to the Start Menu Programs path
     lstrcat(lpszLink, "//");
     lstrcat(lpszLink, lpszRelativeFolder);
     lstrcat(lpszLink, "//");
 
     // Create the directory if it does not exist
     CreateDirectory(lpszLink,NULL);

     // Add the file name for the shortcut
     lstrcat(lpszLink, lpszDescription);
     lstrcat(lpszLink, ".lnk");

     // Convert string to Unicode, and call IPersistFile::Save()
     MultiByteToWideChar(CP_ACP, 0, lpszLink, -1, pLinkUnicode, MAX_PATH);
     hr = ppf->Save(pLinkUnicode, TRUE);
    }
    ppf->Release();
   }
   pIShellLink->Release();
  }
  CoUninitialize();
 }
 return hr;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值