File: VirtualPIDLTools.pas

package info (click to toggle)
mysql-query-browser 1.1.6-1sarge0
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 36,320 kB
  • ctags: 24,680
  • sloc: pascal: 203,479; xml: 136,561; ansic: 47,502; cpp: 28,926; sh: 12,433; objc: 4,823; java: 1,849; php: 1,485; python: 1,225; sql: 1,128; makefile: 872
file content (824 lines) | stat: -rw-r--r-- 23,607 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
unit VirtualPIDLTools;

// Version 1.2.0
//   The contents of this file are subject to the Mozilla Public License
// Version 1.1 (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the
// License at
//
// http://www.mozilla.org/MPL/
//
//   Software distributed under the License is distributed on an
// " AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either expressed or
// implied. See the License for the specific language governing rights
// and limitations under the License.
//
//
//   Alternatively, the contents of this file may be used under
// the terms of the GNU General Public License Version 2 or later
// (the "GPL"), in which case the provisions of the GPL are applicable
// instead of those above. If you wish to allow use of your version of
// this file only under the terms of the GPL and not to allow others to
// use your version of this file under the MPL, indicate your decision
// by deleting the provisions above and replace them with the notice and
// other provisions required by the GPL. If you do not delete the provisions
// above, a recipient may use your version of this file under either the
// MPL or the GPL.
//
// The initial developer of this code is Jim Kueneman <jimdk@mindspring.com>
//
//----------------------------------------------------------------------------
//
// This unit implements two classes to simplify the handling of PItemIDLists.  They
// are:
//      TPIDLManager - Exposes numerous methods to handle the details of
//                     manipulting and comparing PIDLs
//      TPIDLList    - A TList decendant that is tailored to store PIDLs
//
//  USAGE:
//    This unit is compiled with WeakPackaging because it is used in several
// different packages and Delphi does not allow this without WeakPackaging.
// A WeakPackaged unit can not have initialization or Finalization sections so
// due to this fact this unit must implement a "load on demand" for the undocumented
// PIDL functions.
//
// Jim Kueneman
// 2/20/02
// *****************************************************************************
//
// Software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
// either express or implied.
//
// *****************************************************************************

interface

uses
  Windows, Messages, SysUtils, Classes, Controls, ShlObj, ShellAPI,
  {$IFNDEF COMPILER_5_UP}
  VirtualUtilities,
  {$ENDIF COMPILER_5_UP}
  ActiveX,
  VirtualWideStrings;

type

  TPIDLManager = class;  // forward
  TPIDLList = class;     // forward

  TPIDLList = class(TList)
  private
    FSharePIDLs: Boolean;    // If true the class will not free the PIDL's automaticlly when destroyed
    FPIDLMgr: TPIDLManager;
    FDestroying: Boolean;  // Instance of a PIDLManager used to easily deal with the PIDL's
    function GetPIDL(Index: integer): PItemIDList;
    function GetPIDLMgr: TPIDLManager;

  protected
    property Destroying: Boolean read FDestroying;
    property PIDLMgr: TPIDLManager read GetPIDLMgr;
  public
    destructor Destroy; override;

    procedure Clear; override;
    procedure CloneList(PIDLList: TPIDLList);
    function CopyAdd(PIDL: PItemIDList): Integer;
    function FindPIDL(TestPIDL: PItemIDList): Integer;
    function LoadFromFile(FileName: WideString): Boolean;
    function LoadFromStream( Stream: TStream): Boolean; virtual;
    function SaveToFile(FileName: WideString): Boolean;
    function SaveToStream( Stream: TStream): Boolean; virtual;

    property SharePIDLs: Boolean read FSharePIDLs write FSharePIDLs;
  end;

// TPIDL Manager is a class the encapsulates PIDLs and makes them easier to
// handle.
  TPIDLManager = class
  private
  protected
    FMalloc: IMalloc;  // The global Memory allocator
  public
    constructor Create;
    destructor Destroy; override;

    function AllocStrGlobal(SourceStr: WideString): POleStr;
    function AppendPIDL(DestPIDL, SrcPIDL: PItemIDList): PItemIDList;
    function CopyPIDL(APIDL: PItemIDList): PItemIDList;
    function EqualPIDL(PIDL1, PIDL2: PItemIDList): Boolean;
    procedure FreeAndNilPIDL(var PIDL: PItemIDList);
    procedure FreeOLEStr(OLEStr: LPWSTR);
    procedure FreePIDL(PIDL: PItemIDList);
    function CopyLastID(IDList: PItemIDList): PItemIDList;
    function GetPointerToLastID(IDList: PItemIDList): PItemIDList;
    function IDCount(APIDL: PItemIDList): integer;
    function IsDesktopFolder(APIDL: PItemIDList): Boolean;
    function IsSubPIDL(FullPIDL, SubPIDL: PItemIDList): Boolean;
    function NextID(APIDL: PItemIDList): PItemIDList;
    function PIDLSize(APIDL: PItemIDList): integer;
    function PIDLToString(APIDL: PItemIDList): string;
    function LoadFromFile(FileName: WideString): PItemIDList;
    function LoadFromStream(Stream: TStream): PItemIDList;
    procedure ParsePIDL(AbsolutePIDL: PItemIDList; var PIDLList: TPIDLList; AllAbsolutePIDLs: Boolean);
    function StringToPIDL(PIDLStr: string): PItemIDList;
    function StripLastID(IDList: PItemIDList): PItemIDList; overload;
    function StripLastID(IDList: PItemIDList; var Last_CB: Word; var LastID: PItemIDList): PItemIDList; overload;
    procedure SaveToFile(FileName: WideString; FileMode: Word; PIDL: PItemIdList);
    procedure SaveToStream(Stream: TStream; PIDL: PItemIdList);

    property Malloc: IMalloc read FMalloc;
  end;

  function ILIsEqual(PIDL1: PItemIDList; PIDL2: PItemIDList): LongBool;
  function ILIsParent(PIDL1: PItemIDList; PIDL2: PItemIDList; ImmediateParent: LongBool): LongBool;

  function GetMyDocumentsVirtualFolder: PItemIDList;

// Don't use the following functions, they are only here because with Weakpackaging
// on we can't use initializaiton and finialzation sections.  Because of that we
// have to load the Shell 32 functions on demand, but if the package that uses
// this unit does not calls these functions the compiler gives us a "The unit
// does not use or export the function XXXX"  This keeps the compiler quiet.
type
  TShellILIsParent = function(PIDL1: PItemIDList; PIDL2: PItemIDList;
    ImmediateParent: LongBool): LongBool; stdcall;
  TShellILIsEqual = function(PIDL1: PItemIDList; PIDL2: PItemIDList): LongBool; stdcall;
  procedure LoadShell32Functions;

var
  ShellILIsParent: TShellILIsParent = nil;
  ShellILIsEqual: TShellILIsEqual = nil;

implementation

uses
  VirtualNamespace;

var
  PIDLManager: TPIDLManager;

// -----------------------------------------------------------------------------
procedure LoadShell32Functions;
var
  ShellDLL: HMODULE;
begin
  ShellDLL := GetModuleHandle(PChar(Shell32));
//  ShellDLL := LoadLibrary(PChar(Shell32));
  if ShellDll <> 0 then
  begin
    ShellILIsEqual := GetProcAddress(ShellDLL, PChar(21));
    ShellILIsParent := GetProcAddress(ShellDLL, PChar(23));
  end
end;
// -----------------------------------------------------------------------------

function ILIsEqual(PIDL1: PItemIDList; PIDL2: PItemIDList): LongBool;
// Wrapper around undocumented ILIsEqual function.  It can't take nil parameters
{$IFDEF VIRTUALNAMESPACES}
var
  OldPIDL: PItemIDList;
  OldCB: Word;
  Desktop, Folder: IShellFolder;
  IsVirtual1,
  IsVirtual2: Boolean;
{$ENDIF}
begin
  if not Assigned(ShellILIsEqual) then
    LoadShell32Functions;
  if Assigned(PIDL1) and Assigned(PIDL2) then
  {$IFDEF VIRTUALNAMESPACES}
  begin          
    Result := False;
    IsVirtual1 := NamespaceExtensionFactory.IsVirtualPIDL(PIDL1);
    IsVirtual2 := NamespaceExtensionFactory.IsVirtualPIDL(PIDL2);
    if (IsVirtual1 and IsVirtual2) then
    begin
      if PIDLManager.IDCount(PIDL1) = PIDLManager.IDCount(PIDL2) then
      begin
        Folder := NamespaceExtensionFactory.BindToVirtualParentObject(PIDL1);
        OldPIDL := PIDLManager.GetPointerToLastID(PIDL1);
        if Assigned(Folder) then
          Result := Folder.CompareIDs(0, OldPIDL, PIDLManager.GetPointerToLastID(PIDL2)) = 0
      end
    end else
    if not IsVirtual1 and not IsVirtual2 then
    begin
   //   Result := ShellILIsEqual(PIDL1, PIDL2)
      // Believe it or not MY routine is faster than the M$ version!!!!
      if PIDLManager.IDCount(PIDL1) = PIDLManager.IDCount(PIDL2) then
      begin
        if PIDLManager.IDCount(PIDL1) > 1 then
        begin
          PIDLManager.StripLastID(PIDL1, OldCB, OldPIDL);
          SHGetDesktopFolder(Desktop);
          if Succeeded(Desktop.BindToObject(PIDL1, nil, IID_IShellFolder, Folder)) then
          begin
            OldPIDL.mkid.cb := OldCB;
            OldPIDL := PIDLManager.GetPointerToLastID(PIDL1);
            Result := Folder.CompareIDs(0, OldPIDL, PIDLManager.GetPointerToLastID(PIDL2)) = 0
          end else
            OldPIDL.mkid.cb := OldCB;
        end else
        begin
          SHGetDesktopFolder(Folder);
          Result := Folder.CompareIDs(0, PIDL1, PIDL2) = 0
        end
      end
    end
  end
  {$ELSE}
    Result := ShellILIsEqual(PIDL1, PIDL2)
  {$ENDIF}
  else
   Result := False
end;
// -----------------------------------------------------------------------------

// -----------------------------------------------------------------------------
function ILIsParent(PIDL1: PItemIDList; PIDL2: PItemIDList; ImmediateParent: LongBool): LongBool;
// Wrapper around undocumented ILIsParent function.  It can't take nil parameters
{$IFDEF VIRTUALNAMESPACES}
const
  AllObjects = SHCONTF_FOLDERS or SHCONTF_NONFOLDERS or SHCONTF_INCLUDEHIDDEN;
var
  Folder: IShellFolder;
  TailPIDL1, TailPIDL2: PItemIDList;
  OldCB: Word;
  OldPIDL: PItemIDList;
  i, Count1, Count2: Integer;
{$ENDIF}
begin
  if not Assigned(ShellILIsParent) then
    LoadShell32Functions;
  if Assigned(PIDL1) and Assigned(PIDL2) then
  {$IFDEF VIRTUALNAMESPACES}
  begin
    Result := False;
    if NamespaceExtensionFactory.IsVirtualPIDL(PIDL1) or NamespaceExtensionFactory.IsVirtualPIDL(PIDL2) then
    begin
      Count1 := PIDLManager.IDCount(PIDL1);
      Count2 := PIDLManager.IDCount(PIDL2);
      // The Parent must have less PIDLs to be a parent!
      if Count1 < Count2 then
      begin
        // Desktop is a special case
        if (PIDL1.mkid.cb = 0) and (PIDL2.mkid.cb > 0) then
        begin
          if ImmediateParent then
          begin
            if PIDLManager.IDCount(PIDL2) = 1 then
              Result := True
          end else
            Result := True
        end else
        begin
          // If looking for the immediate parent and the second pidl is different
          // than one greater than it can't be true
          if ImmediateParent and (Count1 + 1 <> Count2) then
            Exit;


          Folder := NamespaceExtensionFactory.BindToVirtualParentObject(PIDL1);

          TailPIDL1 := PIDLManager.GetPointerToLastID(PIDL1);
          TailPIDL2 := PIDL2;
          for i := 0 to PIDLManager.IDCount(PIDL1) - 2 do
            TailPIDL2 := PIDLManager.NextID(TailPIDL2);
          OldPIDL := PIDLManager.NextID(TailPIDL2);
          OldCB := OldPIDL.mkid.cb;
          OldPIDL.mkid.cb := 0;
          try
            Result := Folder.CompareIDs(0, TailPIDL1, TailPIDL2) = 0;
          finally
            OldPIDL.mkid.cb := OldCB
          end;
        end
      end
    end else
     Result := ShellILIsParent(PIDL1, PIDL2, ImmediateParent)
  end
  {$ELSE}
    Result := ShellILIsParent(PIDL1, PIDL2, ImmediateParent)
  {$ENDIF}
  else
    Result := False
end;
// -----------------------------------------------------------------------------


function GetMyDocumentsVirtualFolder: PItemIDList;

const
  MYCOMPUTER_GUID = WideString('::{450d8fba-ad25-11d0-98a8-0800361b1103}');

var
  dwAttributes, pchEaten: ULONG;
  Desktop: IShellFolder;
begin
  Result := nil;
  dwAttributes := 0;
  SHGetDesktopFolder(Desktop);
  pchEaten := Length(MYCOMPUTER_GUID);
  if not Succeeded(Desktop.ParseDisplayName(0, nil,
    PWideChar(MYCOMPUTER_GUID), pchEaten, Result, dwAttributes))
  then
    Result := nil
end;



{ TPIDLList }

// -----------------------------------------------------------------------------
// Specialized TList that handles PIDLs
// -----------------------------------------------------------------------------

procedure TPIDLList.Clear;
var
  i: integer;
begin
  if (not Destroying) or Assigned(FPIDLMgr) then
  begin
    if not SharePIDLs and Assigned(PIDLMgr)then
      for i := 0 to Count - 1 do
        PIDLMgr.FreePIDL( PItemIDList( Items[i]));
  end;
  inherited;
end;

procedure TPIDLList.CloneList(PIDLList: TPIDLList);
var
  i: Integer;
begin
  if Assigned(PIDLList) then
    for i := 0 to Count - 1 do
      PIDLList.CopyAdd(Items[i])
end;

function TPIDLList.CopyAdd(PIDL: PItemIDList): integer;
// Adds a Copy of the passed PIDL to the list
begin
  Result := Add( PIDLMgr.CopyPIDL(PIDL));
end;

destructor TPIDLList.Destroy;
begin
  FDestroying := True;
  FreeAndNil(FPIDLMgr);
  inherited;
end;

function TPIDLList.FindPIDL(TestPIDL: PItemIDList): Integer;
// Finds the index of the PIDL that is equivalent to the passed PIDL.  This is not
// the same as an byte for byte equivalent comparison
var
  i: Integer;
begin
  i := 0;
  Result := -1;
  while (i < Count) and (Result < 0) do
  begin
    if PIDLMgr.EqualPIDL(TestPIDL, GetPIDL(i)) then
      Result := i;
    Inc(i);
  end;
end;

function TPIDLList.GetPIDL(Index: integer): PItemIDList;
begin
  Result := PItemIDList( Items[Index]);
end;

function TPIDLList.GetPIDLMgr: TPIDLManager;
begin
  if not Assigned(FPIDLMgr) then
    FPIDLMgr := TPIDLManager.Create;
  Result := FPIDLMgr
end;

function TPIDLList.LoadFromFile(FileName: WideString): Boolean;
// Loads the PIDL list from a file
var
  FileStream: TWideFileStream;
begin
  FileStream := nil;
  try
    try
      FileStream := TWideFileStream.Create(FileName, fmOpenRead or fmShareExclusive);
      Result := LoadFromStream(FileStream);
    except
      Result := False;
    end;
  finally
    FileStream.Free
  end;
end;

function TPIDLList.LoadFromStream(Stream: TStream): Boolean;
// Loads the PIDL list from a stream
var
  PIDLCount, i: integer;
begin
  Result := True;
  try
    Stream.ReadBuffer(PIDLCount, SizeOf(Integer));
    for i := 0 to PIDLCount - 1 do
      Add( PIDLMgr.LoadFromStream(Stream));
  except
    Result := False;
  end;
end;

function TPIDLList.SaveToFile(FileName: WideString): Boolean;
// Saves the PIDL list to a File
var
  FileStream: TWideFileStream;
begin
  FileStream := nil;
  try
    try
      FileStream := TWideFileStream.Create(FileName, fmCreate or fmShareExclusive);
      Result := SaveToStream(FileStream);
    except
      Result := False;
    end;
  finally
    FileStream.Free
  end;
end;

function TPIDLList.SaveToStream(Stream: TStream): Boolean;
// Saves the PIDL list to a stream
var
  i: integer;
begin
  Result := True;
  try
    Stream.WriteBuffer(Count, SizeOf(Count));
    for i := 0 to Count - 1 do
      PIDLMgr.SaveToStream(Stream, Items[i]);
  except
    Result := False;
  end;
end;

{ TPIDLManager }

// Routines to do most anything you would want to do with a PIDL

function TPIDLManager.AppendPIDL(DestPIDL, SrcPIDL: PItemIDList): PItemIDList;
// Returns the concatination of the two PIDLs. Neither passed PIDLs are
// freed so it is up to the caller to free them.
var
  DestPIDLSize, SrcPIDLSize: integer;
begin
  DestPIDLSize := 0;
  SrcPIDLSize := 0;
  // Appending a PIDL to the DesktopPIDL is invalid so don't allow it.
  if Assigned(DestPIDL) then
    if not IsDesktopFolder(DestPIDL) then
      DestPIDLSize := PIDLSize(DestPIDL) - SizeOf(DestPIDL^.mkid.cb);

  if Assigned(SrcPIDL) then
    SrcPIDLSize := PIDLSize(SrcPIDL);

  Result := FMalloc.Alloc(DestPIDLSize + SrcPIDLSize);
  if Assigned(Result) then
  begin
    if Assigned(DestPIDL) then
      CopyMemory(Result, DestPIDL, DestPIDLSize);
    if Assigned(SrcPIDL) then
      CopyMemory(Pchar(Result) + DestPIDLSize, SrcPIDL, SrcPIDLSize);
  end;
end;

function TPIDLManager.CopyPIDL(APIDL: PItemIDList): PItemIDList;
// Copies the PIDL and returns a newly allocated PIDL. It is not associated
// with any instance of TPIDLManager so it may be assigned to any instance.
var
  Size: integer;
begin
  if Assigned(APIDL) then
  begin
    Size := PIDLSize(APIDL);
    Result := FMalloc.Alloc(Size);
    if Result <> nil then
      CopyMemory(Result, APIDL, Size);
  end else
    Result := nil
end;

constructor TPIDLManager.Create;
begin
  inherited Create;
  if SHGetMalloc(FMalloc) = E_FAIL then
    fail
end;

destructor TPIDLManager.Destroy;
begin
  FMalloc := nil;
  inherited
end;

function TPIDLManager.EqualPIDL(PIDL1, PIDL2: PItemIDList): Boolean;
begin
  Result := Boolean( ILIsEqual(PIDL1, PIDL2))
end;

procedure TPIDLManager.FreeOLEStr(OLEStr: LPWSTR);
// Frees an OLE string created by the Shell; as in StrRet
begin
  FMalloc.Free(OLEStr)
end;

procedure TPIDLManager.FreePIDL(PIDL: PItemIDList);
// Frees the PIDL using the shell memory allocator
begin
  if Assigned(PIDL) then
    FMalloc.Free(PIDL)
end;

function TPIDLManager.CopyLastID(IDList: PItemIDList): PItemIDList;
// Returns a copy of the last PID in the list
var
  Count, i: integer;
  PIDIndex: PItemIDList;
begin
  PIDIndex := IDList;
  Count := IDCount(IDList);
  if Count > 1 then
    for i := 0 to Count - 2 do
     PIDIndex := NextID(PIDIndex);
  Result := CopyPIDL(PIDIndex);
end;

function TPIDLManager.GetPointerToLastID(IDList: PItemIDList): PItemIDList;
// Return a pointer to the last PIDL in the complex PIDL passed to it.
// Useful to overlap an Absolute complex PIDL with the single level
// Relative PIDL.
var
  Count, i: integer;
  PIDIndex: PItemIDList;
begin
  if Assigned(IDList) then
  begin
    PIDIndex := IDList;
    Count := IDCount(IDList);
    if Count > 1 then
      for i := 0 to Count - 2 do
       PIDIndex := NextID(PIDIndex);
    Result := PIDIndex;
  end else
    Result := nil
end;

function TPIDLManager.IDCount(APIDL: PItemIDList): integer;
// Counts the number of Simple PIDLs contained in a Complex PIDL.
var
  Next: PItemIDList;
begin
  Result := 0;
  Next := APIDL;
  if Assigned(Next) then
  begin
    while Next^.mkid.cb <> 0 do
    begin
      Inc(Result);
      Next := NextID(Next);
    end
  end
end;

function TPIDLManager.IsDesktopFolder(APIDL: PItemIDList): Boolean;
// Tests the passed PIDL to see if it is the root Desktop Folder
begin
  if Assigned(APIDL) then
    Result := APIDL.mkid.cb = 0
  else
    Result := False
end;

function TPIDLManager.NextID(APIDL: PItemIDList): PItemIDList;
// Returns a pointer to the next Simple PIDL in a Complex PIDL.
begin
  Result := APIDL;
  Inc(PChar(Result), APIDL^.mkid.cb);
end;

function TPIDLManager.PIDLSize(APIDL: PItemIDList): integer;
// Returns the total Memory in bytes the PIDL occupies.
begin
  Result := 0;
  if Assigned(APIDL) then
  begin
    Result := SizeOf( Word);  // add the null terminating last ItemID
    while APIDL.mkid.cb <> 0 do
    begin
      Result := Result + APIDL.mkid.cb;
      APIDL := NextID(APIDL);
    end;
  end;
end;

function TPIDLManager.PIDLToString(APIDL: PItemIDList): string;
var
  Size: integer;
  P: PChar;
begin
  Size := PIDLSize(APIDL);
  SetLength(Result, PIDLSize(APIDL));
  P := @Result[1];
  Move(APIDL^, P^, Size);
end;

function TPIDLManager.LoadFromFile(FileName: WideString): PItemIDList;
// Loads the PIDL from a File
var
  S: TWideFileStream;
begin
  S := TWideFileStream.Create(FileName, fmOpenRead);
  try
    Result := LoadFromStream(S);
  finally
    S.Free;
  end;
end;

function TPIDLManager.LoadFromStream(Stream: TStream): PItemIDList;
// Loads the PIDL from a Stream
var
  Size: integer;
begin
  Result := nil;
  if Assigned(Stream) then
  begin
    Stream.ReadBuffer(Size, SizeOf(Integer));
    if Size > 0 then
    begin
      Result := FMalloc.Alloc(Size);
      Stream.ReadBuffer(Result^, Size);
    end
  end
end;

function TPIDLManager.StringToPIDL(PIDLStr: string): PItemIDList;
var
  P: PChar;
begin
  Result := FMalloc.Alloc(Length(PIDLStr));
  P := @PIDLStr[1];
  Move(P^, Result^, Length(PIDLStr));
end;

function TPIDLManager.StripLastID(IDList: PItemIDList): PItemIDList;
// Removes the last PID from the list. Returns the same, shortened, IDList passed
// to the function
var
  MarkerID: PItemIDList;
begin
  Result := IDList;
  MarkerID := IDList;
  if Assigned(IDList) then
  begin
    while IDList.mkid.cb <> 0 do
    begin
      MarkerID := IDList;
      IDList := NextID(IDList);
    end;
    MarkerID.mkid.cb := 0;
  end;
end;

procedure TPIDLManager.SaveToFile(FileName: WideString; FileMode: Word;
  PIDL: PItemIdList);
// Saves the PIDL from a File
var
  S: TWideFileStream;
begin
  S := TWideFileStream.Create(FileName, FileMode);
  try
    SaveToStream(S, PIDL);
  finally
    S.Free;
  end;
end;

procedure TPIDLManager.SaveToStream(Stream: TStream; PIDL: PItemIdList);
// Saves the PIDL from a Stream
var
  Size: Integer;
begin
  Size := PIDLSize(PIDL);
  Stream.WriteBuffer(Size, SizeOf(Size));
  Stream.WriteBuffer(PIDL^, Size);
end;



function TPIDLManager.StripLastID(IDList: PItemIDList; var Last_CB: Word;
  var LastID: PItemIDList): PItemIDList;
// Strips the last ID but also returns the pointer to where the last CB was and the
// value that was there before setting it to 0 to shorten the PIDL.  All that is necessary
// is to do a LastID^ := Last_CB.mkid.cb to return the PIDL to its previous state.  Used to
// temporarily strip the last ID of a PIDL
var
  MarkerID: PItemIDList;
begin
  Last_CB := 0;
  LastID := nil;
  Result := IDList;
  MarkerID := IDList;
  if Assigned(IDList) then
  begin
    while IDList.mkid.cb <> 0 do
    begin
      MarkerID := IDList;
      IDList := NextID(IDList);
    end;
    Last_CB := MarkerID.mkid.cb;
    LastID := MarkerID;
    MarkerID.mkid.cb := 0;
  end;
end;

function TPIDLManager.IsSubPIDL(FullPIDL, SubPIDL: PItemIDList): Boolean;
// Tests to see if the SubPIDL can be expanded into the passed FullPIDL
var
  i, PIDLLen, SubPIDLLen: integer;
  PIDL: PItemIDList;
  OldCB: Word;
begin
  Result := False;
  if Assigned(FullPIDL) and Assigned(SubPIDL) then
  begin
    SubPIDLLen := IDCount(SubPIDL);
    PIDLLen := IDCount(FullPIDL);
    if SubPIDLLen <= PIDLLen then
    begin
      PIDL := FullPIDL;
      for i := 0 to SubPIDLLen - 1 do
        PIDL := NextID(PIDL);
      OldCB := PIDL.mkid.cb;
      PIDL.mkid.cb := 0;
      try
        Result := ILIsEqual(FullPIDL, SubPIDL);
      finally
        PIDL.mkid.cb := OldCB
      end
    end
  end
end;

procedure TPIDLManager.FreeAndNilPIDL(var PIDL: PItemIDList);
var
  OldPIDL: PItemIDList;
begin
  OldPIDL := PIDL;
  PIDL := nil;
  FreePIDL(OldPIDL)
end;

function TPIDLManager.AllocStrGlobal(SourceStr: WideString): POleStr;
begin
  Result := Malloc.Alloc((Length(SourceStr) + 1) * 2); // Add the null
  if Result <> nil then
    CopyMemory(Result, PWideChar(SourceStr), (Length(SourceStr) + 1) * 2);
end;

procedure TPIDLManager.ParsePIDL(AbsolutePIDL: PItemIDList; var PIDLList: TPIDLList;
  AllAbsolutePIDLs: Boolean);
// Parses the AbsolutePIDL in to its single level PIDLs, if AllAbsolutePIDLs is true
// then each item is not a single level PIDL but an AbsolutePIDL but walking from the
// Desktop up to the passed AbsolutePIDL
var
  OldCB: Word;
  Head, Tail: PItemIDList;
begin
  Head := AbsolutePIDL;
  Tail := Head;
  if Assigned(PIDLList) and Assigned(Head) then
  begin
    while Tail.mkid.cb <> 0 do
    begin
      Tail := NextID(Tail);
      OldCB := Tail.mkid.cb;
      try
        Tail.mkid.cb := 0;
        PIDLList.Add(CopyPIDL(Head));
      finally
        Tail.mkid.cb := OldCB;
      end;
      if not AllAbsolutePIDLs then
        Head := Tail
    end
  end
end;

initialization
  PIDLManager := TPIDLManager.Create;

finalization
  FreeAndNil(PIDLManager);

end.