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.

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