//==============================================================================
//获得或设置快捷方式文件******************************************************
//==============================================================================
function LinkFileInfo(const LinkFileName: string; var Info: LINK_FILE_INFO; const bSet: boolean=false): boolean;
var HResu: HResult;
Buffer: PWideChar;
ShellLink: IShelllink;
PersistFile: IPersistFile;
Win32FindData: WIN32_FIND_DATA;
begin
Result := false;
GetMem(Buffer, MAX_PATH);
try
if Succeeded(CoInitialize(nil))
then if (Succeeded(coCreateInstance(CLSID_SHELLLINK, nil, CLSCTX_INPROC_SERVER, IID_IShellLinkA, ShellLink)))
then begin//04
HResu := ShellLink.QueryInterface(iPersistFile, PersistFile);
if Succeeded(HResu)
then begin//03
HResu := PersistFile.Load(StringToWideChar(LinkFileName, Buffer, MAX_PATH), STGM_READ);
if Succeeded(HResu)
then begin//02
HResu := ShellLink.Resolve(0, SLR_NO_UI);
if Succeeded(HResu)
then begin//01
if bSet
then begin
ShellLink.SetArguments(Info.Arguments);
ShellLink.SetDescription(Info.Description);
ShellLink.SetHotkey(Info.HotKey);
ShellLink.SetIconLocation(Info.IconLocation, Info.IconIndex);
ShellLink.SetIDList(Info.ItemIDList);
ShellLink.SetPath(Info.FileName);
ShellLink.SetShowCmd(Info.ShowState);
ShellLink.SetRelativePath(Info.RelativePath, 0);
ShellLink.SetWorkingDirectory(Info.WorkDirectory);
if Succeeded(ShellLink.Resolve(0, SLR_UPDATE)) then Result := true;
end else
begin
ShellLink.GetPath(Info.FileName, MAX_PATH, Win32FindData, SLGP_SHORTPATH );
ShellLink.GetIconLocation(Info.IconLocation, MAX_PATH, Info.IconIndex);
ShellLink.GetWorkingDirectory(Info.WorkDirectory, MAX_PATH);
ShellLink.GetDescription(Info.Description, 255);
ShellLink.GetArguments(Info.Arguments, MAX_PATH);
ShellLink.GetHotkey(Info.HotKey);
ShellLink.GetIDList(Info.ItemIDList);
ShellLink.GetShowCmd(Info.ShowState);
Result:=true;
end;
end;//01
end;//02
end;//03
end;//04
finally//try
FreeMem(Buffer);
end;//endtry
end;//==============================================================================
//创建一个快捷方式**************************************************************
//==============================================================================
function CreateLinkFile(const Info: LINK_FILE_INFO; const TargetFileName: string=''): boolean;
var OBJ: IUnknown;
ShellLink: IShellLink;
PersistFile: IPersistFile;
wFileName: WideString;
begin
wFileName := TargetFileName;
OBJ := CreateComObject(CLSID_SHELLLINK);
ShellLink := OBJ as IShellLink;
PersistFile := OBJ as IPersistFile;
ShellLink.SetPath(Info.FileName);
ShellLink.SetWorkingDirectory(Info.WorkDirectory);
ShellLink.SetDescription(Info.Description);
ShellLink.SetArguments(Info.Arguments);
ShellLink.SetIconLocation(Info.IconLocation, Info.IconIndex);
//ShellLink.SetIDList(Info.ItemIDList);
ShellLink.SetHotkey(Info.HotKey);
ShellLink.SetShowCmd(Info.ShowState);
ShellLink.SetRelativePath(Info.RelativePath,0);
if TargetFileName='' then wFileName:=ChangeFileExt(Info.FileName,'lnk');
Result := Succeeded(PersistFile.Save(PWChar(wFileName), false));
end;
//获得或设置快捷方式文件******************************************************
//==============================================================================
function LinkFileInfo(const LinkFileName: string; var Info: LINK_FILE_INFO; const bSet: boolean=false): boolean;
var HResu: HResult;
Buffer: PWideChar;
ShellLink: IShelllink;
PersistFile: IPersistFile;
Win32FindData: WIN32_FIND_DATA;
begin
Result := false;
GetMem(Buffer, MAX_PATH);
try
if Succeeded(CoInitialize(nil))
then if (Succeeded(coCreateInstance(CLSID_SHELLLINK, nil, CLSCTX_INPROC_SERVER, IID_IShellLinkA, ShellLink)))
then begin//04
HResu := ShellLink.QueryInterface(iPersistFile, PersistFile);
if Succeeded(HResu)
then begin//03
HResu := PersistFile.Load(StringToWideChar(LinkFileName, Buffer, MAX_PATH), STGM_READ);
if Succeeded(HResu)
then begin//02
HResu := ShellLink.Resolve(0, SLR_NO_UI);
if Succeeded(HResu)
then begin//01
if bSet
then begin
ShellLink.SetArguments(Info.Arguments);
ShellLink.SetDescription(Info.Description);
ShellLink.SetHotkey(Info.HotKey);
ShellLink.SetIconLocation(Info.IconLocation, Info.IconIndex);
ShellLink.SetIDList(Info.ItemIDList);
ShellLink.SetPath(Info.FileName);
ShellLink.SetShowCmd(Info.ShowState);
ShellLink.SetRelativePath(Info.RelativePath, 0);
ShellLink.SetWorkingDirectory(Info.WorkDirectory);
if Succeeded(ShellLink.Resolve(0, SLR_UPDATE)) then Result := true;
end else
begin
ShellLink.GetPath(Info.FileName, MAX_PATH, Win32FindData, SLGP_SHORTPATH );
ShellLink.GetIconLocation(Info.IconLocation, MAX_PATH, Info.IconIndex);
ShellLink.GetWorkingDirectory(Info.WorkDirectory, MAX_PATH);
ShellLink.GetDescription(Info.Description, 255);
ShellLink.GetArguments(Info.Arguments, MAX_PATH);
ShellLink.GetHotkey(Info.HotKey);
ShellLink.GetIDList(Info.ItemIDList);
ShellLink.GetShowCmd(Info.ShowState);
Result:=true;
end;
end;//01
end;//02
end;//03
end;//04
finally//try
FreeMem(Buffer);
end;//endtry
end;//==============================================================================
//创建一个快捷方式**************************************************************
//==============================================================================
function CreateLinkFile(const Info: LINK_FILE_INFO; const TargetFileName: string=''): boolean;
var OBJ: IUnknown;
ShellLink: IShellLink;
PersistFile: IPersistFile;
wFileName: WideString;
begin
wFileName := TargetFileName;
OBJ := CreateComObject(CLSID_SHELLLINK);
ShellLink := OBJ as IShellLink;
PersistFile := OBJ as IPersistFile;
ShellLink.SetPath(Info.FileName);
ShellLink.SetWorkingDirectory(Info.WorkDirectory);
ShellLink.SetDescription(Info.Description);
ShellLink.SetArguments(Info.Arguments);
ShellLink.SetIconLocation(Info.IconLocation, Info.IconIndex);
//ShellLink.SetIDList(Info.ItemIDList);
ShellLink.SetHotkey(Info.HotKey);
ShellLink.SetShowCmd(Info.ShowState);
ShellLink.SetRelativePath(Info.RelativePath,0);
if TargetFileName='' then wFileName:=ChangeFileExt(Info.FileName,'lnk');
Result := Succeeded(PersistFile.Save(PWChar(wFileName), false));
end;
有一本书上有:《windows核心编程》