Vista Does Not Virtualize Creation Of Shell Links

Win32 No Comments »

Windows Vista developers beware: Vista does not perform file virtualization on the creation of shell links. Consider the following code:

  1. // Creates a shell link (a.k.a. shortcut) located at swzLinkFile that
  2. // points to szTargetFile with a description of szDescription.
  3. BOOL CreateLink(LPCTSTR szTargetFile, LPCTSTR szDescription,
  4.                 LPCOLESTR swzLinkFile)
  5. {
  6.     BOOL bRet = FALSE;
  7.  
  8.     IShellLink* psl;
  9.     HRESULT hr = ::CoCreateInstance(CLSID_ShellLink, NULL,
  10.                                     CLSCTX_INPROC_SERVER,
  11.                                     IID_IShellLink,
  12.                                     (void**) &psl);
  13.     if (SUCCEEDED(hr))
  14.     {
  15.         IPersistFile* ppf;
  16.         hr = psl->QueryInterface(IID_IPersistFile, (void**) &ppf);
  17.         if (SUCCEEDED(hr))
  18.         {          
  19.             hr = psl->SetPath(szTargetFile);
  20.             if (SUCCEEDED(hr))
  21.             {
  22.                 hr = psl->SetDescription(szDescription);
  23.                 if (SUCCEEDED(hr))
  24.                 {
  25.                     hr = ppf->Save(swzLinkFile, TRUE);
  26.                     if (SUCCEEDED(hr))
  27.                     {
  28.                         bSuccess = TRUE;
  29.                     }
  30.                 }
  31.             }
  32.             ppf->Release();
  33.         }
  34.         psl->Release();
  35.     }
  36.     return bSuccess;
  37. }
  38.  
  39. // NOTE: Hardcoding C:\\WINDOWS and C:\\Program Files is a bad practice.
  40. // Use something like ::SHGetFolderPath().
  41. BOOL bSuccess = CreateLink
  42.     (
  43.     _T("C:\\WINDOWS\\SYSTEM32\\SOL.EXE"),
  44.     _T("Shortcut to SOL.EXE"),
  45.     L"C:\\Program Files\\sol.lnk")
  46.     );

One might expect that the creation of the file C:\Program Files\sol.lnk would be silently redirected by Vista using file virtualization and CreateLink() would succeed, but it doesn’t — the call to IPersistFile::Save() returns E_ACCESSDENIED.

For more information about developing on Vista, see the document Windows Vista Application Development Requirements for User Account Control Compatibility.

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in