创建一个快捷方式("*.lnk) 那位大哥给个vc++版吧(100分)

  • 主题发起人 主题发起人 peiwents
  • 开始时间 开始时间
P

peiwents

Unregistered / Unconfirmed
GUEST, unregistred user!
创建快捷方式在Vc++中,很急的说
 
// create a shortcut to a file<br>//<br>// in:<br>// &nbsp; pszShortcutFile &nbsp; &nbsp; fully qualified path to shortcut file<br>// &nbsp; pszTo &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;shortcut points to this file<br>//<br>// returns:<br>// &nbsp; standard hres codes<br>//<br>// assumes:<br>// &nbsp; CoInitialize() has been called...<br><br>在执行这个之前,你要执行了CoInitialize()<br><br>HRESULT CreateShortCut(LPCSTR pszShortcutFile, LPSTR pszLink, <br> LPSTR pszDesc)<br>{<br>&nbsp; &nbsp; HRESULT hres;<br>&nbsp; &nbsp; IShellLink *psl;<br><br> // Create an IShellLink object and get a pointer to the IShellLink<br> // interface (returned from CoCreateInstance).<br>&nbsp; &nbsp; hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IID_IShellLink, (void **)&amp;psl);<br>&nbsp; &nbsp; if (SUCCEEDED(hres))<br>&nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp;IPersistFile *ppf;<br>&nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; // Query IShellLink for the IPersistFile interface for<br> &nbsp; // saving the shortcut in persistent storage.<br>&nbsp; &nbsp; &nbsp; &nbsp;hres = psl-&gt;QueryInterface(IID_IPersistFile, (void **)&amp;ppf);<br> &nbsp; if (SUCCEEDED(hres))<br> &nbsp; { &nbsp; <br> &nbsp; &nbsp; &nbsp; &nbsp;WORD wsz[MAX_PATH]; &nbsp; // buffer for Unicode string<br><br> &nbsp; // Set the path to the shortcut target.<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hres = psl-&gt;SetPath(pszShortcutFile);<br><br> if (!SUCCEEDED(hres))<br> AfxMessageBox("SetPath failed!");<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Set the description of the shortcut.<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hres = psl-&gt;SetDescription(pszDesc);<br><br> if (!SUCCEEDED(hres))<br> AfxMessageBox("SetDescription failed!");<br><br> &nbsp; &nbsp; // Ensure that the string consists of ANSI characters.<br> &nbsp; &nbsp; MultiByteToWideChar(CP_ACP, 0, pszLink, -1, wsz, MAX_PATH);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Save the shortcut via the IPersistFile::Save member function.<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hres = ppf-&gt;Save(wsz, TRUE);<br><br> if (!SUCCEEDED(hres))<br> AfxMessageBox("Save failed!");<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Release the pointer to IPersistFile.<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ppf-&gt;Release();<br>&nbsp; &nbsp; &nbsp; &nbsp;}<br>&nbsp; &nbsp; &nbsp; &nbsp;// Release the pointer to IShellLink.<br>&nbsp; &nbsp; &nbsp; &nbsp;psl-&gt;Release();<br>&nbsp; &nbsp; }<br>&nbsp; &nbsp; return hres;<br>}<br>
 
To here: www.vckbase.com
 
哇赛,这个问题还没有解决啊<br>
 
// 看看我的单元<br>unit ShlFunc; {本单元可使用尽可能少的资源完成下面的功能}<br>interface<br>const<br>//用于描述系统文件夹的前缀常量<br>&nbsp; nvF_PgmMenu = #$82; // [开始][程序]<br>&nbsp; nvF_MyDoc &nbsp; = #$85; // 我的文档<br>&nbsp; nvF_BookMrk = #$86; // 收藏夹<br>&nbsp; nvF_Startup = #$87; // [开始][启动]<br>&nbsp; nvF_Recent &nbsp;= #$88; // [开始][文档]<br>&nbsp; nvF_SendTo &nbsp;= #$89; // 发送到...<br>&nbsp; nvF_StrMenu = #$8B; // [开始]<br>&nbsp; nvF_Desktop = #$90; // 桌面<br><br>&nbsp; nvF_Windows = #$A0; // Windows<br>&nbsp; nvF_System &nbsp;= #$A1; // Windows/System<br>&nbsp; nvF_Temp &nbsp; &nbsp;= #$A2; // Temp Directory<br><br>&nbsp; nvF_PgmFile = #$A3; // Program Files<br>&nbsp; nvF_PgmComm = #$A4;<br>&nbsp; nvF_AppData = #$A5; // Application Data<br>//-----------------------------------------<br>&nbsp; nvF_ProgramMenu &nbsp; &nbsp; = nvF_PgmMenu;<br>&nbsp; nvF_MyDocuments &nbsp; &nbsp; = nvF_MyDoc;<br>&nbsp; nvF_Favorites &nbsp; &nbsp; &nbsp; = nvF_Bookmrk;<br>&nbsp; nvF_Bookmark &nbsp; &nbsp; &nbsp; &nbsp;= nvF_Bookmrk;<br>&nbsp; nvF_StartMenu &nbsp; &nbsp; &nbsp; = nvF_StrMenu;<br>&nbsp; nvF_ProgramFile &nbsp; &nbsp; = nvF_PgmFile;<br>&nbsp; nvF_CommonFile &nbsp; &nbsp; &nbsp;= nvF_PgmComm;<br>&nbsp; nvF_ApplicationData = nvF_AppData;<br>{/-----------------------------------------<br>===============================================================================<br>取系统文件夹, nvFolder 为上述常数, 可返回短文件名}<br>function GetSysFolder(nvFolder: Char; ShortPath: Boolean): String;<br>function DoGetSysFolder(nvFolder: Char; ShortPath: Boolean; var S: String): Integer; // 返回长度<br>{<br>展开如 nvF_Desktop+'MyFolder/MySubFolder' 的路径名, 结果可用 ExpandedPathName 变量访问}<br>function GetExpandPathName(const xPath: String): String;<br>function &nbsp;DoExpandPathName(const xPath: String): Integer; // 返回长度<br>{<br>搜索文件夹, 可返回短文件名, hWnd可以是0(nil)或调用窗口的Handle}<br>function SearchPaths(hWND: Integer; const Title: String; ShortPath: Boolean): String;<br>{<br>创建快捷方式(ShortCutName可描述为 nvF_xxx+'.../.../YYY" )<br>如果ShortCutName='' 那么加入到[开始][文档], 成功返回实际的lnk文件名长度, 失败返回0}<br>function CreateFileShortCut(const FileName, ShortCutName: String): Integer;<br>{<br>通用字符串函数(从Src中的第SrcId个开始拷贝Count个字符到Tar的TarId开始的位置, 返回目标串的长度)<br>该函数可避免频繁的字符串内存重分配<br>SrcId, TarId, Count都可以为0, TarId=0 时将在Tar后面连接Src, Count=0 时将一直复制到Src的结尾}<br>function StrCopyEx(const Src: String; var Tar: String; SrcId, TarId, Count: Integer): Integer;<br>{<br>取文件的短文件名}<br>function DOSFileName(const FileName: String): String;<br>function FileName8_3(var FileName: String): Integer; // 返回长度<br>{<br>路径是否存在, 如果AutoCreate, 那么如果路径不存在则自动创建}<br>function PathExists(const xPath: String; AutoCreate: Boolean): Boolean;<br>{<br>把文件删除到回收站}<br>procedure EraseToRecyle(const FileName: String);<br><br>var<br>&nbsp; ExpandedPathName: string;<br>///////////////////////////////////////////////////////////////////////////////<br>implementation<br><br>uses<br>&nbsp; ShellAPI, ShlObj, Windows;<br><br>var<br>&nbsp; pxBrowse: PBrowseInfoA;<br>&nbsp; pxItemID: PItemIDList;<br>&nbsp; BrowseDlgTitle: String;<br>{<br>===============================================================================<br>}<br>function StrCopyEx(const Src: String; var Tar: String; SrcId, TarId, Count: Integer): Integer;<br>var<br>&nbsp; I: Integer;<br>begin<br>&nbsp; if SrcId &lt;= 0 then SrcId:= 0 else Dec(SrcId);<br>&nbsp; if Count &lt;= 0 then Count:= Length(Src) - SrcId;<br>&nbsp; if TarId &lt;= 0 then TarId:= Length(Tar) else Dec(TarId);<br>&nbsp; Result:= TarId + Count;<br>&nbsp; if Result &gt; Length(Tar) then SetLength(Tar, Result);<br>&nbsp; for I:= 1 to Count do Tar[TarId + I]:= Src[SrcId + I];<br>end;<br>{<br>===============================================================================<br>}<br>function DOSFileName(const FileName: String): String;<br>begin<br>&nbsp; Result:= FileName;<br>&nbsp; SetLength(Result, FileName8_3(Result));<br>end;<br><br>function FileName8_3(var FileName: String): Integer;<br>begin<br>&nbsp; try<br>&nbsp; &nbsp; Result:= GetShortPathName(PChar(FileName), @FileName[1], Length(FileName));<br>&nbsp; &nbsp; if Result &lt; Length(FileName) then FileName[Result+1]:= #0;<br>&nbsp; except<br>&nbsp; &nbsp; Result:= 0;<br>&nbsp; end;<br>end;<br>{<br>===============================================================================<br>}<br>function GetExpandPathName(const xPath: String): String;<br>var<br>&nbsp; I, X: Integer;<br>begin<br>&nbsp; X:= DoExpandPathName(xPath);<br>&nbsp; SetLength(Result, X);<br>&nbsp; for I:= 1 to X do Result:= ExpandedPathName;<br>end;<br><br>function DoExpandPathName(const xPath: String): Integer;<br>begin<br>&nbsp; if Ord(xPath[1]) &lt; $80 then begin<br>&nbsp; &nbsp; ExpandedPathName:= xPath + #0;<br>&nbsp; &nbsp; Result:= Length(xPath);<br>&nbsp; end else begin<br>&nbsp; &nbsp; if Length(ExpandedPathName) &lt; 255 then SetLength(ExpandedPathName, 255);<br>&nbsp; &nbsp; Result:= DoGetSysFolder(xPath[1], false, ExpandedPathName);<br>&nbsp; &nbsp; Result:= StrCopyEx(xPath, ExpandedPathName, 2, Result+1, 0);<br>&nbsp; &nbsp; ExpandedPathName[Result+1]:= #0;<br>&nbsp; end;<br>end;<br><br>function PathExists(const xPath: String; AutoCreate: Boolean): Boolean;<br>var<br>&nbsp; X: Integer;<br>procedure CreatePaths;<br>var<br>&nbsp; I: Integer; ch: char;<br>begin<br>&nbsp; for I:= 1 to Length(ExpandedPathName) do begin<br>&nbsp; &nbsp; ch:= ExpandedPathName;<br>&nbsp; &nbsp; if ch = #0 then Break;<br>&nbsp; &nbsp; if ch &lt;&gt; '/' then Continue;<br><br>&nbsp; &nbsp; ch:= ExpandedPathName[I+1];<br>&nbsp; &nbsp; ExpandedPathName[I+1]:= #0;<br>&nbsp; &nbsp; X:= GetFileAttributes(@ExpandedPathName[1]);<br>&nbsp; &nbsp; ExpandedPathName[I+1]:= ch;<br>&nbsp; &nbsp; if (X &lt;&gt; -1) and (FILE_ATTRIBUTE_DIRECTORY and X &lt;&gt; 0) then Continue;<br><br>&nbsp; &nbsp; ExpandedPathName:= #0;<br>&nbsp; &nbsp; CreateDirectory(@ExpandedPathName[1], nil);<br>&nbsp; &nbsp; ExpandedPathName:= '/';<br>&nbsp; end;<br>end;<br><br>begin<br>&nbsp; DoExpandPathName(xPath);<br>&nbsp; X:= GetFileAttributes(@ExpandedPathName[1]);<br>&nbsp; Result:= (X &lt;&gt; -1) and (FILE_ATTRIBUTE_DIRECTORY and X &lt;&gt; 0);<br>&nbsp; if Result or (not AutoCreate) then Exit;<br>&nbsp; try CreatePaths; Result:= True; except end;<br>end;<br>{<br>===============================================================================<br>}<br>function GetSysFolder(nvFolder: Char; ShortPath: Boolean): String;<br>begin<br>&nbsp; SetLength(Result, 255);<br>&nbsp; SetLength(Result, DoGetSysFolder(nvFolder, ShortPath, Result));<br>end;<br><br>function DoGetSysFolder(nvFolder: Char; ShortPath: Boolean; var S: String): Integer;<br>var<br>&nbsp; X: Integer;<br>procedure zReadRegistry(const KeyName: String);<br>var<br>&nbsp; phk: HKEY;<br>begin<br>&nbsp; PShortString(X):= @S[1]; PShortString(X)^:= 'Software/Microsoft/Windows/CurrentVersion/Explorer/Shell Folders'#0;<br>&nbsp; if KeyName[1] = '/' then begin &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //01234567890123456789012345678901234567890123<br>&nbsp; &nbsp; S[43]:= #0; X:= HKEY_LOCAL_MACHINE; &nbsp; &nbsp;//0---------1---------2---------3---------4---<br>&nbsp; end else &nbsp; &nbsp; &nbsp;X:= HKEY_CURRENT_USER; &nbsp; &nbsp; //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>&nbsp; if RegOpenKeyEx(X, @S[2], 0, KEY_QUERY_VALUE, phk) = ERROR_SUCCESS then begin<br>&nbsp; &nbsp; X:= Length(S);<br>&nbsp; &nbsp; if RegQueryValueEx(phk, @KeyName[2], nil, nil, @S[1], Addr(X)) = ERROR_SUCCESS then Dec(X) else X:= 0;<br>&nbsp; end else X:= 0;<br>end;<br>begin Result:= 0; try<br>&nbsp; X:= Ord(nvFolder);<br>&nbsp; if X &lt; $A0 then begin<br>&nbsp; &nbsp; if SHGetSpecialFolderLocation(0, (X and $7F), pxItemID) &lt;&gt; NOERROR then Exit;<br>&nbsp; &nbsp; if pxItemID = nil then Exit;<br>&nbsp; &nbsp; if not SHGetPathFromIDList(pxItemID, @S[1]) then Exit;<br>&nbsp; &nbsp; X:= Pos(#0, S) - 1;<br>&nbsp; end else case nvFolder of<br>&nbsp; &nbsp; nvF_Windows: X:= GetWindowsDirectory(@S[1], 255);<br>&nbsp; &nbsp; nvF_System : X:= GetSystemDirectory(@S[1], 255);<br>&nbsp; &nbsp; nvF_Temp &nbsp; : X:= GetTempPath(255, @S[1]);<br>&nbsp; &nbsp; nvF_PgmFile: zReadRegistry('/ProgramFilesDir'#0);<br>&nbsp; &nbsp; nvF_PgmComm: zReadRegistry('/CommonFilesDir'#0);<br>&nbsp; &nbsp; nvF_AppData: zReadRegistry('.AppData'#0);<br>&nbsp; end; {case}<br>&nbsp; if X = 0 then Exit;<br>&nbsp; if ShortPath then X:= FileName8_3(S);<br>&nbsp; if S[X] &lt;&gt; '/' then begin Inc(X); S[X]:= '/'; end;<br>&nbsp; Result:= X; S[X+1]:= #0;<br>except Exit; end; end;<br>{<br>===============================================================================<br>}<br>function CreateFileShortCut(const FileName, ShortCutName: String): Integer;<br>var<br>&nbsp; X: Integer; S: String;<br>begin Result:= 0; try<br>&nbsp; //首先在"文档"里创建快捷方式, 如不指定目标则算完成<br>&nbsp; SHAddToRecentDocs(SHARD_PATH, PChar(FileName));<br>&nbsp; if Length(ShortCutName) = 0 then begin Dec(Result); Exit; end;<br><br>&nbsp; //取文件名的开始<br>&nbsp; for X:= Length(FileName) downto 1 do<br>&nbsp; if FileName[X] = '/' then begin Result:= X; Break; end;<br><br>&nbsp; //取源文件名("文档"菜单的路径)<br>&nbsp; SetLength(S, 255);<br>&nbsp; SHGetSpecialFolderLocation(0, CSIDL_RECENT, pxItemID);<br>&nbsp; SHGetPathFromIDList(pxItemID, @S[1]);<br>&nbsp; X:= Pos(#0, S); if S[X-1] &lt;&gt; '/' then begin S[X]:= '/'; Inc(X); end;<br><br>&nbsp; X:= StrCopyEx(FileName, S, Result + 1, X, 0) + 1;<br>&nbsp; Result:= 0; PShortString(@S[X])^:= 'lnk'#0; S[X]:= '.';<br><br>&nbsp; //定位目标文件<br>&nbsp; X:= DoExpandPathName(ShortCutName);<br>&nbsp; if not PathExists(ExpandedPathName, True) then Exit;<br>&nbsp; Result:= StrCopyEx('.lnk'#0, ExpandedPathName, 0, X+1, 0);<br>&nbsp; if CopyFile(@S[1], @ExpandedPathName[1], False) then DeleteFile(@S[1]);<br>except Result:= 0; end;<br>end;<br>{<br>===============================================================================<br>}<br>procedure InitBrowseInfo(hWND: Integer);<br>begin<br>&nbsp; if pxBrowse = nil then New(pxBrowse);<br>&nbsp; with pxBrowse^ do begin<br>&nbsp; &nbsp; hWndOwner:= hWND;<br>&nbsp; &nbsp; pidlRoot:= nil;<br>&nbsp; &nbsp; pszDisplayName:= nil;<br><br>&nbsp; &nbsp; lpszTitle:= @BrowseDlgTitle[1];<br>&nbsp; &nbsp; ulFlags:= BIF_RETURNONLYFSDIRS;<br>&nbsp; &nbsp; lpfn:= nil;<br>&nbsp; end;<br>end;<br>{<br>===============================================================================<br>}<br>function SearchPaths(hWND: Integer; const Title: String; ShortPath: Boolean): String;<br>begin SetLength(Result, 0); try;<br>&nbsp; if Length(Title) &gt; 0 then BrowseDlgTitle:= Title + #0;<br>&nbsp; InitBrowseInfo(hWND);<br>&nbsp; pxItemID:= SHBrowseForFolder(pxBrowse^);<br>&nbsp; Dispose(pxBrowse); pxBrowse:= nil;<br>&nbsp; if pxItemID = nil then Exit;<br><br>&nbsp; SetLength(Result, 255);<br>&nbsp; SHGetPathFromIDList(pxItemID, @Result[1]);<br>&nbsp; if ShortPath then hWND:= FileName8_3(Result) else hWND:= Pos(#0, Result) - 1;<br>&nbsp; if Result[hWND] &lt;&gt; '/' then begin Inc(hWND); Result[hWND]:= '/'; end;<br>&nbsp; SetLength(Result, hWND);<br>except SetLength(Result, 0); end; end;<br><br>procedure EraseToRecyle(const FileName: String);<br>{ 利用ShellApi中: function SHFileOperation(const lpFileOp:TSHFileOpStruct): Integer; stdcall; }<br>var<br>&nbsp; T: TSHFileOpStruct;<br>begin<br>&nbsp; with T do begin<br>&nbsp; &nbsp; Wnd:=0;<br>&nbsp; &nbsp; wFunc:=FO_DELETE;<br>&nbsp; &nbsp; pFrom:=Pchar(FileName);<br>&nbsp; &nbsp; fFlags:=FOF_ALLOWUNDO<br>&nbsp; end;<br>&nbsp; SHFileOperation(T);<br>end;<br>{<br>===============================================================================<br>}<br>initialization<br>&nbsp; BrowseDlgTitle:= '搜索文件夹'#0;<br>&nbsp; SetLength(ExpandedPathName, 0);<br>&nbsp; pxBrowse:= nil;<br>finalization<br>&nbsp; if pxBrowse &lt;&gt; nil then Dispose(pxBrowse);<br>end.<br>
 
收藏,太好了
 
http://vip.6to23.com/NowCan1/tech/shortcut.htm
 
后退
顶部