sysuintf.inc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. {
  2. *********************************************************************
  3. Copyright (C) 2002 Free Pascal Development Team
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. *********************************************************************
  16. System Utilities For Free Pascal
  17. }
  18. function Supports(const Instance: IInterface; const AClass: TClass; out Obj): Boolean;
  19. var
  20. Getter: IImplementorGetter;
  21. begin
  22. if (Instance<>nil) and (Instance.QueryInterface(IImplementorGetter,Getter)=S_OK) then
  23. begin
  24. TObject(Obj) := Getter.GetObject;
  25. Result := Assigned(TObject(Obj)) and (TObject(Obj).InheritsFrom(AClass));
  26. end else
  27. Result := False;
  28. end;
  29. function Supports(const Instance: IInterface; const IID: TGUID; out Intf): Boolean;
  30. begin
  31. Result:=(Instance<>nil) and (Instance.QueryInterface(IID,Intf)=S_OK);
  32. end;
  33. function Supports(const Instance: TObject; const IID: TGUID; out Intf): Boolean;
  34. var
  35. Temp: Pointer; // weak
  36. begin
  37. Result:=(Instance<>nil) and ((Instance.GetInterfaceWeak(IInterface,Temp) and (IInterface(Temp).QueryInterface(IID,Intf)=S_OK))
  38. or Instance.GetInterface(IID,Intf));
  39. { Some applications expect that the QueryInterface method is invoked as first priority
  40. to query for an interface and GetInterface as 2nd priority }
  41. end;
  42. function Supports(const Instance: TObject; const IID: Shortstring; out Intf): Boolean;
  43. begin
  44. Result:=(Instance<>nil) and Instance.GetInterface(IID,Intf);
  45. end;
  46. function Supports(const Instance: IInterface; const AClass: TClass): Boolean;
  47. var
  48. Temp: TObject;
  49. begin
  50. Result:=Supports(Instance,AClass,Temp);
  51. end;
  52. function Supports(const Instance: IInterface; const IID: TGUID): Boolean;
  53. var
  54. Temp: IInterface;
  55. begin
  56. Result:=Supports(Instance,IID,Temp);
  57. end;
  58. function Supports(const Instance: TObject; const IID: TGUID): Boolean;
  59. var
  60. Temp: IInterface;
  61. begin
  62. Result:=Supports(Instance,IID,Temp);
  63. end;
  64. function Supports(const Instance: TObject; const IID: Shortstring): Boolean;
  65. begin
  66. Result:=(Instance<>nil) and (Instance.GetInterfaceEntryByStr(IID)<>nil);
  67. end;
  68. function Supports(const AClass: TClass; const IID: TGUID): Boolean;
  69. begin
  70. Result:=(AClass<>nil) and (AClass.GetInterfaceEntry(IID)<>nil);
  71. end;
  72. function Supports(const AClass: TClass; const IID: Shortstring): Boolean;
  73. begin
  74. Result:=(AClass<>nil) and (AClass.GetInterfaceEntryByStr(IID)<>nil);
  75. end;
  76. function StringToGUID(const S: string): TGUID;
  77. begin
  78. if not TryStringToGUID(S, Result) then
  79. raise EConvertError.CreateFmt(SInvalidGUID, [S]);
  80. end;
  81. function TryStringToGUID(const S: string; out Guid: TGUID): Boolean;
  82. var
  83. e: Boolean;
  84. p: PChar;
  85. function rb: Byte;
  86. begin
  87. case p^ of
  88. '0'..'9': Result := Byte(p^) - Byte('0');
  89. 'a'..'f': Result := Byte(p^) - Byte('a') + 10;
  90. 'A'..'F': Result := Byte(p^) - Byte('A') + 10;
  91. else e := False;
  92. end;
  93. Inc(p);
  94. end;
  95. procedure nextChar(c: Char); inline;
  96. begin
  97. if p^ <> c then
  98. e := False;
  99. Inc(p);
  100. end;
  101. begin
  102. if Length(S)<>38 then Exit(False);
  103. e := True;
  104. p := PChar(S);
  105. nextChar('{');
  106. Guid.D1 := rb shl 28 or rb shl 24 or rb shl 20 or rb shl 16 or rb shl 12 or rb shl 8 or rb shl 4 or rb;
  107. nextChar('-');
  108. Guid.D2 := rb shl 12 or rb shl 8 or rb shl 4 or rb;
  109. nextChar('-');
  110. Guid.D3 := rb shl 12 or rb shl 8 or rb shl 4 or rb;
  111. nextChar('-');
  112. Guid.D4[0] := rb shl 4 or rb;
  113. Guid.D4[1] := rb shl 4 or rb;
  114. nextChar('-');
  115. Guid.D4[2] := rb shl 4 or rb;
  116. Guid.D4[3] := rb shl 4 or rb;
  117. Guid.D4[4] := rb shl 4 or rb;
  118. Guid.D4[5] := rb shl 4 or rb;
  119. Guid.D4[6] := rb shl 4 or rb;
  120. Guid.D4[7] := rb shl 4 or rb;
  121. nextChar('}');
  122. Result := e;
  123. end;
  124. function IsEqualGUID(const guid1, guid2: TGUID): Boolean;
  125. var
  126. a1,a2: PIntegerArray;
  127. begin
  128. a1:=PIntegerArray(@guid1);
  129. a2:=PIntegerArray(@guid2);
  130. Result:=(a1^[0]=a2^[0]) and
  131. (a1^[1]=a2^[1]) and
  132. (a1^[2]=a2^[2]) and
  133. (a1^[3]=a2^[3]);
  134. end;
  135. function GuidCase(const GUID: TGUID; const List: array of TGuid): Integer;
  136. begin
  137. for Result := High(List) downto 0 do
  138. if IsEqualGUID(GUID, List[Result]) then
  139. Exit;
  140. Result := -1;
  141. end;
  142. function GUIDToString(const GUID: TGUID): string;
  143. begin
  144. SetLength(Result, 38);
  145. StrLFmt(PChar(Result), 38,'{%.8x-%.4x-%.4x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x}',
  146. [
  147. GUID.D1, GUID.D2, GUID.D3,
  148. GUID.D4[0], GUID.D4[1], GUID.D4[2], GUID.D4[3],
  149. GUID.D4[4], GUID.D4[5], GUID.D4[6], GUID.D4[7]
  150. ]);
  151. end;