2
0

sysuintf.inc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. function HexChar(c: Char): Byte;
  59. begin
  60. case c of
  61. '0'..'9':
  62. Result:=Byte(c) - Byte('0');
  63. 'a'..'f':
  64. Result:=(Byte(c) - Byte('a')) + 10;
  65. 'A'..'F':
  66. Result:=(Byte(c) - Byte('A')) + 10;
  67. else
  68. raise EConvertError.CreateFmt(SInvalidGUID, [s]);
  69. Result:=0;
  70. end;
  71. end;
  72. function HexByte(p: PChar): Byte;
  73. begin
  74. Result:=(HexChar(p[0]) shl 4) + HexChar(p[1]);
  75. end;
  76. var
  77. i: integer;
  78. src: PChar;
  79. dest: PByte;
  80. begin
  81. if ((Length(S)<>38) or (s[1]<>'{')) then
  82. Exit(False);
  83. dest:=PByte(@Guid);
  84. src:=PChar(s);
  85. inc(src);
  86. for i:=0 to 3 do
  87. dest[i]:=HexByte(src+(3-i)*2);
  88. inc(src, 8);
  89. inc(dest, 4);
  90. if src[0]<>'-' then
  91. Exit(False);
  92. inc(src);
  93. for i:=0 to 1 do
  94. begin
  95. dest^:=HexByte(src+2);
  96. inc(dest);
  97. dest^:=HexByte(src);
  98. inc(dest);
  99. inc(src, 4);
  100. if src[0]<>'-' then
  101. Exit(False);
  102. inc(src);
  103. end;
  104. dest^:=HexByte(src);
  105. inc(dest);
  106. inc(src, 2);
  107. dest^:=HexByte(src);
  108. inc(dest);
  109. inc(src, 2);
  110. if src[0]<>'-' then
  111. Exit(False);
  112. inc(src);
  113. for i:=0 to 5 do
  114. begin
  115. dest^:=HexByte(src);
  116. inc(dest);
  117. inc(src, 2);
  118. end;
  119. Result := True;
  120. end;
  121. function IsEqualGUID(const guid1, guid2: TGUID): Boolean;
  122. var
  123. a1,a2: PIntegerArray;
  124. begin
  125. a1:=PIntegerArray(@guid1);
  126. a2:=PIntegerArray(@guid2);
  127. Result:=(a1^[0]=a2^[0]) and
  128. (a1^[1]=a2^[1]) and
  129. (a1^[2]=a2^[2]) and
  130. (a1^[3]=a2^[3]);
  131. end;
  132. function GuidCase(const GUID: TGUID; const List: array of TGuid): Integer;
  133. begin
  134. for Result := High(List) downto 0 do
  135. if IsEqualGUID(GUID, List[Result]) then
  136. Exit;
  137. Result := -1;
  138. end;
  139. function GUIDToString(const GUID: TGUID): string;
  140. begin
  141. SetLength(Result, 38);
  142. StrLFmt(PChar(Result), 38,'{%.8x-%.4x-%.4x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x}',
  143. [
  144. GUID.D1, GUID.D2, GUID.D3,
  145. GUID.D4[0], GUID.D4[1], GUID.D4[2], GUID.D4[3],
  146. GUID.D4[4], GUID.D4[5], GUID.D4[6], GUID.D4[7]
  147. ]);
  148. end;