sysuintf.inc 4.4 KB

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