Leaked source code of windows server 2003
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.

210 lines
4.5 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. ///////////////////////////////////////////////////////////////////////////////
  85. //
  86. // Functions to move integers to and from a buffer.
  87. //
  88. ///////////////////////////////////////////////////////////////////////////////
  89. VOID
  90. WINAPI
  91. IASInsertDWORD(
  92. IN PBYTE pBuffer,
  93. IN DWORD dwValue
  94. );
  95. DWORD
  96. WINAPI
  97. IASExtractDWORD(
  98. IN CONST BYTE *pBuffer
  99. );
  100. VOID
  101. WINAPI
  102. IASInsertWORD(
  103. IN PBYTE pBuffer,
  104. IN WORD wValue
  105. );
  106. WORD
  107. WINAPI
  108. IASExtractWORD(
  109. IN CONST BYTE *pBuffer
  110. );
  111. #ifdef __cplusplus
  112. }
  113. // We need this for std::bad_alloc.
  114. #include <new>
  115. // For _com_error
  116. #include "comdef.h"
  117. ///////////////////////////////////////////////////////////////////////////////
  118. //
  119. // Extensions to _com_error to handle Win32 errors.
  120. //
  121. ///////////////////////////////////////////////////////////////////////////////
  122. // Exception class for Win32 errors.
  123. class _w32_error : public _com_error
  124. {
  125. public:
  126. _w32_error(DWORD errorCode) throw ()
  127. : _com_error(HRESULT_FROM_WIN32(errorCode)) { }
  128. DWORD Error() const
  129. {
  130. return _com_error::Error() & 0x0000FFFF;
  131. }
  132. };
  133. // Throw a _w32_error.
  134. void __stdcall _w32_issue_error(DWORD errorCode = GetLastError())
  135. throw (_w32_error);
  136. // Utility functions for checking Win32 return values and throwing an
  137. // exception upon failure.
  138. namespace _w32_util
  139. {
  140. // Check handles, memory, etc.
  141. inline void CheckAlloc(const void* p) throw (_w32_error)
  142. {
  143. if (p == NULL) { _w32_issue_error(); }
  144. }
  145. // Check 32-bit error code.
  146. inline void CheckError(DWORD errorCode) throw (_w32_error)
  147. {
  148. if (errorCode != NO_ERROR) { _w32_issue_error(errorCode); }
  149. }
  150. // Check boolean success flag.
  151. inline void CheckSuccess(BOOL success) throw (_w32_error)
  152. {
  153. if (!success) { _w32_issue_error(); }
  154. }
  155. }
  156. ///////////////////////////////////////////////////////////////////////////////
  157. //
  158. // Miscellaneous macros.
  159. //
  160. ///////////////////////////////////////////////////////////////////////////////
  161. // Allocate an array on the stack.
  162. #define IAS_STACK_NEW(type, count) \
  163. new (_alloca(sizeof(type) * count)) type[count]
  164. // Safely release an object.
  165. #define IAS_DEREF(obj) \
  166. if (obj) { (obj)->Release(); (obj) = NULL; }
  167. // Return the error code from a failed COM invocation. Useful if you don't
  168. // have to do any special clean-up.
  169. #define RETURN_ERROR(expr) \
  170. { HRESULT __hr__ = (expr); if (FAILED(__hr__)) return __hr__; }
  171. // Catch any exception and return an appropriate error code.
  172. #define CATCH_AND_RETURN() \
  173. catch (const std::bad_alloc&) { return E_OUTOFMEMORY; } \
  174. catch (const _com_error& ce) { return ce.Error(); } \
  175. catch (...) { return E_FAIL; }
  176. #endif
  177. #endif // _IASUTIL_H_