//***************************************************************************************************************************************************************
//*                                                                                                                                                             *
//*     3delite's PIDLString functions 1.0 © 3delite 2016                                                                                                       *
//*                                                                                                                                                             *
//* 3delite's PIDLString functions is free (a mention in the credits would be nice if you use it!).                                                             *
//*                                                                                                                                                             *
//* For other Delphi components please visit the home page:                                                                                                     *
//*                                                                                                                                                             *
//*     http://www.3delite.hu/                                                                                                                                  *
//*                                                                                                                                                             *
//* If you have any of the aforementioned please email: 3delite@3delite.hu                                                                                      *
//*                                                                                                                                                             *
//***************************************************************************************************************************************************************

unit PIDLString;

interface

Uses
    SysUtils,
    ShlObj;

    function StringToPIDL(PIDLStr: String): PItemIDList;
    function PIDLToString(APIDL: PItemIDList): String;

implementation

Uses
    ActiveX;

var
    FMalloc: IMalloc;

function StringToPIDL(PIDLStr: String): PItemIDList;
var
    P: PByte;
    i: Integer;
    ByteValue: Byte;
    ByteChars: String;
    CharCounter: Integer;
begin
    Result := nil;
    if PIDLStr = '' then begin
        Exit;
    end;
    Result := FMalloc.Alloc(Length(PIDLStr) div 2);
    P := PByte(Result);
    CharCounter := 1;
    for i := 1 to Length(PIDLStr) div 2 do begin
        ByteChars := '$' + Copy(PIDLStr, CharCounter, 2);
        ByteValue := StrToInt(ByteChars);
        P^ := ByteValue;
        Inc(P);
        Inc(CharCounter, 2);
    end;
end;

function PIDLToString(APIDL: PItemIDList): String;
var
    Size: integer;
    P: PByte;
    i: Integer;
    ByteChars: String;
    StringCounter: Integer;
begin
    Result := '';
    if APIDL = nil then begin
        Exit;
    end;
    Size := ILGetSize(APIDL);
    SetLength(Result, Size * 2);
    StringCounter := 1;
    P := PByte(APIDL);
    for i := 1 to Size do begin
        ByteChars := IntToHex(P^, 2);
        Inc(P);
        Result[StringCounter] := ByteChars[1];
        Result[StringCounter + 1] := ByteChars[2];
        Inc(StringCounter, 2);
    end;
end;

Initialization;

    SHGetMalloc(FMalloc);

end.
