リンクファイルから参照先のファイル名を得る
|
IShellLinkを使用すれば出来ます。
CoInitialize(NULL);
HRESULT HR;
IShellLink *psl;
IPersistFile *ppf;
WideChar wsz[MAX_PATH];
char szGotPath[MAX_PATH];
WIN32_FIND_DATA wfd;
HR = CoCreateInstance(CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER, IID_IShellLink, (void **)&psl);
if(SUCCEEDED(HR)){
HR = psl->QueryInterface(IID_IPersistFile, (void **)&ppf);
if(SUCCEEDED(HR)){
MultiByteToWideChar(CP_ACP, 0, "ショートカット.lnk", -1,
wsz, MAX_PATH );
HR = ppf->Load( wsz, STGM_READ );
if(SUCCEEDED(HR)){
HR = psl->Resolve(Application->Handle, SLR_ANY_MATCH);
if(SUCCEEDED(HR)){
HR = psl->GetPath(szGotPath, MAX_PATH,
(WIN32_FIND_DATA *)&wfd, SLGP_SHORTPATH );
Edit2->Text = szGotPath;
}
}
}
ppf->Release();
}
psl->Release();
}
CoUninitialize();
Delphi翻訳版 (関数です)
uses
Windows, SysUtils, ShlObj, ActiveX, ComObj;
const IID_IPersistFile: TGUID= (D1:$0000010B;D2:$0000;D3:$0000;D4:($C0,$00,$00,$00,$00,$00,$00,$46));
function ExtractStringFromLinkFile( Target:TFileName ): String;
var
R : HRESULT;
SL : IShellLink;
PF : IPersistFile;
Wrk : array[0..MAX_PATH] of WideChar;
Path: array[0..MAX_PATH] of Char;
WFD : WIN32_FIND_DATA;
begin
Result := '';
CoInitialize( NIL );
R := CoCreateInstance( CLSID_ShellLink, NIL, CLSCTX_INPROC_SERVER, IID_IShellLinkA, SL );
if Succeeded( R ) then
begin
R := SL.QueryInterface( IID_IPersistFile, PF );
if Succeeded( R ) then
begin
MultiByteToWideChar( CP_ACP, 0, PChar(Target), -1, Wrk, MAX_PATH );
R := PF.Load( Wrk, STGM_READ );
if Succeeded( R ) then
begin
R := SL.Resolve( 0{Application.Handle}, SLR_ANY_MATCH );
if Succeeded( R ) then
begin
SL.GetPath( Path, MAX_PATH, WFD, SLGP_SHORTPATH );
Result := Path;
end;
end;
end;
end;
CoUninitialize;
end;
|
|