示例#1
0
/**
 * Install a shortcut for the virtual folder in the Windows Start Menu.
 * This method knows when we're installing under WOW64 so creating a shortcut will allow
 * the user to access the virtual folder, even when using a 32bit DLL under 64bit Windows.
 */
HRESULT UpdateStartMenuLink(LPCWSTR pstrDisplayName, LPCWSTR pstrDescription, KNOWNFOLDERID FolderId, BOOL bRegister)
{
   HRESULT Hr = S_OK;
   // Get folder for installation...
   CPidl pidlFolderPath;
   CCoTaskString strFolderPath;
   HR( pidlFolderPath.CreateFromKnownFolder(FolderId) );
   HR( pidlFolderPath.GetName(SIGDN_DESKTOPABSOLUTEPARSING, &strFolderPath) );
   // Create shortcut link...
   CComPtr<IShellLink> spLink;
   HR( spLink.CoCreateInstance(CLSID_ShellLink) );
   BOOL bIsWOW64 = FALSE; ::IsWow64Process(::GetCurrentProcess(), &bIsWOW64);
   CComBSTR bstrPath = bIsWOW64 ? L"%windir%\\syswow64\\explorer.exe" : L"%windir%\\explorer.exe";
   CComBSTR bstrIcon = bIsWOW64 ? L"%windir%\\syswow64\\shell32.dll" : L"%SystemRoot%\\system32\\shell32.dll";
   int iIconIndex = _ShellModule.GetConfigBool(VFS_HAVE_VIRTUAL_FILES) ? SIID_DRIVEFIXED : SIID_FOLDER;
   WCHAR wszArgs[MAX_PATH + 80] = { 0 };
   ::wnsprintf(wszArgs, lengthof(wszArgs) - 1, L"/separate,%s%s%s::%s", bIsWOW64 ? L"/select," : L"", static_cast<LPCWSTR>(strFolderPath), strFolderPath.IsEmpty() ? L"" : L"\\", static_cast<LPCWSTR>(CComBSTR(CLSID_ShellFolder)));
   spLink->SetPath(bstrPath);
   spLink->SetArguments(wszArgs);
   spLink->SetIconLocation(bstrIcon, iIconIndex);
   spLink->SetDescription(pstrDescription);
   // Write the link file to the Windows Start Menu...
   CComQIPtr<IPersistFile> spPersist = spLink;
   if( spPersist == NULL ) return E_UNEXPECTED;
   CCoTaskString strLinkPath;
   HR( ::SHGetKnownFolderPath(FOLDERID_StartMenu, 0, NULL, &strLinkPath) );
   WCHAR wszLinkFilename[MAX_PATH] = { 0 };
   ::wnsprintf(wszLinkFilename, lengthof(wszLinkFilename) - 1, L"%s\\%s.lnk", static_cast<LPCWSTR>(strLinkPath), pstrDisplayName);
   if( bRegister ) Hr = spPersist->Save(wszLinkFilename, TRUE);
   else ::DeleteFileW(wszLinkFilename);
   return Hr;
}