Verknüpfungsdatei (lnk-File) erzeugen



  • Hi Leute,
    weiß einer, wie ich in C++ (MFC) eine Verknüpfungsdatei (lnk-File) erzeugen kann?
    Es würde mir auch schon nützen, wenn ich die Struktur einer solchen Datei wüßte.
    Dank im Vorraus
    peseif



  • Hier der Auschschnitcode aus der Klasse die ich dafür nutze:

    BOOL CShortcut::CreateShortCut(CString LnkTarget, CString LnkName, UINT SpecialFolder, CString LnkDescription, CString IconLocation, UINT IconIndex)
    {
    HRESULT hr;
    CFile cfFull;
    CString sExePath, sExe, sSpecialFolder;
    
    	char *chTmp = sExePath.GetBuffer(MAX_PATH);
    
    	GetModuleFileName(NULL, chTmp, MAX_PATH);
    
    		sExePath.ReleaseBuffer();
    
    // Find the Special Folder:
    	if(!GetSpecialFolder(SpecialFolder, sSpecialFolder))
    			return FALSE;
    
    	sSpecialFolder += LnkName + "." + "lnk";
    
    		if(LnkTarget == "_this")	
    			{
    				cfFull.SetFilePath(sExePath);
    				sExe = cfFull.GetFileName();
    				sExe.Delete(sExe.Find(".") + 1, 3);
    			}
    		else
    			{
    				sExePath = LnkTarget;
    			}
    
    // Create the ShortCut:
    	CoInitialize(NULL);
        BOOL bRet = FALSE;
        IShellLink* psl;
    
        if (SUCCEEDED( CoCreateInstance(CLSID_ShellLink,
    									NULL,
    									CLSCTX_INPROC_SERVER,
    									IID_IShellLink,
    									(LPVOID*) &psl)))
        {
            IPersistFile* ppf;
    		// 
            psl->SetPath(sExePath);
            psl->SetDescription(LnkDescription);
    
    		if(!m_sCmdArg.IsEmpty())
    			psl->SetArguments(m_sCmdArg);
    
            if (SUCCEEDED(psl->QueryInterface(IID_IPersistFile, (LPVOID *)&ppf)))
    			{
    	            WORD wsz[MAX_PATH];
    
    		        MultiByteToWideChar(CP_ACP,
    			                        MB_PRECOMPOSED,
    				                    sSpecialFolder,
    					                -1,
    						            wsz,
    							        MAX_PATH);
    
    				/* Call IShellLink::SetIconLocation with the file containing
    									the icon and the index of the icon */
    				if(!IconLocation.IsEmpty())
    					{
    						hr = psl->SetIconLocation(IconLocation, IconIndex);	
    							#ifdef _DEBUG
    								if(FAILED(hr))
    									TRACE("IconLocation not changed!\n");
    							#endif
    					}
    
    				if(SUCCEEDED(ppf->Save(wsz, TRUE)))
    					{
    						bRet = TRUE;
    					}
    
    		        ppf->Release();
    			}
            psl->Release();
        } 
    
    		if(bRet)
    			{
    				TRACE("Lnk Written!\n");
    			}
    		else
    			{
    				TRACE("Lnk NOT Written! CreateShortCut(...) failed!\n");
    			}
    return bRet;
    }
    


  • Wenn es das ganze nochmal in leicht verdaulich geben würde, wär ich glücklich - denn mich interessiert, wie ich auslese, auf was verwiesen wird. 🙂



  • Wenns hilft: http://www.codeproject.com/shell/CShortcut.asp <<-- das ist die klasse


Anmelden zum Antworten