sysuintf.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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): Char;
  64. begin
  65. Result:=Char((HexChar(p[0]) shl 4) + HexChar(p[1]));
  66. end;
  67. var
  68. i: integer;
  69. src, dest: PChar;
  70. begin
  71. if ((Length(S)<>38) or
  72. (s[1]<>'{')) then
  73. raise EConvertError.CreateFmt(SInvalidGUID, [s]);
  74. dest:=@Result;
  75. src:=PChar(s);
  76. inc(src);
  77. for i:=0 to 3 do
  78. dest[i]:=HexByte(src+(3-i)*2);
  79. inc(src, 8);
  80. inc(dest, 4);
  81. if src[0]<>'-' then
  82. raise EConvertError.CreateFmt(SInvalidGUID, [s]);
  83. inc(src);
  84. for i:=0 to 1 do
  85. begin
  86. dest^:=HexByte(src+2);
  87. inc(dest);
  88. dest^:=HexByte(src);
  89. inc(dest);
  90. inc(src, 4);
  91. if src[0]<>'-' then
  92. raise EConvertError.CreateFmt(SInvalidGUID, [s]);
  93. inc(src);
  94. end;
  95. dest^:=HexByte(src);
  96. inc(dest);
  97. inc(src, 2);
  98. dest^:=HexByte(src);
  99. inc(dest);
  100. inc(src, 2);
  101. if src[0]<>'-' then
  102. raise EConvertError.CreateFmt(SInvalidGUID, [s]);
  103. inc(src);
  104. for i:=0 to 5 do
  105. begin
  106. dest^:=HexByte(src);
  107. inc(dest);
  108. inc(src, 2);
  109. end;
  110. end;
  111. function IsEqualGUID(const guid1, guid2: TGUID): Boolean;
  112. var
  113. a1,a2: PIntegerArray;
  114. begin
  115. a1:=PIntegerArray(@guid1);
  116. a2:=PIntegerArray(@guid2);
  117. Result:=(a1^[0]=a2^[0]) and
  118. (a1^[1]=a2^[1]) and
  119. (a1^[2]=a2^[2]) and
  120. (a1^[3]=a2^[3]);
  121. end;
  122. function GUIDToString(const GUID: TGUID): string;
  123. begin
  124. SetLength(Result, 38);
  125. StrLFmt(PChar(Result), 38,'{%.8x-%.4x-%.4x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x}',
  126. [
  127. GUID.D1, GUID.D2, GUID.D3,
  128. GUID.D4[0], GUID.D4[1], GUID.D4[2], GUID.D4[3],
  129. GUID.D4[4], GUID.D4[5], GUID.D4[6], GUID.D4[7]
  130. ]);
  131. end;