Search This Blog

Showing posts with label Shortcut. Show all posts
Showing posts with label Shortcut. Show all posts

Thursday, July 16, 2009

Program to create shortcut programatically



int createshotcut(CString csShorcutFile,CString csSourceFile,CString csWorkingDirectory,CString Comments )
{
     CoInitialize( NULL );
    
     IShellLink* psl;
    
     // Get a pointer to the IShellLink interface.
    
     HRESULT hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
         
          IID_IShellLink, (PVOID *) &psl);
    
     if (SUCCEEDED(hres))
         
     {
         
          IPersistFile* ppf;
         
          // Set the path to the shortcut target and add the
         
          // description.
         
          psl->SetPath((LPCSTR) csSourceFile);
         
          psl->SetWorkingDirectory((LPCSTR)csWorkingDirectory);
         
          psl->SetDescription((LPCSTR) Comments );
         
          hres = psl->QueryInterface(IID_IPersistFile, (PVOID *) &ppf);
         
          if (SUCCEEDED(hres))
              
          {
              
               WORD wsz[MAX_PATH];
              
               MultiByteToWideChar(CP_ACP, 0, (LPCSTR) csShorcutFile, -1, wsz, MAX_PATH);
              
               hres = ppf->Save(wsz, TRUE);
              
               ppf->Release();
              
          }
         
          psl->Release();
         
     }
    
     CoUninitialize();

     return 1;
}


Example:
     createshotcut("c:\\mydairy.lnk","d:\\files\\secret.txt","d:\\","My Notepad Shortcut");