如:生成一个快捷方式,读取快捷方式文件所指向的文件的路径等

解决方案 »

  1.   

    http://www.codeguru.com/shell/filelinks.shtml
      

  2.   

    //创建快捷方式BOOL CreateLink ( LPSTR szPath,//快捷方式的目标应用程序名LPSTR szLink)//快捷方式的数据文件名(*.lnk){HRESULT hres ;IShellLink * psl ;IPersistFile* ppf ;WORD wsz[ MAX_PATH] ;//创建一个IShellLink实例hres = CoCreateInstance( CLSID_ShellLink, NULL,CLSCTX_INPROC_SERVER, IID_IShellLink,(void **)&psl) ;if( FAILED( hres))return FALSE ;//设置目标应用程序psl -> SetPath( szPath) ;//设置快捷键(此处设为Shift+Ctrl+'R')psl -> SetHotkey( MAKEWORD( 'R',HOTKEYF_SHIFT ¦HOTKEYF_CONTROL)) ;//从IShellLink获取其IPersistFile接口//用于保存快捷方式的数据文件 (*.lnk)hres = psl -> QueryInterface( IID_IPersistFile,(void**)&ppf) ;if( FAILED( hres))return FALSE ;// 确保数据文件名为ANSI格式MultiByteToWideChar( CP_ACP, 0, szLink, -1, wsz, MAX_PATH) ;//调用IPersistFile::Save//保存快捷方式的数据文件 (*.lnk)hres = ppf -> Save( wsz, STGM_READWRITE) ;//释放IPersistFile和IShellLink接口ppf -> Release( ) ;psl -> Release( ) ;return TRUE;}
      

  3.   

    解析快捷方式:
    HRESULT ResolveIt(HWND hwnd, LPCSTR lpszLinkFile, LPSTR lpszPath) 

        HRESULT hres; 
        IShellLink* psl; 
        char szGotPath[MAX_PATH]; 
        char szDescription[MAX_PATH]; 
        WIN32_FIND_DATA wfd; 
     
        *lpszPath = 0; // assume failure 
     
        // Get a pointer to the IShellLink interface. 
        hres = CoCreateInstance(&CLSID_ShellLink, NULL, 
                CLSCTX_INPROC_SERVER, &IID_IShellLink, &psl); 
        if (SUCCEEDED(hres)) { 
            IPersistFile* ppf; 
     
            // Get a pointer to the IPersistFile interface. 
            hres = psl->lpVtbl->QueryInterface(psl, &IID_IPersistFile, 
                &ppf); 
            if (SUCCEEDED(hres)) { 
                WORD wsz[MAX_PATH]; 
     
                // Ensure that the string is Unicode. 
                MultiByteToWideChar(CP_ACP, 0, lpszLinkFile, -1, wsz, 
                    MAX_PATH); 
     
                // Load the shortcut. 
                hres = ppf->lpVtbl->Load(ppf, wsz, STGM_READ); 
                if (SUCCEEDED(hres)) { 
     
                    // Resolve the link. 
                    hres = psl->lpVtbl->Resolve(psl, hwnd, SLR_ANY_MATCH); 
                    if (SUCCEEDED(hres)) { 
     
                        // Get the path to the link target. 
                        hres = psl->lpVtbl->GetPath(psl, szGotPath, 
                            MAX_PATH, (WIN32_FIND_DATA *)&wfd, 
                            SLGP_SHORTPATH ); 
                        if (!SUCCEEDED(hres) 
                            HandleErr(hres); // application-defined function 
     
                        // Get the description of the target. 
                        hres = psl->lpVtbl->GetDescription(psl, 
                            szDescription, MAX_PATH); 
                        if (!SUCCEEDED(hres)) 
                            HandleErr(hres); 
                        lstrcpy(lpszPath, szGotPath); 
                    } 
                } 
            // Release the pointer to the IPersistFile interface. 
            ppf->lpVtbl->Release(ppf); 
            } 
        // Release the pointer to the IShellLink interface. 
        psl->lpVtbl->Release(psl); 
        } 
        return hres;