Advertisement
Guest User

Using Shell Links

a guest
May 24th, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "windows.h"
  3. #include "winnls.h"
  4. #include "shobjidl.h"
  5. #include "objbase.h"
  6. #include "objidl.h"
  7. #include "shlguid.h"
  8.  
  9. HRESULT CreateLink(LPCWSTR lpszPathObj, LPCSTR lpszPathLink, LPCWSTR lpszDesc)
  10. {
  11.   HRESULT hres;
  12.   IShellLink* psl;
  13.  
  14.   hres = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
  15.   // Get a pointer to the IShellLink interface. It is assumed that CoInitialize
  16.   // has already been called.
  17.   hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
  18.   if (SUCCEEDED(hres))
  19.   {
  20.     IPersistFile* ppf;
  21.  
  22.     // Set the path to the shortcut target and add the description.
  23.     psl->SetPath(lpszPathObj);
  24.     psl->SetDescription(lpszDesc);
  25.  
  26.     // Query IShellLink for the IPersistFile interface, used for saving the
  27.     // shortcut in persistent storage.
  28.     hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
  29.  
  30.     if (SUCCEEDED(hres))
  31.     {
  32.       WCHAR wsz[MAX_PATH];
  33.  
  34.       // Ensure that the string is Unicode.
  35.       MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1, wsz, MAX_PATH);
  36.  
  37.       // Add code here to check return value from MultiByteWideChar
  38.       // for success.
  39.  
  40.       // Save the link by calling IPersistFile::Save.
  41.       hres = ppf->Save(wsz, TRUE);
  42.       ppf->Release();
  43.     }
  44.     psl->Release();
  45.   }
  46.   CoUninitialize();
  47.   return hres;
  48. }
  49.  
  50. int main(void)
  51. {
  52.   return CreateLink(_T("C:\\Program Files (x86)\\Notepad++\\notepad++.exe"),
  53.                     "c:\\Temp\\notepad_test2", _T("DESC"));
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement