We help IT Professionals succeed at work.

Get filename string from a shell item identifier list

seabear
seabear asked
on
I am attempting some very basic shell programming. Could someone please tell me the following.
1.  What exactly is an item identifier list?
2.  If one of the shell API functions returns an item identifier list how can I extract a file name and or path from it ?

I have tried the following test code from the Delphi 6 shell controls demos but it does not work the way I am using it

procedure TForm1.Button2Click(Sender: TObject);
var
    somePIDL: PItemIDList;
    StrRet: TStrRet;
begin
    SHGetSpecialFolderLocation(0, CSIDL_PERSONAL, somePIDL);
    FillChar(StrRet, SizeOf(StrRet), 0);
    caption := StrRetToString(somePIDL, StrRet);
end;

function StrRetToString(PIDL: PItemIDList; StrRet: TStrRet; Flag:string=''): string;
var
  P: PChar;
begin
  case StrRet.uType of
    STRRET_CSTR:
      SetString(Result, StrRet.cStr, lStrLen(StrRet.cStr));
    STRRET_OFFSET:
      begin
        P := @PIDL.mkid.abID[StrRet.uOffset - SizeOf(PIDL.mkid.cb)];
        SetString(Result, P, PIDL.mkid.cb - StrRet.uOffset);
      end;
    STRRET_WSTR:
      if Assigned(StrRet.pOleStr) then
        Result := StrRet.pOleStr
      else
        Result := '';  
  end;
  { This is a hack bug fix to get around Windows Shell Controls returning
    spurious "?"s in date/time detail fields }
  if (Length(Result) > 1) and (Result[1] = '?') and (Result[2] in ['0'..'9']) then
    Result := StringReplace(Result,'?','',[rfReplaceAll]);
end;


Comment
Watch Question

CERTIFIED EXPERT
Commented:
hello seabear, here is some info from the API help

Because there are many kinds of folders and file objects, each folder is a OLE component object model (COM) object that "knows" how to enumerate its contents and carry out other actions. More precisely, each folder implements the IShellFolder interface.

Objects in the shell's namespace are assigned item identifiers and item identifier lists. An item identifier uniquely identifies an item within its parent folder. An item identifier list uniquely identifies an item within the shell's namespace by tracing a path to the item from the desktop. A pointer to an item identifier list, which is sometimes called a PIDL (pronounced piddle), is used with many functions. . . .
Item identifiers and PIDLs are much like the filenames and paths used in a file system. However, they share this important difference: item identifiers and PIDLs are binary data structures that never appear to the user. Item names that can be shown to the user (called display names) are described in Display Names and Filenames.

An item identifier is defined by the variable-length SHITEMID structure. The first two bytes of this structure specify its size, and the format of the remaining bytes depends on the parent folder, or more precisely on the software that implements the parent folder's IShellFolder interface. Except for the first two bytes, item identifiers are not strictly defined, and applications should make no assumptions about their format. To determine whether two item identifiers are equal, an application can use the IShellFolder::CompareIDs member function.

The ITEMIDLIST structure defines an element in an item identifier list (the only member of this structure is an SHITEMID structure). An item identifier list consists of one or more consecutive ITEMIDLIST structures packed on byte boundaries, followed by a 16-bit zero value. An application can walk a list of item identifiers by examining the size specified in each SHITEMID structure and stopping when it finds a size of zero.
Item identifier lists are almost always allocated using the shell's allocator (an IMalloc interface that you can retrieve by using the SHGetMalloc function). For example, some shell functions create an item identifier list and return a PIDL to it. In such cases, it is usually the application's responsibility to free the PIDL using the shell's allocator. Note that the SHGetMalloc function retrieves the task allocator for OLE applications.
 - - - - - - - - - - - - - - - -

here is code I use to get Special Folders

var
SpecialDir : PItemIdList;
FBuf : array[0..MAX_PATH] of Char;
Dir1: String;
Allocator : IMalloc;


if SHGetMalloc(Allocator) = NOERROR then
begin
SHGetSpecialFolderLocation(theHandle, CSIDL_PROGRAMS, SpecialDir);
SHGetPathFromIDList(SpecialDir, @FBuf[0] );
Dir2 := String(FBuf);
Allocator.Free(SpecialDir);
Allocator._Release;
end;

hope this works for you

Commented:
Hi Slick812, my congrats! I seldomly see code which is correctly freeing the pidl returned by SHGetSpecialFolderLocation...   :-)

Commented:
Uah! Sorry, I have to take back the congrats. You're freeing too much! Calling "Allocator._Release" is a no go. Beginning with Delphi3 Delphi automatically releases interfaces for you. If you call _Release manually you confuse the reference counting, which usually ends up in exceptions. Please never call _Release manually!
CERTIFIED EXPERT

Commented:
Madshi, yea, I put _Release in, then left it out, and put it in again, sometimes it is ok to release something even if it is already released. . . I was trying to folow the API exapmples in C . . . I never got any exceptions though .. . Thanks for the input

revision

var
SpecialDir : PItemIdList;
FBuf : array[0..MAX_PATH] of Char;
Dir1: String;
Allocator : IMalloc;


if SHGetMalloc(Allocator) = NOERROR then
begin
SHGetSpecialFolderLocation(theHandle, CSIDL_PROGRAMS, SpecialDir);
SHGetPathFromIDList(SpecialDir, @FBuf[0] );
Dir2 := String(FBuf);
Allocator.Free(SpecialDir);
end;

Commented:
Yeah, the C people have to call Release manually, because C is not as intelligent as Delphi...  :-)  But I think C++ (at least MSVC++) has something like "smart pointers" or whatever it was called, which works similar to Delphi's interface handling.

Author

Commented:
Thanks Slick812 and Madshi - the code is working fine and i appreciate the explanation - Dan

Author

Commented:
Thanks Slick812 for the answer and to Madshi for comments - Dan