2
0

intf.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. {
  2. *********************************************************************
  3. $Id$
  4. Copyright (C) 2002 Free Pascal Development Team
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. *********************************************************************
  17. System Utilities For Free Pascal
  18. }
  19. function Supports(const Instance: IInterface; const IID: TGUID; out Intf): Boolean;
  20. begin
  21. Result:=(Instance<>nil) and
  22. (Instance.QueryInterface(IID,Intf)=0);
  23. end;
  24. function Supports(const Instance: TObject; const IID: TGUID; out Intf): Boolean;
  25. var
  26. LUnknown: IUnknown;
  27. begin
  28. Result:=(Instance<>nil) and
  29. ((Instance.GetInterface(IUnknown,LUnknown) and
  30. Supports(LUnknown,IID,Intf)) or
  31. Instance.GetInterface(IID,Intf));
  32. end;
  33. function Supports(const Instance: IInterface; const IID: TGUID): Boolean;
  34. var
  35. Temp: IInterface;
  36. begin
  37. Result:=Supports(Instance,IID,Temp);
  38. end;
  39. function Supports(const Instance: TObject; const IID: TGUID): Boolean;
  40. var
  41. Temp: IInterface;
  42. begin
  43. Result:=Supports(Instance,IID,Temp);
  44. end;
  45. function Supports(const AClass: TClass; const IID: TGUID): Boolean;
  46. begin
  47. Result:=AClass.GetInterfaceEntry(IID)<>nil;
  48. end;
  49. function StringToGUID(const S: string): TGUID;
  50. function HexChar(c: Char): Byte;
  51. begin
  52. case c of
  53. '0'..'9':
  54. Result:=Byte(c) - Byte('0');
  55. 'a'..'f':
  56. Result:=(Byte(c) - Byte('a')) + 10;
  57. 'A'..'F':
  58. Result:=(Byte(c) - Byte('A')) + 10;
  59. else
  60. raise EConvertError.CreateFmt(SInvalidGUID, [s]);
  61. Result:=0;
  62. end;
  63. end;
  64. function HexByte(p: PChar): Char;
  65. begin
  66. Result:=Char((HexChar(p[0]) shl 4) + HexChar(p[1]));
  67. end;
  68. var
  69. i: integer;
  70. src, dest: PChar;
  71. begin
  72. if ((Length(S)<>38) or
  73. (s[1]<>'{')) then
  74. raise EConvertError.CreateFmt(SInvalidGUID, [s]);
  75. dest:=@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;
  133. {
  134. $Log$
  135. Revision 1.1 2003-10-06 21:01:06 peter
  136. * moved classes unit to rtl
  137. Revision 1.1 2002/01/25 17:42:03 peter
  138. * interface helpers
  139. }