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.

245 lines
4.9 KiB

  1. /*++
  2. Copyright (c) 1990-2003 Microsoft Corporation
  3. All Rights Reserved
  4. Module Name:
  5. util.c
  6. Abstract:
  7. This module provides all the utility functions for localui.
  8. // @@BEGIN_DDKSPLIT
  9. Revision History:
  10. // @@END_DDKSPLIT
  11. --*/
  12. #include "precomp.h"
  13. #pragma hdrstop
  14. #include "spltypes.h"
  15. #include "local.h"
  16. #include "localui.h"
  17. PWSTR
  18. ConstructXcvName(
  19. PCWSTR pServerName,
  20. PCWSTR pObjectName,
  21. PCWSTR pObjectType
  22. )
  23. {
  24. size_t cchOutput;
  25. PWSTR pOut;
  26. cchOutput = pServerName ? (wcslen(pServerName) + 2) : 1; /* "\\Server\," */
  27. cchOutput += wcslen(pObjectType) + 2; /* "\\Server\,XcvPort _" */
  28. cchOutput += pObjectName ? wcslen(pObjectName) : 0; /* "\\Server\,XcvPort Object_" */
  29. if (pOut = AllocSplMem(cchOutput * sizeof (pOut [0]))) {
  30. if (pServerName) {
  31. StringCchCopy(pOut, cchOutput, pServerName);
  32. StringCchCat (pOut, cchOutput, L"\\,");
  33. }
  34. else
  35. {
  36. StringCchCopy (pOut, cchOutput, L",");
  37. }
  38. StringCchCat (pOut, cchOutput, pObjectType);
  39. StringCchCat (pOut, cchOutput, L" ");
  40. if (pObjectName)
  41. {
  42. StringCchCat (pOut, cchOutput, pObjectName);
  43. }
  44. }
  45. return pOut;
  46. }
  47. BOOL
  48. IsCOMPort(
  49. PCWSTR pPort
  50. )
  51. {
  52. //
  53. // Must begin with szCom
  54. //
  55. if ( _wcsnicmp( pPort, szCOM, 3 ) )
  56. {
  57. return FALSE;
  58. }
  59. //
  60. // wcslen guarenteed >= 3
  61. //
  62. return pPort[ wcslen( pPort ) - 1 ] == L':';
  63. }
  64. BOOL
  65. IsLPTPort(
  66. PCWSTR pPort
  67. )
  68. {
  69. //
  70. // Must begin with szLPT
  71. //
  72. if ( _wcsnicmp( pPort, szLPT, 3 ) )
  73. {
  74. return FALSE;
  75. }
  76. //
  77. // wcslen guarenteed >= 3
  78. //
  79. return pPort[ wcslen( pPort ) - 1 ] == L':';
  80. }
  81. /* Message
  82. *
  83. * Displays a message by loading the strings whose IDs are passed into
  84. * the function, and substituting the supplied variable argument list
  85. * using the varargs macros.
  86. *
  87. */
  88. int Message(HWND hwnd, DWORD Type, int CaptionID, int TextID, ...)
  89. {
  90. WCHAR MsgText[2*MAX_PATH + 1];
  91. WCHAR MsgFormat[256];
  92. WCHAR MsgCaption[40];
  93. va_list vargs;
  94. if( ( LoadString( hInst, TextID, MsgFormat,
  95. sizeof MsgFormat / sizeof *MsgFormat ) > 0 )
  96. && ( LoadString( hInst, CaptionID, MsgCaption,
  97. sizeof MsgCaption / sizeof *MsgCaption ) > 0 ) )
  98. {
  99. va_start( vargs, TextID );
  100. StringCchVPrintf ( MsgText, COUNTOF(MsgText), MsgFormat, vargs );
  101. va_end( vargs );
  102. MsgText[COUNTOF(MsgText) - 1] = L'\0';
  103. return MessageBox(hwnd, MsgText, MsgCaption, Type);
  104. }
  105. else
  106. return 0;
  107. }
  108. INT
  109. ErrorMessage(
  110. HWND hwnd,
  111. DWORD dwStatus
  112. )
  113. {
  114. WCHAR MsgCaption[MAX_PATH];
  115. PWSTR pBuffer = NULL;
  116. INT iRet = 0;
  117. FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM |
  118. FORMAT_MESSAGE_ALLOCATE_BUFFER,
  119. NULL,
  120. dwStatus,
  121. 0,
  122. (PWSTR) &pBuffer,
  123. 0,
  124. NULL);
  125. if (pBuffer) {
  126. if (LoadString( hInst, IDS_LOCALMONITOR, MsgCaption,
  127. sizeof MsgCaption / sizeof *MsgCaption) > 0) {
  128. iRet = MessageBox(hwnd, pBuffer, MsgCaption, MSG_ERROR);
  129. }
  130. LocalFree(pBuffer);
  131. }
  132. return iRet;
  133. }
  134. LPWSTR
  135. AllocSplStr(
  136. LPCWSTR pStr
  137. )
  138. /*++
  139. Routine Description:
  140. This function will allocate enough local memory to store the specified
  141. string, and copy that string to the allocated memory
  142. Arguments:
  143. pStr - Pointer to the string that needs to be allocated and stored
  144. Return Value:
  145. NON-NULL - A pointer to the allocated memory containing the string
  146. FALSE/NULL - The operation failed. Extended error status is available
  147. using GetLastError.
  148. --*/
  149. {
  150. LPWSTR pMem;
  151. DWORD cbStr;
  152. if (!pStr) {
  153. return NULL;
  154. }
  155. cbStr = wcslen(pStr)*sizeof(WCHAR) + sizeof(WCHAR);
  156. if (pMem = AllocSplMem( cbStr )) {
  157. CopyMemory( pMem, pStr, cbStr );
  158. }
  159. return pMem;
  160. }
  161. LPVOID
  162. AllocSplMem(
  163. DWORD cbAlloc
  164. )
  165. {
  166. return GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT, cbAlloc);
  167. }
  168. // -----------------------------------------------------------------------
  169. //
  170. // DEBUG Stuff
  171. //
  172. // -----------------------------------------------------------------------
  173. DWORD SplDbgLevel = 0;
  174. VOID cdecl DbgMsg( LPWSTR MsgFormat, ... )
  175. {
  176. WCHAR MsgText[1024];
  177. va_list pArgs;
  178. va_start( pArgs, MsgFormat);
  179. StringCchVPrintf (MsgText, COUNTOF (MsgText), MsgFormat, pArgs);
  180. StringCchCat( MsgText, COUNTOF (MsgText), L"\r");
  181. va_end( pArgs);
  182. OutputDebugString(MsgText);
  183. }