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.

400 lines
15 KiB

  1. /*++
  2. Copyright (c) 1997-1999 Microsoft Corporation
  3. Module Name:
  4. mofapi.c
  5. Abstract:
  6. WMI MOF access apis
  7. Author:
  8. 16-Jan-1997 AlanWar
  9. Revision History:
  10. --*/
  11. #include "wmiump.h"
  12. #include "common.h"
  13. #include "request.h"
  14. ULONG
  15. WMIAPI
  16. WmiMofEnumerateResourcesA(
  17. IN MOFHANDLE MofResourceHandle,
  18. OUT ULONG *MofResourceCount,
  19. OUT PMOFRESOURCEINFOA *MofResourceInfo
  20. )
  21. /*++
  22. Routine Description:
  23. ANSI thunk to WMIMofEnumerateResourcesA
  24. --*/
  25. {
  26. ULONG Status;
  27. PMOFRESOURCEINFOW MofResourceInfoUnicode;
  28. PMOFRESOURCEINFOA MofResourceInfoAnsi;
  29. PCHAR AnsiPtr;
  30. PCHAR Ansi;
  31. ULONG i, AnsiSize, AnsiStructureSize;
  32. ULONG MofResourceCountUnicode;
  33. ULONG AnsiLen;
  34. ULONG AnsiImagePathSize;
  35. ULONG AnsiResourceNameSize;
  36. EtwpInitProcessHeap();
  37. Status = WmiMofEnumerateResourcesW(MofResourceHandle,
  38. &MofResourceCountUnicode,
  39. &MofResourceInfoUnicode);
  40. if (Status == ERROR_SUCCESS)
  41. {
  42. //
  43. // Walk the unicode MOFRESOURCEINFOW to determine the ansi size needed
  44. // for all of the ansi MOFRESOURCEINFOA structures and strings. We
  45. // determine the entire size and allocate a single block that holds
  46. // all of it since that is what WMIMofEnumerateResourceInfoW does.
  47. AnsiStructureSize = MofResourceCountUnicode * sizeof(MOFRESOURCEINFOA);
  48. AnsiSize = AnsiStructureSize;
  49. for (i = 0; i < MofResourceCountUnicode; i++)
  50. {
  51. Status = AnsiSizeForUnicodeString(MofResourceInfoUnicode[i].ImagePath,
  52. &AnsiImagePathSize);
  53. if (Status != ERROR_SUCCESS)
  54. {
  55. goto Done;
  56. }
  57. Status = AnsiSizeForUnicodeString(MofResourceInfoUnicode[i].ResourceName,
  58. &AnsiResourceNameSize);
  59. if (Status != ERROR_SUCCESS)
  60. {
  61. goto Done;
  62. }
  63. AnsiSize += AnsiImagePathSize + AnsiResourceNameSize;
  64. }
  65. MofResourceInfoAnsi = EtwpAlloc(AnsiSize);
  66. if (MofResourceInfoAnsi != NULL)
  67. {
  68. AnsiPtr = (PCHAR)((PUCHAR)MofResourceInfoAnsi + AnsiStructureSize);
  69. for (i = 0; i < MofResourceCountUnicode; i++)
  70. {
  71. MofResourceInfoAnsi[i].ResourceSize = MofResourceInfoUnicode[i].ResourceSize;
  72. MofResourceInfoAnsi[i].ResourceBuffer = MofResourceInfoUnicode[i].ResourceBuffer;
  73. MofResourceInfoAnsi[i].ImagePath = AnsiPtr;
  74. Status = UnicodeToAnsi(MofResourceInfoUnicode[i].ImagePath,
  75. &MofResourceInfoAnsi[i].ImagePath,
  76. &AnsiLen);
  77. if (Status != ERROR_SUCCESS)
  78. {
  79. break;
  80. }
  81. AnsiPtr += AnsiLen;
  82. MofResourceInfoAnsi[i].ResourceName = AnsiPtr;
  83. Status = UnicodeToAnsi(MofResourceInfoUnicode[i].ResourceName,
  84. &MofResourceInfoAnsi[i].ResourceName,
  85. &AnsiLen);
  86. if (Status != ERROR_SUCCESS)
  87. {
  88. break;
  89. }
  90. AnsiPtr += AnsiLen;
  91. }
  92. if (Status == ERROR_SUCCESS)
  93. {
  94. try
  95. {
  96. *MofResourceInfo = MofResourceInfoAnsi;
  97. } except(EXCEPTION_EXECUTE_HANDLER) {
  98. Status = ERROR_NOACCESS;
  99. EtwpFree(MofResourceInfoAnsi);
  100. }
  101. }
  102. } else {
  103. //
  104. // Not enough memory for ansi thunking so free unicode
  105. // mof class info and return an error.
  106. Status = ERROR_NOT_ENOUGH_MEMORY;
  107. }
  108. Done:
  109. WmiFreeBuffer(MofResourceInfoUnicode);
  110. }
  111. SetLastError(Status);
  112. return(Status);
  113. }
  114. ULONG
  115. WmiMofEnumerateResourcesW(
  116. IN MOFHANDLE MofResourceHandle,
  117. OUT ULONG *MofResourceCount,
  118. OUT PMOFRESOURCEINFOW *MofResourceInfo
  119. )
  120. /*++
  121. Routine Description:
  122. This routine will enumerate one or all of the MOF resources that are
  123. registered with WMI.
  124. Arguments:
  125. MofResourceHandle is reserved and must be 0
  126. *MofResourceCount returns with the count of MOFRESOURCEINFO structures
  127. returned in *MofResourceInfo.
  128. *MofResourceInfo returns with a pointer to an array of MOFRESOURCEINFO
  129. structures. The caller MUST call WMIFreeBuffer with *MofResourceInfo
  130. in order to ensure that there are no memory leaks.
  131. Return Value:
  132. ERROR_SUCCESS or an error code
  133. --*/
  134. {
  135. ULONG Status, SubStatus;
  136. PWMIMOFLIST MofList;
  137. ULONG MofListCount;
  138. ULONG MRInfoSize;
  139. ULONG MRCount;
  140. PWCHAR MRBuffer;
  141. PMOFRESOURCEINFOW MRInfo;
  142. PWCHAR RegPath, ResName, ImagePath;
  143. PWMIMOFENTRY MofEntry;
  144. ULONG i, j;
  145. PWCHAR *LanguageList;
  146. ULONG LanguageCount;
  147. BOOLEAN b;
  148. ULONG HeaderLen;
  149. ULONG MRBufferRemaining;
  150. PWCHAR ResourcePtr;
  151. ULONG BufferUsed;
  152. PWCHAR ImagePathStatic;
  153. EtwpInitProcessHeap();
  154. if (MofResourceHandle != 0)
  155. {
  156. SetLastError(ERROR_INVALID_PARAMETER);
  157. return(ERROR_INVALID_PARAMETER);
  158. }
  159. ImagePathStatic = EtwpAlloc(MAX_PATH * sizeof(WCHAR));
  160. if (ImagePathStatic != NULL)
  161. {
  162. *MofResourceInfo = NULL;
  163. Status = EtwpGetMofResourceList(&MofList);
  164. if (Status == ERROR_SUCCESS)
  165. {
  166. //
  167. // Ok, we have got a valid list of mofs. Now we need to
  168. // loop over them all and convert the regpaths into image
  169. // paths
  170. //
  171. Status = EtwpGetLanguageList(&LanguageList,
  172. &LanguageCount);
  173. if (Status == ERROR_SUCCESS)
  174. {
  175. MofListCount = MofList->MofListCount;
  176. //
  177. // Take a guess as to the size of the buffer needed to
  178. // satisfy the complete list of mof resources
  179. //
  180. HeaderLen = (MofListCount * (LanguageCount+1)) *
  181. sizeof(MOFRESOURCEINFOW);
  182. #if DBG
  183. MRInfoSize = HeaderLen + 2 * (MAX_PATH * sizeof(WCHAR));
  184. #else
  185. MRInfoSize = HeaderLen + (2*MofListCount * (MAX_PATH * sizeof(WCHAR)));
  186. #endif
  187. MRInfo = NULL;
  188. do
  189. {
  190. TryAgain:
  191. if (MRInfo != NULL)
  192. {
  193. EtwpDebugPrint(("WMI: MofList was too small, retry 0x%x bytes\n",
  194. MRInfoSize));
  195. EtwpFree(MRInfo);
  196. }
  197. MRInfo = EtwpAlloc(MRInfoSize);
  198. if (MRInfo != NULL)
  199. {
  200. memset(MRInfo, 0, MRInfoSize);
  201. MRBuffer = (PWCHAR)OffsetToPtr(MRInfo, HeaderLen);
  202. MRBufferRemaining = (MRInfoSize - HeaderLen) / sizeof(WCHAR);
  203. MRCount = 0;
  204. for (i = 0; i < MofListCount; i++)
  205. {
  206. //
  207. // Pull out thee image path and resource names
  208. //
  209. MofEntry = &MofList->MofEntry[i];
  210. RegPath = (PWCHAR)OffsetToPtr(MofList, MofEntry->RegPathOffset);
  211. ResName = (PWCHAR)OffsetToPtr(MofList, MofEntry->ResourceOffset);
  212. if (*ResName != 0)
  213. {
  214. if ((MofEntry->Flags & WMIMOFENTRY_FLAG_USERMODE) == 0)
  215. {
  216. ImagePath = EtwpRegistryToImagePath(ImagePathStatic,
  217. RegPath);
  218. } else {
  219. ImagePath = RegPath;
  220. }
  221. if (ImagePath != NULL)
  222. {
  223. //
  224. // If we've got a valid image path then
  225. // out it and the resource name into the
  226. // output buffer
  227. //
  228. MRInfo[MRCount].ImagePath = MRBuffer;
  229. b = EtwpCopyMRString(MRBuffer,
  230. MRBufferRemaining,
  231. &BufferUsed,
  232. ImagePath);
  233. if (! b)
  234. {
  235. //
  236. // The buffer was not big enough so we
  237. // double the size used and try again
  238. //
  239. MRInfoSize *= 2;
  240. goto TryAgain;
  241. }
  242. MRBuffer += BufferUsed;
  243. MRBufferRemaining -= BufferUsed;
  244. EtwpDebugPrint(("WMI: Add ImagePath %p (%ws) to MRList at position %d\n",
  245. MRInfo[MRCount].ImagePath,
  246. MRInfo[MRCount].ImagePath,
  247. MRCount));
  248. MRInfo[MRCount].ResourceName = MRBuffer;
  249. ResourcePtr = MRBuffer;
  250. b = EtwpCopyMRString(MRBuffer,
  251. MRBufferRemaining,
  252. &BufferUsed,
  253. ResName);
  254. if (! b)
  255. {
  256. //
  257. // The buffer was not big enough so we
  258. // double the size used and try again
  259. //
  260. MRInfoSize *= 2;
  261. goto TryAgain;
  262. }
  263. MRBuffer += BufferUsed;
  264. MRBufferRemaining -= BufferUsed;
  265. EtwpDebugPrint(("WMI: Add Resource %p (%ws) to MRList at position %d\n",
  266. MRInfo[MRCount].ResourceName,
  267. MRInfo[MRCount].ResourceName,
  268. MRCount));
  269. MRCount++;
  270. for (j = 0; j < LanguageCount; j++)
  271. {
  272. MRInfo[MRCount].ImagePath = MRBuffer;
  273. SubStatus = EtwpBuildMUIPath(MRBuffer,
  274. MRBufferRemaining,
  275. &BufferUsed,
  276. ImagePath,
  277. LanguageList[j],
  278. &b);
  279. if (SubStatus == ERROR_SUCCESS)
  280. {
  281. if (! b)
  282. {
  283. //
  284. // The buffer was not big enough so we
  285. // double the size used and try again
  286. //
  287. MRInfoSize *= 2;
  288. goto TryAgain;
  289. }
  290. MRBuffer += BufferUsed;
  291. MRBufferRemaining -= BufferUsed;
  292. EtwpDebugPrint(("WMI: Add ImagePath %p (%ws) to MRList at position %d\n",
  293. MRInfo[MRCount].ImagePath,
  294. MRInfo[MRCount].ImagePath,
  295. MRCount));
  296. //
  297. // We did find a MUI resource
  298. // so add it to the list
  299. //
  300. MRInfo[MRCount].ResourceName = ResourcePtr;
  301. EtwpDebugPrint(("WMI: Add Resource %p (%ws) to MRList at position %d\n",
  302. MRInfo[MRCount].ResourceName,
  303. MRInfo[MRCount].ResourceName,
  304. MRCount));
  305. MRCount++;
  306. }
  307. }
  308. }
  309. }
  310. }
  311. } else {
  312. Status = STATUS_INSUFFICIENT_RESOURCES;
  313. }
  314. } while (FALSE);
  315. //
  316. // Free up memory used to hold the language list
  317. //
  318. for (i = 0; i < LanguageCount; i++)
  319. {
  320. EtwpFree(LanguageList[i]);
  321. }
  322. EtwpFree(LanguageList);
  323. *MofResourceCount = MRCount;
  324. *MofResourceInfo = MRInfo;
  325. }
  326. EtwpFree(MofList);
  327. }
  328. EtwpFree(ImagePathStatic);
  329. } else {
  330. Status = ERROR_NOT_ENOUGH_MEMORY;
  331. }
  332. SetLastError(Status);
  333. return(Status);
  334. }