Creating Shortcuts

If you want to directly create shortcuts with your program, you can use the following code fragment:

(it is taken from the "ln -s" command of ntsh.)


// important lines are marked with /***/

         // symbolic link
        IShellLink* psl; 
 
        CString strPath;
        if (m_args[2].Find(':')==-1)
        {
            GetFullPathName(m_args[2], MAX_PATH, strPath.GetBuffer(MAX_PATH), NULL);
            strPath.ReleaseBuffer();
        }
        // don't try to get full name with internet addresses
        else strPath = m_args[2];

        // Get a pointer to the IShellLink interface. 
        HRESULT hres;
/***/   CoInitialize(NULL);
/***/   hres = CoCreateInstance(CLSID_ShellLink, NULL, 
/***/       CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&psl); 
        if ((SUCCEEDED(hres) &&
            (psl->SetPath(strPath) == NOERROR) && 
            (psl->SetDescription(m_args[3]) == NOERROR)))
        {
            // Query IShellLink for the IPersistFile interface for saving the 
            // shortcut in persistent storage. 
            IPersistFile* ppf;
/***/       hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf); 
 
            if (SUCCEEDED(hres))
            {
                // Ensure that the string is wide char
#ifdef _UNICODE
                CString wsz = m_args[3]+".lnk";
#else
                WCHAR wsz[MAX_PATH]; 
                MultiByteToWideChar(CP_ACP, 0, m_args[3]+".lnk", -1, 
                    wsz, MAX_PATH); 
#endif
                // Save the link by calling IPersistFile::Save. 
                TRACE("Creating symbolic link in %s.lnk\n", m_args[3]);
/***/           hres = ppf->Save(wsz, TRUE); 
/***/           ppf->Release(); 
                bSuccess = SUCCEEDED(hres);
            } 
/***/       psl->Release();
        }
/***/   CoUninitialize();

You are free to use it in your own programs

Martin Waitz
don't miss to visit my private homepage (in German).


This page hosted by Get your own Free Home Page
1