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.

276 lines
7.0 KiB

  1. /*++
  2. Copyright (C) 1997-2001 Microsoft Corporation
  3. Module Name:
  4. MOFUTILSD.CPP
  5. Abstract:
  6. Support of trace output and internationalized strings.
  7. History:
  8. a-davj 13-July-97 Created.
  9. --*/
  10. #include "precomp.h"
  11. #include <reg.h>
  12. #include "strings.h"
  13. #include <stdio.h>
  14. #include <stdarg.h>
  15. #include <wbemutil.h>
  16. #include <wbemcli.h>
  17. #include "mofutils.h"
  18. TCHAR JustInCase = 0;
  19. //***************************************************************************
  20. //
  21. // int Trace
  22. //
  23. // DESCRIPTION:
  24. //
  25. // Allows for the output function (printf in this case) to be overridden.
  26. //
  27. // PARAMETERS:
  28. //
  29. // *fmt format string. Ex "%s hello %d"
  30. // ... argument list. Ex cpTest, 23
  31. //
  32. // RETURN VALUE:
  33. //
  34. // size of output in characters.
  35. //***************************************************************************
  36. int Trace(bool bError, DWORD dwID, ...)
  37. {
  38. IntString is(dwID);
  39. TCHAR * fmt = is;
  40. TCHAR *buffer = new TCHAR[2048];
  41. if(buffer == NULL)
  42. return 0;
  43. char *buffer2 = new char[4096];
  44. if(buffer2 == NULL)
  45. {
  46. delete buffer;
  47. return 0;
  48. }
  49. va_list argptr;
  50. int cnt;
  51. va_start(argptr, dwID);
  52. #ifdef UNICODE
  53. cnt = _vsnwprintf(buffer, 2048, fmt, argptr);
  54. #else
  55. cnt = _vsnprintf(buffer, 2048, fmt, argptr);
  56. #endif
  57. va_end(argptr);
  58. CharToOem(buffer, buffer2);
  59. printf("%s", buffer2);
  60. if(bError)
  61. ERRORTRACE((LOG_MOFCOMP,"%s", buffer2));
  62. else
  63. DEBUGTRACE((LOG_MOFCOMP,"%s", buffer2));
  64. delete buffer;
  65. delete buffer2;
  66. return cnt;
  67. }
  68. void PrintUsage()
  69. {
  70. Trace(false, USAGE1);
  71. Trace(false, USAGE1A);
  72. Trace(false, USAGE1B);
  73. Trace(false, USAGE1C);
  74. Trace(false, USAGE1D);
  75. Trace(false, USAGE1E);
  76. Trace(false, USAGE1F);
  77. Trace(false, USAGE2);
  78. Trace(false, USAGE3);
  79. Trace(false, USAGE4);
  80. Trace(false, USAGE4a);
  81. Trace(false, USAGE4b);
  82. Trace(false, USAGE5);
  83. Trace(false, USAGE6);
  84. Trace(false, USAGE7);
  85. Trace(false, USAGE8);
  86. Trace(false, USAGE9);
  87. Trace(false, USAGE10);
  88. Trace(false, USAGE11);
  89. Trace(false, USAGE12);
  90. Trace(false, USAGE12A);
  91. Trace(false, USAGE12B);
  92. Trace(false, USAGE12C);
  93. Trace(false, USAGE12D);
  94. Trace(false, USAGE12E);
  95. Trace(false, USAGE13);
  96. Trace(false, USAGE14);
  97. }
  98. //******************************************************************************
  99. //
  100. // See GETVER.H for documentation
  101. //
  102. //******************************************************************************
  103. BOOL GetVerInfo(TCHAR * pResStringName,
  104. TCHAR * pRes, DWORD dwResSize)
  105. {
  106. // Extract Version informatio
  107. DWORD dwTemp, dwSize = MAX_PATH;
  108. TCHAR cName[MAX_PATH];
  109. BOOL bRet = FALSE;
  110. HINSTANCE hInst = GetModuleHandle(NULL);
  111. long lSize = GetModuleFileName(hInst, cName, MAX_PATH);
  112. if(lSize == 0)
  113. return FALSE;
  114. lSize = GetFileVersionInfoSize(cName, &dwTemp);
  115. if(lSize < 1)
  116. return FALSE;
  117. TCHAR * pBlock = new TCHAR[lSize];
  118. if(pBlock != NULL)
  119. {
  120. bRet = GetFileVersionInfo(cName, NULL, lSize, pBlock);
  121. if(bRet)
  122. {
  123. TCHAR lpSubBlock[MAX_PATH];
  124. TCHAR * lpBuffer = NULL;
  125. UINT wBuffSize = MAX_PATH;
  126. short * piStuff;
  127. bRet = VerQueryValue(pBlock, TEXT("\\VarFileInfo\\Translation") , (void**)&piStuff, &wBuffSize);
  128. if(bRet)
  129. {
  130. wsprintf(lpSubBlock,TEXT("\\StringFileInfo\\%04x%04x\\%s"),piStuff[0], piStuff[1],"ProductVersion");
  131. bRet = VerQueryValue(pBlock, lpSubBlock, (void**)&lpBuffer, &wBuffSize);
  132. }
  133. if(bRet == FALSE)
  134. {
  135. // Try again in english
  136. wsprintf(lpSubBlock,TEXT("\\StringFileInfo\\040904E4\\%s"),pResStringName);
  137. bRet = VerQueryValue(pBlock, lpSubBlock,(void**)&lpBuffer, &wBuffSize);
  138. }
  139. if(bRet)
  140. lstrcpyn(pRes, lpBuffer, dwResSize);
  141. }
  142. delete pBlock;
  143. }
  144. return bRet;
  145. }
  146. IntString::IntString(DWORD dwID)
  147. {
  148. DWORD dwSize, dwRet;
  149. for(dwSize = 128; dwSize < 4096; dwSize *= 2)
  150. {
  151. m_pString = new TCHAR[dwSize];
  152. if(m_pString == NULL)
  153. {
  154. m_pString = &JustInCase; // should never happen!
  155. return;
  156. }
  157. dwRet = LoadString( GetModuleHandle(NULL), dwID, m_pString, dwSize);
  158. // Check for failure to load
  159. if(dwRet == 0)
  160. {
  161. m_pString = &JustInCase; // should never happen!
  162. return;
  163. }
  164. // Check for the case where the buffer was too small
  165. if((dwRet + 1) >= dwSize)
  166. delete m_pString;
  167. else
  168. return; // all is well!
  169. }
  170. }
  171. IntString::~IntString()
  172. {
  173. if(m_pString != &JustInCase)
  174. delete(m_pString);
  175. }
  176. //***************************************************************************
  177. //
  178. // BOOL bGetString
  179. //
  180. // DESCRIPTION:
  181. //
  182. // Converts a command line argument into a WCHAR string. Note that the arugment is
  183. // of the form /X:stuff. This is passed a pointer to the colon.
  184. //
  185. // PARAMETERS:
  186. //
  187. // pArg Input, pointer to the colon
  188. // pOut Points the the output buffer where the data is to be copied.
  189. // IT IS ASSUMED THAT pOut points to a buffer of MAX_PATH length
  190. //
  191. //
  192. // RETURN VALUE:
  193. //
  194. // TRUE if OK
  195. //
  196. //***************************************************************************
  197. BOOL bGetString(char * pIn, WCHAR * pOut)
  198. {
  199. if(pIn == NULL)
  200. return FALSE;
  201. if(*pIn != ':')
  202. {
  203. PrintUsage();
  204. return FALSE;
  205. }
  206. pIn++; // skip passed the colon
  207. int iLen = mbstowcs(NULL, pIn, strlen(pIn)+1);
  208. if(iLen > MAX_PATH-1)
  209. {
  210. PrintUsage();
  211. return FALSE;
  212. }
  213. int iRet = mbstowcs(pOut, pIn, MAX_PATH-1);
  214. if(iRet < 1)
  215. {
  216. PrintUsage();
  217. return FALSE;
  218. }
  219. return TRUE;
  220. }
  221. //***************************************************************************
  222. //
  223. // ValidFlags.
  224. //
  225. //***************************************************************************
  226. bool ValidFlags(bool bClass, long lFlags)
  227. {
  228. if(bClass)
  229. return ((lFlags == WBEM_FLAG_CREATE_OR_UPDATE) ||
  230. (lFlags == WBEM_FLAG_UPDATE_ONLY) ||
  231. (lFlags == WBEM_FLAG_CREATE_ONLY) ||
  232. (lFlags == WBEM_FLAG_UPDATE_SAFE_MODE) ||
  233. (lFlags == WBEM_FLAG_UPDATE_FORCE_MODE) ||
  234. (lFlags == (WBEM_FLAG_UPDATE_ONLY | WBEM_FLAG_UPDATE_SAFE_MODE)) ||
  235. (lFlags == (WBEM_FLAG_UPDATE_ONLY | WBEM_FLAG_UPDATE_FORCE_MODE)));
  236. else
  237. return
  238. ((lFlags == WBEM_FLAG_CREATE_OR_UPDATE) ||
  239. (lFlags == WBEM_FLAG_UPDATE_ONLY) ||
  240. (lFlags == WBEM_FLAG_CREATE_ONLY));
  241. }