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.

304 lines
6.4 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1997 - 1999
  3. Module Name:
  4. common.cxx
  5. Abstract:
  6. This file contains some common stuff for SENS project.
  7. Author:
  8. Gopal Parupudi <GopalP>
  9. [Notes:]
  10. optional-notes
  11. Revision History:
  12. GopalP 10/11/1997 Start.
  13. --*/
  14. #include "common.hxx"
  15. #ifdef DBG
  16. extern DWORD gdwDebugOutputLevel;
  17. #endif // DBG
  18. //
  19. // Constants
  20. //
  21. #define MAX_DBGPRINT_LEN 512
  22. //
  23. // Available only on Debug builds.
  24. //
  25. #if DBG
  26. #define SENS_DEBUG_PREFIXA "[SENS] "
  27. #define SENS_DEBUG_PREFIXW L"[SENS] "
  28. ULONG _cdecl
  29. SensDbgPrintA(
  30. CHAR * Format,
  31. ...
  32. )
  33. /*++
  34. Routine Description:
  35. Equivalent of NT's DbgPrint().
  36. Arguments:
  37. Same as for printf()
  38. Return Value:
  39. 0, if successful.
  40. --*/
  41. {
  42. va_list arglist;
  43. CHAR OutputBuffer[MAX_DBGPRINT_LEN];
  44. ULONG length;
  45. // See if we are supposed to print
  46. if (0x0 == gdwDebugOutputLevel)
  47. {
  48. return -1;
  49. }
  50. length = sizeof(SENS_DEBUG_PREFIXA);
  51. memcpy(OutputBuffer, SENS_DEBUG_PREFIXA, length); // indicate this is a SENS message.
  52. //
  53. // Put the information requested by the caller onto the line
  54. //
  55. va_start(arglist, Format);
  56. HRESULT hr = StringCchVPrintfA(&OutputBuffer[length - 1], MAX_DBGPRINT_LEN - length, Format, arglist);
  57. // We don't care about failures - this just means the result was truncated.
  58. va_end(arglist);
  59. //
  60. // Just output to the debug terminal
  61. //
  62. OutputDebugStringA(OutputBuffer);
  63. return (0);
  64. }
  65. ULONG _cdecl
  66. SensDbgPrintW(
  67. WCHAR * Format,
  68. ...
  69. )
  70. /*++
  71. Routine Description:
  72. Equivalent of NT's DbgPrint().
  73. Arguments:
  74. Same as for printf()
  75. Return Value:
  76. 0, if successful.
  77. --*/
  78. {
  79. va_list arglist;
  80. WCHAR OutputBuffer[MAX_DBGPRINT_LEN];
  81. ULONG length;
  82. // See if we are supposed to print
  83. if (0x0 == gdwDebugOutputLevel)
  84. {
  85. return -1;
  86. }
  87. length = sizeof(SENS_DEBUG_PREFIXW);
  88. memcpy(OutputBuffer, SENS_DEBUG_PREFIXW, length); // indicate this is a SENS message.
  89. //
  90. // Put the information requested by the caller onto the line
  91. //
  92. va_start(arglist, Format);
  93. HRESULT hr = StringCchVPrintfW(&OutputBuffer[(length/sizeof(WCHAR))-1], MAX_DBGPRINT_LEN - (length/sizeof(WCHAR)), Format, arglist);
  94. // We don't care about failures - this just means the result was truncated.
  95. va_end(arglist);
  96. //
  97. // Just output to the debug terminal
  98. //
  99. OutputDebugStringW(OutputBuffer);
  100. return (0);
  101. }
  102. BOOL
  103. ValidateError(
  104. IN int Status,
  105. IN unsigned int Count,
  106. IN const int ErrorList[])
  107. /*++
  108. Routine Description
  109. Tests that 'Status' is one of an expected set of error codes.
  110. Used on debug builds as part of the VALIDATE() macro.
  111. Example:
  112. VALIDATE(EventStatus)
  113. {
  114. RPC_P_CONNECTION_CLOSED,
  115. RPC_P_RECEIVE_FAILED,
  116. RPC_P_CONNECTION_SHUTDOWN
  117. // more error codes here
  118. } END_VALIDATE;
  119. This function is called with the RpcStatus and expected errors codes
  120. as parameters. If RpcStatus is not one of the expected error
  121. codes and it not zero a message will be printed to the debugger
  122. and the function will return false. The VALIDATE macro ASSERT's the
  123. return value.
  124. Arguments:
  125. Status - Status code in question.
  126. Count - number of variable length arguments
  127. ... - One or more expected status codes. Terminated with 0 (RPC_S_OK).
  128. Return Value:
  129. TRUE - Status code is in the list or the status is 0.
  130. FALSE - Status code is not in the list.
  131. --*/
  132. {
  133. unsigned int i;
  134. for (i = 0; i < Count; i++)
  135. {
  136. if (ErrorList[i] == Status)
  137. {
  138. return TRUE;
  139. }
  140. }
  141. SensPrintToDebugger(SENS_DEB, ("[SENS] Assertion: unexpected failure %lu (0x%08x)\n",
  142. (unsigned long)Status, (unsigned long)Status));
  143. return(FALSE);
  144. }
  145. void
  146. EnableDebugOutputIfNecessary(
  147. void
  148. )
  149. /*++
  150. This routine tries to set gdwDebugOuputLevel to the value defined
  151. in the regitry at HKLM\Software\Microsoft\Mobile\SENS\Debug value.
  152. All binaries using this function need to define the following
  153. global value:
  154. DWORD gdwDebugOutputLevel;
  155. --*/
  156. {
  157. HRESULT hr;
  158. HKEY hKeySens;
  159. LONG RegStatus;
  160. BOOL bStatus;
  161. DWORD dwType;
  162. DWORD cbData;
  163. LPBYTE lpbData;
  164. hr = S_OK;
  165. hKeySens = NULL;
  166. RegStatus = ERROR_SUCCESS;
  167. bStatus = FALSE;
  168. dwType = 0x0;
  169. cbData = 0x0;
  170. lpbData = NULL;
  171. RegStatus = RegOpenKeyEx(
  172. HKEY_LOCAL_MACHINE, // Handle of the key
  173. SENS_REGISTRY_KEY, // String which represents the sub-key to open
  174. 0, // Reserved (MBZ)
  175. KEY_QUERY_VALUE, // Security Access mask
  176. &hKeySens // Returned HKEY
  177. );
  178. if (RegStatus != ERROR_SUCCESS)
  179. {
  180. SensPrintToDebugger(SENS_ERR, ("[SENS] RegOpenKeyEx(Sens) returned %d.\n", RegStatus));
  181. goto Cleanup;
  182. }
  183. //
  184. // Query the Configured value
  185. //
  186. lpbData = (LPBYTE) &gdwDebugOutputLevel;
  187. cbData = sizeof(DWORD);
  188. RegStatus = RegQueryValueEx(
  189. hKeySens, // Handle of the sub-key
  190. SENS_DEBUG_LEVEL, // Name of the Value
  191. NULL, // Reserved (MBZ)
  192. &dwType, // Address of the type of the Value
  193. lpbData, // Address of the data of the Value
  194. &cbData // Address of size of data of the Value
  195. );
  196. if (RegStatus != ERROR_SUCCESS)
  197. {
  198. SensPrintToDebugger(SENS_ERR, ("[SENS] RegQueryValueEx(SENS_DEBUG_LEVEL) failed with 0x%x\n", RegStatus));
  199. gdwDebugOutputLevel = 0x0;
  200. goto Cleanup;
  201. }
  202. SensPrintToDebugger(SENS_INFO, ("[SENS] Debug Output is turned %s. The level is 0x%x.\n",
  203. gdwDebugOutputLevel ? "ON" : "OFF", gdwDebugOutputLevel));
  204. Cleanup:
  205. //
  206. // Cleanup
  207. //
  208. if (hKeySens)
  209. {
  210. RegCloseKey(hKeySens);
  211. }
  212. return;
  213. }
  214. #endif // DBG