我使用windows shell来枚举控制面板中的项目,并在备忘录中添加项目名称,在最终版本IShellFolder
和PITEMIDLIST
中但是会收到错误。
代码:
procedure TForm1.Button1Click(Sender: TObject);
var
psfDeskTop : IShellFolder;
psfControl : IShellFolder;
pidControl : PITEMIDLIST;
pidChild : PITEMIDLIST;
pidAbsolute : PItemIdList;
pEnumList : IEnumIDList;
celtFetched : ULONG;
FileInfo : SHFILEINFOW;
begin
OleCheck(SHGetDesktopFolder(psfDeskTop));
OleCheck(SHGetSpecialFolderLocation(0, CSIDL_CONTROLS, pidControl));
OleCheck(psfDeskTop.BindToObject(pidControl, nil, IID_IShellFolder,
psfControl));
OleCheck(psfControl.EnumObjects(0, SHCONTF_NONFOLDERS or SHCONTF_INCLUDEHIDDEN
or SHCONTF_FOLDERS, pEnumList));
while pEnumList.Next(1, pidChild, celtFetched) = 0 do
begin
pidAbsolute := ILCombine(pidControl, pidChild);
SHGetFileInfo(LPCTSTR(pidAbsolute), 0, FileInfo, SizeOf(FileInfo),
SHGFI_PIDL or SHGFI_DISPLAYNAME);
Memo1.Lines.Add(FileInfo.szDisplayName);
end;
//OK
psfDeskTop._Release;
//Error
psfControl._Release;
//Error
//pEnumList._Release;
CoTaskMemFree(pidControl);
CoTaskMemFree(pidChild);
CoTaskMemFree(pidAbsolute);
end;
为什么发布psfDeskTop
可以,但在发布psfControl and pEnumList
时会出错?有没有其他方法来释放它们?或者在我发布之前我需要一些判断吗?
答案 0 :(得分:4)
接口对象由内部引用计数器释放,无需释放它们(您不得释放它们)。