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.

373 lines
9.4 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 <bmof.h>
  18. #include <mrciclass.h>
  19. #include <arrtempl.h>
  20. #include <scopeguard.h>
  21. #include "mofutils.h"
  22. TCHAR JustInCase = 0;
  23. //***************************************************************************
  24. //
  25. // int Trace
  26. //
  27. // DESCRIPTION:
  28. //
  29. // Allows for the output function (printf in this case) to be overridden.
  30. //
  31. // PARAMETERS:
  32. //
  33. // *fmt format string. Ex "%s hello %d"
  34. // ... argument list. Ex cpTest, 23
  35. //
  36. // RETURN VALUE:
  37. //
  38. // size of output in characters.
  39. //***************************************************************************
  40. int Trace(bool bError, DWORD dwID, ...)
  41. {
  42. IntString is(dwID);
  43. TCHAR * fmt = is;
  44. TCHAR *buffer = new TCHAR[2048];
  45. if(buffer == NULL)
  46. return 0;
  47. char *buffer2 = new char[4096];
  48. if(buffer2 == NULL)
  49. {
  50. delete buffer;
  51. return 0;
  52. }
  53. va_list argptr;
  54. int cnt;
  55. va_start(argptr, dwID);
  56. #ifdef UNICODE
  57. cnt = _vsnwprintf(buffer, 2048, fmt, argptr);
  58. #else
  59. cnt = _vsnprintf(buffer, 2048, fmt, argptr);
  60. #endif
  61. va_end(argptr);
  62. CharToOem(buffer, buffer2);
  63. printf("%s", buffer2);
  64. if(bError)
  65. ERRORTRACE((LOG_MOFCOMP,"%s", buffer2));
  66. else
  67. DEBUGTRACE((LOG_MOFCOMP,"%s", buffer2));
  68. delete buffer;
  69. delete buffer2;
  70. return cnt;
  71. }
  72. void PrintUsage()
  73. {
  74. Trace(false, USAGE1);
  75. Trace(false, USAGE1A);
  76. Trace(false, USAGE1B);
  77. Trace(false, USAGE1C);
  78. Trace(false, USAGE1D);
  79. Trace(false, USAGE1E);
  80. Trace(false, USAGE1F);
  81. Trace(false, USAGE2);
  82. Trace(false, USAGE3);
  83. Trace(false, USAGE4);
  84. Trace(false, USAGE4a);
  85. Trace(false, USAGE4b);
  86. Trace(false, USAGE5);
  87. Trace(false, USAGE6);
  88. Trace(false, USAGE7);
  89. Trace(false, USAGE8);
  90. Trace(false, USAGE9);
  91. Trace(false, USAGE10);
  92. Trace(false, USAGE11);
  93. Trace(false, USAGE12);
  94. Trace(false, USAGE12A);
  95. Trace(false, USAGE12B);
  96. Trace(false, USAGE12C);
  97. Trace(false, USAGE12D);
  98. Trace(false, USAGE12E);
  99. Trace(false, USAGE13);
  100. Trace(false, USAGE14);
  101. }
  102. //******************************************************************************
  103. //
  104. // See GETVER.H for documentation
  105. //
  106. //******************************************************************************
  107. BOOL GetVerInfo(TCHAR * pResStringName,
  108. TCHAR * pRes, DWORD dwResSize)
  109. {
  110. // Extract Version informatio
  111. DWORD dwTemp, dwSize = MAX_PATH;
  112. TCHAR cName[MAX_PATH];
  113. BOOL bRet = FALSE;
  114. HINSTANCE hInst = GetModuleHandle(NULL);
  115. long lSize = GetModuleFileName(hInst, cName, MAX_PATH);
  116. if(lSize == 0)
  117. return FALSE;
  118. lSize = GetFileVersionInfoSize(cName, &dwTemp);
  119. if(lSize < 1)
  120. return FALSE;
  121. BYTE * pBlock = new BYTE[lSize];
  122. if(pBlock != NULL)
  123. {
  124. bRet = GetFileVersionInfo(cName, NULL, lSize, pBlock);
  125. if(bRet)
  126. {
  127. TCHAR lpSubBlock[MAX_PATH];
  128. TCHAR * lpBuffer = NULL;
  129. UINT wBuffSize = MAX_PATH;
  130. short * piStuff;
  131. bRet = VerQueryValue(pBlock, TEXT("\\VarFileInfo\\Translation") , (void**)&piStuff, &wBuffSize);
  132. if(bRet)
  133. {
  134. wsprintf(lpSubBlock,TEXT("\\StringFileInfo\\%04x%04x\\%ls"),piStuff[0], piStuff[1],L"ProductVersion");
  135. bRet = VerQueryValue(pBlock, lpSubBlock, (void**)&lpBuffer, &wBuffSize);
  136. }
  137. if(bRet == FALSE)
  138. {
  139. // Try again in english
  140. wsprintf(lpSubBlock,TEXT("\\StringFileInfo\\040904E4\\%ls"),pResStringName);
  141. bRet = VerQueryValue(pBlock, lpSubBlock,(void**)&lpBuffer, &wBuffSize);
  142. }
  143. if(bRet)
  144. lstrcpyn(pRes, lpBuffer, dwResSize);
  145. }
  146. delete pBlock;
  147. }
  148. return bRet;
  149. }
  150. IntString::IntString(DWORD dwID)
  151. {
  152. DWORD dwSize, dwRet;
  153. for(dwSize = 128; dwSize < 4096; dwSize *= 2)
  154. {
  155. m_pString = new TCHAR[dwSize];
  156. if(m_pString == NULL)
  157. {
  158. m_pString = &JustInCase; // should never happen!
  159. return;
  160. }
  161. dwRet = LoadString( GetModuleHandle(NULL), dwID, m_pString, dwSize);
  162. // Check for failure to load
  163. if(dwRet == 0)
  164. {
  165. delete m_pString;
  166. m_pString = &JustInCase; // should never happen!
  167. return;
  168. }
  169. // Check for the case where the buffer was too small
  170. if((dwRet + 1) >= dwSize)
  171. delete m_pString;
  172. else
  173. return; // all is well!
  174. }
  175. }
  176. IntString::~IntString()
  177. {
  178. if(m_pString != &JustInCase)
  179. delete(m_pString);
  180. }
  181. //***************************************************************************
  182. //
  183. // BOOL bGetString
  184. //
  185. // DESCRIPTION:
  186. //
  187. // Converts a command line argument into a WCHAR string. Note that the arugment is
  188. // of the form /X:stuff. This is passed a pointer to the colon.
  189. //
  190. // PARAMETERS:
  191. //
  192. // pArg Input, pointer to the colon
  193. // pOut Points the the output buffer where the data is to be copied.
  194. // IT IS ASSUMED THAT pOut points to a buffer of MAX_PATH length
  195. //
  196. //
  197. // RETURN VALUE:
  198. //
  199. // TRUE if OK
  200. //
  201. //***************************************************************************
  202. BOOL bGetString(char * pIn, WCHAR * pOut)
  203. {
  204. if(pIn == NULL)
  205. return FALSE;
  206. if(*pIn != ':')
  207. {
  208. PrintUsage();
  209. return FALSE;
  210. }
  211. pIn++; // skip passed the colon
  212. int iLen = mbstowcs(NULL, pIn, strlen(pIn)+1);
  213. if(iLen > MAX_PATH-1)
  214. {
  215. PrintUsage();
  216. return FALSE;
  217. }
  218. int iRet = mbstowcs(pOut, pIn, MAX_PATH-1);
  219. if(iRet < 1)
  220. {
  221. PrintUsage();
  222. return FALSE;
  223. }
  224. return TRUE;
  225. }
  226. //***************************************************************************
  227. //
  228. // ValidFlags.
  229. //
  230. //***************************************************************************
  231. bool ValidFlags(bool bClass, long lFlags)
  232. {
  233. if(bClass)
  234. return ((lFlags == WBEM_FLAG_CREATE_OR_UPDATE) ||
  235. (lFlags == WBEM_FLAG_UPDATE_ONLY) ||
  236. (lFlags == WBEM_FLAG_CREATE_ONLY) ||
  237. (lFlags == WBEM_FLAG_UPDATE_SAFE_MODE) ||
  238. (lFlags == WBEM_FLAG_UPDATE_FORCE_MODE) ||
  239. (lFlags == (WBEM_FLAG_UPDATE_ONLY | WBEM_FLAG_UPDATE_SAFE_MODE)) ||
  240. (lFlags == (WBEM_FLAG_UPDATE_ONLY | WBEM_FLAG_UPDATE_FORCE_MODE)));
  241. else
  242. return
  243. ((lFlags == WBEM_FLAG_CREATE_OR_UPDATE) ||
  244. (lFlags == WBEM_FLAG_UPDATE_ONLY) ||
  245. (lFlags == WBEM_FLAG_CREATE_ONLY));
  246. }
  247. HRESULT ExtractFromResource(
  248. IMofCompiler * pCompiler,
  249. LPWSTR pwsResourceName,
  250. [in, string] LPWSTR FileName,
  251. [in, string] LPWSTR ServerAndNamespace,
  252. [in, string] LPWSTR User,
  253. [in, string] LPWSTR Authority,
  254. [in, string] LPWSTR Password,
  255. [in] LONG lOptionFlags, // autocomp, check, etc
  256. [in] LONG lClassFlags,
  257. [in] LONG lInstanceFlags,
  258. [in, out] WBEM_COMPILE_STATUS_INFO * pInfo,
  259. BOOL bUseLocal,
  260. WORD wLocaleId
  261. )
  262. {
  263. pInfo->lPhaseError = 1; // 0, 1, 2, or 3 matching current return value
  264. pInfo->hRes = 0; // Actual error
  265. pInfo->ObjectNum = 0;
  266. pInfo->FirstLine = 0;
  267. pInfo->LastLine = 0;
  268. pInfo->dwOutFlags = 0;
  269. // load the driver that has the resource
  270. HRESULT hr;
  271. HINSTANCE hInst;
  272. HRSRC hSrc = NULL;
  273. HGLOBAL hResource;
  274. // load up the library as a datafile
  275. hInst = LoadLibraryEx(FileName,NULL,LOAD_LIBRARY_AS_DATAFILE);
  276. if( hInst == NULL )
  277. {
  278. pInfo->hRes = GetLastError();
  279. return pInfo->hRes;
  280. }
  281. ON_BLOCK_EXIT(FreeLibrary, hInst);
  282. // Get the handle to the resource
  283. if( bUseLocal)
  284. {
  285. hSrc = FindResourceEx(hInst,pwsResourceName, L"MOFDATA",wLocaleId);
  286. }
  287. else
  288. {
  289. hSrc = FindResource(hInst,pwsResourceName, L"MOFDATA");
  290. }
  291. if( hSrc == NULL )
  292. {
  293. pInfo->hRes = GetLastError();
  294. return pInfo->hRes;
  295. }
  296. // Get a pointer to the resource
  297. hResource = LoadResource( hInst,hSrc);
  298. if(hResource == NULL)
  299. {
  300. pInfo->hRes = GetLastError();
  301. return pInfo->hRes;
  302. }
  303. BYTE * pRes = (BYTE *)LockResource(hResource);
  304. DWORD dwSize = SizeofResource(hInst,hSrc);
  305. if(pRes == NULL)
  306. {
  307. pInfo->hRes = GetLastError();
  308. return pInfo->hRes;
  309. }
  310. // finally do the actual compile
  311. hr = pCompiler->CompileBuffer(
  312. dwSize,
  313. pRes,
  314. ServerAndNamespace,
  315. User,
  316. Authority,
  317. Password,
  318. lOptionFlags,
  319. lClassFlags,
  320. lInstanceFlags,
  321. pInfo);
  322. return hr;
  323. }