Advertisement
overloop

create_link.cpp

Mar 25th, 2014
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.70 KB | None | 0 0
  1.  
  2. #define UNICODE
  3. #define _UNICODE
  4.  
  5. #include <objbase.h>
  6. #include <winuser.h>
  7. #include <windef.h>
  8. #include <shlobj.h>
  9. #include <olectlid.h>
  10. #include <objidl.h>
  11. #include <tchar.h>
  12.  
  13. #include <iostream>
  14.  
  15. HRESULT CreateLink(LPCWSTR lpszPathObj, LPCSTR lpszPathLink, LPCWSTR lpszDesc)
  16. {
  17.     HRESULT hres;
  18.     IShellLink* psl;
  19.  
  20.     // Get a pointer to the IShellLink interface. It is assumed that CoInitialize
  21.     // has already been called.
  22.     hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
  23.    
  24.     if (SUCCEEDED(hres))
  25.     {
  26.         IPersistFile* ppf;
  27.  
  28.         // Set the path to the shortcut target and add the description.
  29.         psl->SetPath(lpszPathObj);
  30.         psl->SetDescription(lpszDesc);
  31.  
  32.         // Query IShellLink for the IPersistFile interface, used for saving the
  33.         // shortcut in persistent storage.
  34.         hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
  35.  
  36.         if (SUCCEEDED(hres))
  37.         {
  38.             WCHAR wsz[MAX_PATH];
  39.  
  40.             // Ensure that the string is Unicode.
  41.             MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1, wsz, MAX_PATH);
  42.  
  43.             // Add code here to check return value from MultiByteWideChar
  44.             // for success.
  45.  
  46.             // Save the link by calling IPersistFile::Save.
  47.             hres = ppf->Save(wsz, TRUE);
  48.             ppf->Release();
  49.         }
  50.         psl->Release();
  51.     }
  52.     return hres;
  53. }
  54.  
  55. int main(int argc, char** argv) {
  56.     CoInitialize(0);
  57.     LPCWSTR lpszPathObj = L"C:\\temp";
  58.     LPCSTR lpszPathLink = "C:\\temp\\test.lnk";
  59.     LPCWSTR lpszDesc = L"test";
  60.     CreateLink(lpszPathObj, lpszPathLink, lpszDesc);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement