sysuintf.inc 4.2 KB

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