sysuintf.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. begin
  20. Result := (Instance<>nil) and (Instance.QueryInterface(IObjectInstance,Obj)=S_OK) and (TObject(Obj).InheritsFrom(AClass));
  21. end;
  22. function Supports(const Instance: IInterface; const IID: TGUID; out Intf): Boolean;
  23. begin
  24. Result:=(Instance<>nil) and (Instance.QueryInterface(IID,Intf)=S_OK);
  25. end;
  26. function Supports(const Instance: TObject; const IID: TGUID; out Intf): Boolean;
  27. var
  28. Temp: Pointer; // weak
  29. begin
  30. Result:=(Instance<>nil) and ((Instance.GetInterfaceWeak(IInterface,Temp) and (IInterface(Temp).QueryInterface(IID,Intf)=S_OK))
  31. or Instance.GetInterface(IID,Intf));
  32. { Some applications expect that the QueryInterface method is invoked as first priority
  33. to query for an interface and GetInterface as 2nd priority }
  34. end;
  35. function Supports(const Instance: TObject; const IID: Shortstring; out Intf): Boolean;
  36. begin
  37. Result:=(Instance<>nil) and Instance.GetInterface(IID,Intf);
  38. end;
  39. function Supports(const Instance: IInterface; const AClass: TClass): Boolean;
  40. var
  41. Temp: TObject;
  42. begin
  43. Result:=Supports(Instance,AClass,Temp);
  44. end;
  45. function Supports(const Instance: IInterface; const IID: TGUID): Boolean;
  46. var
  47. Temp: IInterface;
  48. begin
  49. Result:=Supports(Instance,IID,Temp);
  50. end;
  51. function Supports(const Instance: TObject; const IID: TGUID): Boolean;
  52. var
  53. Temp: IInterface;
  54. begin
  55. Result:=Supports(Instance,IID,Temp);
  56. end;
  57. function Supports(const Instance: TObject; const IID: Shortstring): Boolean;
  58. begin
  59. Result:=(Instance<>nil) and (Instance.GetInterfaceEntryByStr(IID)<>nil);
  60. end;
  61. function Supports(const AClass: TClass; const IID: TGUID): Boolean;
  62. begin
  63. Result:=(AClass<>nil) and (AClass.GetInterfaceEntry(IID)<>nil);
  64. end;
  65. function Supports(const AClass: TClass; const IID: Shortstring): Boolean;
  66. begin
  67. Result:=(AClass<>nil) and (AClass.GetInterfaceEntryByStr(IID)<>nil);
  68. end;
  69. function StringToGUID(const S: string): TGUID;
  70. begin
  71. if not TryStringToGUID(S, Result) then
  72. raise EConvertError.CreateFmt(SInvalidGUID, [S]);
  73. end;
  74. function TryStringToGUID(const S: string; out Guid: TGUID): Boolean;
  75. var
  76. e: Boolean;
  77. p: PChar;
  78. function rb: Byte;
  79. begin
  80. case p^ of
  81. '0'..'9': Result := Byte(p^) - Byte('0');
  82. 'a'..'f': Result := Byte(p^) - Byte('a') + 10;
  83. 'A'..'F': Result := Byte(p^) - Byte('A') + 10;
  84. else e := False;
  85. end;
  86. Inc(p);
  87. end;
  88. procedure nextChar(c: Char); inline;
  89. begin
  90. if p^ <> c then
  91. e := False;
  92. Inc(p);
  93. end;
  94. begin
  95. if Length(S)<>38 then Exit(False);
  96. e := True;
  97. p := PChar(S);
  98. nextChar('{');
  99. 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;
  100. nextChar('-');
  101. Guid.D2 := rb shl 12 or rb shl 8 or rb shl 4 or rb;
  102. nextChar('-');
  103. Guid.D3 := rb shl 12 or rb shl 8 or rb shl 4 or rb;
  104. nextChar('-');
  105. Guid.D4[0] := rb shl 4 or rb;
  106. Guid.D4[1] := rb shl 4 or rb;
  107. nextChar('-');
  108. Guid.D4[2] := rb shl 4 or rb;
  109. Guid.D4[3] := rb shl 4 or rb;
  110. Guid.D4[4] := rb shl 4 or rb;
  111. Guid.D4[5] := rb shl 4 or rb;
  112. Guid.D4[6] := rb shl 4 or rb;
  113. Guid.D4[7] := rb shl 4 or rb;
  114. nextChar('}');
  115. Result := e;
  116. end;
  117. function IsEqualGUID(const guid1, guid2: TGUID): Boolean;
  118. var
  119. a1,a2: PIntegerArray;
  120. begin
  121. a1:=PIntegerArray(@guid1);
  122. a2:=PIntegerArray(@guid2);
  123. Result:=(a1^[0]=a2^[0]) and
  124. (a1^[1]=a2^[1]) and
  125. (a1^[2]=a2^[2]) and
  126. (a1^[3]=a2^[3]);
  127. end;
  128. function GuidCase(const GUID: TGUID; const List: array of TGuid): Integer;
  129. begin
  130. for Result := High(List) downto 0 do
  131. if IsEqualGUID(GUID, List[Result]) then
  132. Exit;
  133. Result := -1;
  134. end;
  135. function GUIDToString(const GUID: TGUID): string;
  136. begin
  137. SetLength(Result, 38);
  138. StrLFmt(PChar(Result), 38,'{%.8x-%.4x-%.4x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x}',
  139. [
  140. GUID.D1, GUID.D2, GUID.D3,
  141. GUID.D4[0], GUID.D4[1], GUID.D4[2], GUID.D4[3],
  142. GUID.D4[4], GUID.D4[5], GUID.D4[6], GUID.D4[7]
  143. ]);
  144. end;