怎样得到一个快捷方式(.lnk)指向的目标文件名?(100分)

program shllnkapp;<br>uses<br>&nbsp; Windows,<br>&nbsp; Activex,<br>&nbsp; ShlObj;<br>const<br>&nbsp; cslnk: WideString = 'C:/Documents and Settings/yushf/桌面/SSEXP.EXE.lnk';<br>var<br> lnk: IShellLink;<br>&nbsp; bu: array[0..MAX_PATH]of char;<br>begin<br> CoInitialize(nil);<br>&nbsp; if CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER or<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CLSCTX_LOCAL_SERVER, IUnknown, lnk) = S_OK then<br>&nbsp; try<br>&nbsp; &nbsp; (lnk as IPersistFile).Load(@cslnk[1], STGM_READ);<br>&nbsp; &nbsp; lnk.Resolve(0, SLR_ANY_MATCH or SLR_NO_UI);<br>&nbsp; &nbsp; ZeroMemory(@bu[0], sizeof(bu));<br>&nbsp; lnk.GetPath(@bu[0], sizeof(bu), PWin32FindData(nil)^, SLGP_UNCPRIORITY);<br>&nbsp; &nbsp; MessageBox(0, @bu[0], nil, 0);<br>&nbsp; finally<br>&nbsp; &nbsp; lnk := nil;<br>&nbsp; end;<br>&nbsp; CoUninitialize;<br>end.
 
unit lnk;<br><br>interface<br>uses<br>&nbsp; Activex,<br>&nbsp; Comobj,<br>&nbsp; shlobj,<br>&nbsp; windows,<br>&nbsp; SysUtils;<br>type<br>&nbsp; PShellLink = ^TshellLink;<br>&nbsp; TShellLink = record<br>&nbsp; &nbsp; PathName, Arguments, Description, WorkingDirectory, IconLocation: string;<br>&nbsp; &nbsp; IconIndex: Integer;<br>&nbsp; &nbsp; ShowCmd: Integer;<br>&nbsp; &nbsp; HotKey: Word;<br>&nbsp; end;<br>function GetShellLink(const LinkFile: WideString): TShellLink;<br>procedure CreateLnk(sFileName, sLink: string; pchDes: LPSTR);<br><br><br>implementation<br><br>function GetShellLink(const LinkFile: WideString): TShellLink;<br>var<br>&nbsp; SL: IShellLink;<br>&nbsp; PF: IPersistFile;<br>&nbsp; FindData: TWin32FindData;<br>&nbsp; AStr: array[0..MAX_PATH] of char;<br>begin<br>&nbsp; OleCheck(CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER,<br>&nbsp; &nbsp; IShellLink, SL));<br>&nbsp; PF := SL as IPersistFile;<br>&nbsp; OleCheck(PF.Load(PWideChar(LinkFile), STGM_READ));<br>&nbsp; OleCheck(SL.Resolve(0, SLR_ANY_MATCH or SLR_NO_UI));<br>&nbsp; with Result do<br>&nbsp; begin<br>&nbsp; &nbsp; OleCheck(SL.GetPath(AStr, MAX_PATH, FindData, SLGP_UNCPRIORITY));<br>&nbsp; &nbsp; PathName := AStr;<br>&nbsp; &nbsp; OleCheck(SL.GetArguments(AStr, MAX_PATH));<br>&nbsp; &nbsp; Arguments := AStr;<br>&nbsp; &nbsp; OleCheck(SL.GetDescription(AStr, MAX_PATH));<br>&nbsp; &nbsp; Description := AStr;<br>&nbsp; &nbsp; OleCheck(SL.GetWorkingDirectory(AStr, MAX_PATH));<br>&nbsp; &nbsp; WorkingDirectory := AStr;<br>&nbsp; &nbsp; OleCheck(SL.GetIconLocation(AStr, MAX_PATH, IconIndex));<br>&nbsp; &nbsp; IconLocation := AStr;<br>&nbsp; &nbsp; OleCheck(SL.GetShowCmd(ShowCmd));<br>&nbsp; &nbsp; OleCheck(SL.GetHotKey(HotKey));<br>&nbsp; end;<br>end;<br><br>&nbsp; { &nbsp;建立Lnk档 &nbsp;}<br>procedure CreateLnk(sFileName, sLink: string; pchDes: LPSTR);<br>var<br>&nbsp; psl: IShellLink;<br>&nbsp; ppf: IPersistFile;<br>&nbsp; pchBuf: array[0..1024] of Char;<br>&nbsp; wch: array[0..1024] of WideChar;<br>begin<br>&nbsp; try<br>&nbsp; &nbsp; psl := CreateComObject(CLSID_ShellLink) as IShellLink;<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; ppf := psl as IPersistFile;<br>&nbsp; &nbsp; &nbsp; OleCheck(psl.SetDescription(pchDes));<br>&nbsp; &nbsp; &nbsp; StrPCopy(pchBuf, sFileName);<br>&nbsp; &nbsp; &nbsp; OleCheck(psl.SetPath(pchBuf)); StrPCopy(pchBuf, sLink);<br>&nbsp; &nbsp; &nbsp; MultiByteToWideChar(CP_ACP, 0, pchBuf, -1, wch, 1024);<br>&nbsp; &nbsp; &nbsp; OleCheck(ppf.Save(wch, True));<br>&nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; ppf := nil;<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; psl := nil;<br>&nbsp; end;<br>end;<br><br>end.
 
来晚一步<br>uses<br>&nbsp; &nbsp; &nbsp;Windows,ComObj,ShlObj,ActiveX;<br><br>{<br>&nbsp; &nbsp; &nbsp; 函数功能:创建指定文件的快捷方式<br>&nbsp; &nbsp; &nbsp; TargetFile: 快捷方式指向的文件或目录<br>&nbsp; &nbsp; &nbsp; CreateAt: &nbsp; &nbsp; &nbsp;创建的快捷方式保存路径<br>&nbsp; &nbsp; &nbsp; 函数返回True表示操作成功,False表示失败<br>}<br>function CreateShortCut(TargetFile,CreateAt:string):Boolean;<br>const<br>&nbsp; &nbsp; &nbsp;IID_IPersistFile:TGUID = '{0000010B-0000-0000-C000-000000000046}';<br>var<br>&nbsp; &nbsp; &nbsp;intfLink:IShellLink;<br>&nbsp; &nbsp; &nbsp;IntfPersist:IPersistFile;<br>begin<br>&nbsp; &nbsp; &nbsp;IntfLink:=CreateComObject(CLSID_ShellLink) as IShellLink;<br>&nbsp; &nbsp; &nbsp;Result:=(IntfLink&lt;&gt;nil) and SUCCEEDED(IntfLink.QueryInterface(IID_IPersistFile,IntfPersist))<br>&nbsp; &nbsp; &nbsp;and SUCCEEDED(intfLink.SetPath(PAnsiChar(TargetFile))) and<br>&nbsp; &nbsp; &nbsp;SUCCEEDED(IntfPersist.Save(PWideChar(WideString(CreateAt)),True));<br>end;<br><br><br>{<br>&nbsp; &nbsp; &nbsp; 函数功能:返回指定快捷方式的目标文件<br>&nbsp; &nbsp; &nbsp; LinkFile: 快捷方式文件完整路径<br>&nbsp; &nbsp; &nbsp; 函数返回空字符串时表示失败,否则成功<br>}<br><br>function GetTargetOfShorCut(LinkFile:string):string;<br>const<br>&nbsp; &nbsp; &nbsp;IID_IPersistFile:TGUID = '{0000010B-0000-0000-C000-000000000046}';<br>var<br>&nbsp; &nbsp; &nbsp;intfLink:IShellLink;<br>&nbsp; &nbsp; &nbsp;IntfPersist:IPersistFile;<br>&nbsp; &nbsp; &nbsp;pfd:_WIN32_FIND_DATA;<br>&nbsp; &nbsp; &nbsp;bSuccess:Boolean;<br>begin<br>&nbsp; &nbsp; &nbsp;Result:='';<br>&nbsp; &nbsp; &nbsp;IntfLink:=CreateComObject(CLSID_ShellLink) as IShellLink;<br>&nbsp; &nbsp; &nbsp;SetString(Result,nil,MAX_PATH);<br>&nbsp; &nbsp; &nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp;Load方法的第二个参数还可以传递STGM_WRITE或STGM_READWRITE,表示对快捷方式信息的访问权限<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;STGM_READ:只读 <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;STGM_WRITE:只写 <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;STGM_READWRITE:读写<br>&nbsp; &nbsp; &nbsp; &nbsp;GetPath方法的第三个参数还可以传递SLGP_UNCPRIORITY或SLGP_SHORTPATH,表示返回的目标路径格式<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SLGP_UNCPRIORIT:UNC网络路径<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SLGP_SHORTPATH :DOS 8.3格式路径<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SLGP_RAWPATH &nbsp; &nbsp; &nbsp;: 长路径<br>&nbsp; &nbsp; &nbsp;}<br>&nbsp; &nbsp; &nbsp;bSuccess:=(IntfLink&lt;&gt;nil) and SUCCEEDED(IntfLink.QueryInterface(IID_IPersistFile,IntfPersist))<br>&nbsp; &nbsp; &nbsp; and SUCCEEDED(IntfPersist.Load(PWideChar(WideString(LinkFile)),STGM_READ)) and<br>&nbsp; &nbsp; &nbsp; SUCCEEDED(intfLink.GetPath(PAnsiChar(Result),MAX_PATH,pfd,SLGP_RAWPATH));<br>&nbsp; &nbsp; &nbsp;if not bSuccess then Result:='';<br>end;<br><br><br>除SetPath和GetPath方法外,IShellLink接口的其它方法可以设置或读取快捷方式的其它信息:<br>GetArguments:获得参数信息 <br>GetDescription:获得描述信息<br>GetHotkey:获得快捷键<br>GetIconLocation:获得图标 &nbsp;<br>GetIDList:获得快捷方式的目标对象的item identifier list (Windows外壳中的每个对象如文件,目录和打印机等都有唯一的item identifiler list)<br>GetPath: 获得快捷方式的目标文件或目录的全路径<br>GetShowCmd:获得快捷方式的运行方式,比如常规窗口,最大化<br>GetWorkingDirectory:获得工作目录 <br>Resolve:按照一定的搜索规则试图获得目标对象,即使目标对象已经被删除或移动,重命名<br>下面是对应信息的设置方法<br>SetArguments <br>SetDescription <br>SetHotkey<br>SetIconLocation<br>SetIDList<br>SetPath<br>SetRelativePat<br>SetShowCmd<br>SetWorkingDirectory
 
多人接受答案了。
 

Similar threads

顶部