Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
4.4 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1997, Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // iasutil.h
  8. //
  9. // SYNOPSIS
  10. //
  11. // This file declares assorted utility functions, etc.
  12. //
  13. // MODIFICATION HISTORY
  14. //
  15. // 11/14/1997 Original version.
  16. // 12/17/1997 Added conversion routines.
  17. // 01/08/1998 Added RETURN_ERROR macro.
  18. // 02/26/1998 Added ANSI versions of the IP address functions.
  19. // 04/17/1998 Added CComInterlockedPtr.
  20. // 08/11/1998 Major overhaul and consolidation of utility functions.
  21. //
  22. ///////////////////////////////////////////////////////////////////////////////
  23. #ifndef _IASUTIL_H_
  24. #define _IASUTIL_H_
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. ///////////////////////////////////////////////////////////////////////////////
  29. //
  30. // String functions.
  31. //
  32. ///////////////////////////////////////////////////////////////////////////////
  33. LPWSTR
  34. WINAPI
  35. ias_wcsdup(
  36. IN PCWSTR str
  37. );
  38. LPSTR
  39. WINAPI
  40. com_strdup(
  41. IN PCSTR str
  42. );
  43. LPWSTR
  44. WINAPI
  45. com_wcsdup(
  46. IN PCWSTR str
  47. );
  48. INT
  49. WINAPI
  50. ias_wcscmp(
  51. IN PCWSTR str1,
  52. IN PCWSTR str2
  53. );
  54. LPWSTR
  55. WINAPIV
  56. ias_makewcs(LPCWSTR, ...);
  57. ///////////////////////////////////////////////////////////////////////////////
  58. //
  59. // IP address conversion functions.
  60. //
  61. ///////////////////////////////////////////////////////////////////////////////
  62. ULONG
  63. WINAPI
  64. ias_inet_wtoh(
  65. PCWSTR cp
  66. );
  67. PWSTR
  68. WINAPI
  69. ias_inet_htow(
  70. IN ULONG addr,
  71. OUT PWSTR dst
  72. );
  73. ULONG
  74. WINAPI
  75. ias_inet_atoh(
  76. PCSTR cp
  77. );
  78. PSTR
  79. WINAPI
  80. ias_inet_htoa(
  81. IN ULONG addr,
  82. OUT PSTR dst
  83. );
  84. ULONG
  85. WINAPI
  86. IASStringToSubNetW(
  87. PCWSTR cp,
  88. ULONG* width
  89. );
  90. ULONG
  91. WINAPI
  92. IASStringToSubNetA(
  93. PCSTR cp,
  94. ULONG* width
  95. );
  96. ///////////////////////////////////////////////////////////////////////////////
  97. //
  98. // Functions to move integers to and from a buffer.
  99. //
  100. ///////////////////////////////////////////////////////////////////////////////
  101. VOID
  102. WINAPI
  103. IASInsertDWORD(
  104. IN PBYTE pBuffer,
  105. IN DWORD dwValue
  106. );
  107. DWORD
  108. WINAPI
  109. IASExtractDWORD(
  110. IN CONST BYTE *pBuffer
  111. );
  112. VOID
  113. WINAPI
  114. IASInsertWORD(
  115. IN PBYTE pBuffer,
  116. IN WORD wValue
  117. );
  118. WORD
  119. WINAPI
  120. IASExtractWORD(
  121. IN CONST BYTE *pBuffer
  122. );
  123. #ifdef __cplusplus
  124. }
  125. // We need this for std::bad_alloc.
  126. #include <new>
  127. // For _com_error
  128. #include "comdef.h"
  129. ///////////////////////////////////////////////////////////////////////////////
  130. //
  131. // Extensions to _com_error to handle Win32 errors.
  132. //
  133. ///////////////////////////////////////////////////////////////////////////////
  134. // Exception class for Win32 errors.
  135. class _w32_error : public _com_error
  136. {
  137. public:
  138. _w32_error(DWORD errorCode) throw ()
  139. : _com_error(HRESULT_FROM_WIN32(errorCode)) { }
  140. DWORD Error() const
  141. {
  142. return _com_error::Error() & 0x0000FFFF;
  143. }
  144. };
  145. // Throw a _w32_error.
  146. void __stdcall _w32_issue_error(DWORD errorCode = GetLastError())
  147. throw (_w32_error);
  148. // Utility functions for checking Win32 return values and throwing an
  149. // exception upon failure.
  150. namespace _w32_util
  151. {
  152. // Check handles, memory, etc.
  153. inline void CheckAlloc(const void* p) throw (_w32_error)
  154. {
  155. if (p == NULL) { _w32_issue_error(); }
  156. }
  157. // Check 32-bit error code.
  158. inline void CheckError(DWORD errorCode) throw (_w32_error)
  159. {
  160. if (errorCode != NO_ERROR) { _w32_issue_error(errorCode); }
  161. }
  162. // Check boolean success flag.
  163. inline void CheckSuccess(BOOL success) throw (_w32_error)
  164. {
  165. if (!success) { _w32_issue_error(); }
  166. }
  167. }
  168. ///////////////////////////////////////////////////////////////////////////////
  169. //
  170. // Miscellaneous macros.
  171. //
  172. ///////////////////////////////////////////////////////////////////////////////
  173. // Allocate an array on the stack.
  174. #define IAS_STACK_NEW(type, count) \
  175. new (_alloca(sizeof(type) * count)) type[count]
  176. // Safely release an object.
  177. #define IAS_DEREF(obj) \
  178. if (obj) { (obj)->Release(); (obj) = NULL; }
  179. // Return the error code from a failed COM invocation. Useful if you don't
  180. // have to do any special clean-up.
  181. #define RETURN_ERROR(expr) \
  182. { HRESULT __hr__ = (expr); if (FAILED(__hr__)) return __hr__; }
  183. // Catch any exception and return an appropriate error code.
  184. #define CATCH_AND_RETURN() \
  185. catch (const std::bad_alloc&) { return E_OUTOFMEMORY; } \
  186. catch (const _com_error& ce) { return ce.Error(); } \
  187. catch (...) { return E_FAIL; }
  188. #endif
  189. #endif // _IASUTIL_H_