取得LNK文件的目的文件的信息

I

import

Unregistered / Unconfirmed
GUEST, unregistred user!
function LinkFileInfo(const lnkFileName:string;var info:LINK_FILE_INFO;const bSet:boolean):boolean; var
hr:hresult;
psl:IShelllink;
wfd:win32_find_data;
ppf:IPersistFile;
lpw:pwidechar;
buf:pwidechar;
begin
result:=false;
getmem(buf,MAX_PATH);
try
if SUCCEEDED(CoInitialize(nil)) then
if (succeeded(cocreateinstance(clsid_shelllink,nil,clsctx_inproc_server,IID_IShellLinkA,psl))) then
begin
hr:=psl.QueryInterface(iPersistFile,ppf);
if succeeded(hr) then
begin
lpw:=stringtowidechar(lnkfilename,buf,MAX_PATH);
hr := ppf.Load(lpw, STGM_READ);
if succeeded(hr) then
begin
hr := psl.Resolve(0, SLR_NO_UI);
if succeeded(hr) then
begin
if bSet then
begin
psl.SetArguments(info.Arguments);
psl.SetDescription(info.Description);
psl.SetHotkey(info.HotKey);
psl.SetIconLocation(info.IconLocation,info.IconIndex);
psl.SetIDList(info.ItemIDList);
psl.SetPath(info.FileName);
psl.SetShowCmd(info.ShowState);
psl.SetRelativePath(info.RelativePath,0);
psl.SetWorkingDirectory(info.WorkDirectory);
if succeeded(psl.Resolve(0,SLR_UPDATE)) then
result:=true;
end
else
begin
psl.GetPath(info.FileName,MAX_PATH, wfd,SLGP_SHORTPATH );
psl.GetIconLocation(info.IconLocation,MAX_PATH,info.IconIndex);
psl.GetWorkingDirectory(info.WorkDirectory,MAX_PATH);
psl.GetDescription(info.Description,CCH_MAXNAME);
psl.GetArguments(info.Arguments,MAX_PATH);
psl.GetHotkey(info.HotKey);
psl.GetIDList(info.ItemIDList);
psl.GetShowCmd(info.ShowState);
result:=true;
end;
end;
end;
end;
end;
finally
freemem(buf);
end;
end;
//*******************调用*****************
showmessage('FileName:'+info.filename+#13+'Description:'+info.Description+#13+'IconFilename:'+info.IconLocation+','+inttostr(info.IconIndex)+
#13+'WordDir:'+info.WorkDirectory+#13+'Arguments:'+info.Arguments+#13+'ShorCuts:'+shortcuttotext(s));
info.WorkDirectory:='D:';
linkfileinfo(opendialog1.filename,info,true);
end;
end;
==============================================
function GetLinkFileName(sLinkFileName: string; var sTargetFileName: string): boolean;
var
psl: IShellLink;
ppf: IPersistFile;
hres, nLen: integer;
pfd: TWin32FindData;
pTargetFile: PChar;
pwLinkFileName: PWideChar;
begin
Result := false;
if SUCCEEDED(CoInitialize(nil)) then
begin
CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER, IID_IShellLinkA, psl);
if (SUCCEEDED(hres)) then
begin
hres := psl.QueryInterface(IID_IPersistFile, ppf);
if (SUCCEEDED(hres)) then
begin
 
nLen := MultiByteToWideChar(CP_ACP, 0, PChar(sLinkFileName), -1, pwLinkFileName, 0);
GetMem(pwLinkFileName, nLen);
ZeroMemory(pwLinkFileName, nLen);
MultiByteToWideChar(CP_ACP, 0, PChar(sLinkFileName), -1, pwLinkFileName, nLen);
ppf.Load(pwLinkFileName, STGM_READ);
GetMem(pTargetFile, MAX_PATH);
ZeroMemory(pTargetFile, MAX_PATH);
hres := psl.GetPath(pTargetFile, MAX_PATH, pfd, SLGP_UNCPRIORITY );
if (SUCCEEDED(hres)) then
begin
sTargetFileName := StrPas(pTargetFile);
Result := true;
end;
FreeMem(pTargetFile);
FreeMem(pwLinkFileName);
end;
end;
end;
end;
///////////////////////////////////////////////////////////
IShellLink::Resolve
IShellLink::GetIconLocation
IShellLink::GetPath
IShellLink::GetWorkingDirectory
下面是 VC 代码(我不会 DELPHI,我想转换过去应当容易):
BOOL CLinkTestDlg::ResolveLink(HWND hwnd, LPCTSTR lpszLinkFile, LPTSTR lpszPath)
{
USES_CONVERSION;
HRESULT hr;
IShellLink *psl;
TCHAR szGotPath[MAX_PATH];
TCHAR szDescription[MAX_PATH];
WIN32_FIND_DATA wfd;
*lpszPath = 0; // assume failure
// Get a pointer to the IShellLink interface.
hr = CoCreateInstance(CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *) &psl);
if (SUCCEEDED(hr))
{
IPersistFile *ppf;
// Get a pointer to the IPersistFile interface.
hr = psl->QueryInterface(IID_IPersistFile, (void **)&ppf);
if (SUCCEEDED(hr))
{
LPWSTR lpw;
// Ensure that the string is Unicode.
lpw = T2W(lpszLinkFile);
// Load the shortcut.
hr = ppf->Load(lpw, STGM_READ);
if (SUCCEEDED(hr))
{
// Resolve the link.
hr = psl->Resolve(hwnd, SLR_NO_UI);
if (SUCCEEDED(hr))
{
// Get the path to the link target.
hr = psl->GetPath(szGotPath,
MAX_PATH, (WIN32_FIND_DATA *)&wfd,
SLGP_SHORTPATH );
if (FAILED(hr))

// Get the description of the target.
hr = psl->GetDescription(szDescription, MAX_PATH);
if (FAILED(hr))

lstrcpy(lpszPath, szGotPath);
}
}
// Release the pointer to the IPersistFile interface.
ppf->Release();
}
// Release the pointer to the IShellLink interface.
psl->Release();
}
return hr;
}
====================================
 
顶部