sysuintf.inc 3.7 KB

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