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.

445 lines
15 KiB

  1. // *********************************************************************************
  2. //
  3. // Copyright (c) Microsoft Corporation
  4. //
  5. // Module Name:
  6. //
  7. // Init.cpp
  8. //
  9. // Abstract:
  10. //
  11. // This module implements the general initialization stuff
  12. //
  13. // Author:
  14. //
  15. // Sunil G.V.N. Murali ([email protected]) 22-Dec-2000
  16. //
  17. // Revision History:
  18. //
  19. // Sunil G.V.N. Murali ([email protected]) 22-Dec-2000 : Created It.
  20. //
  21. // *********************************************************************************
  22. #include "pch.h"
  23. #include "wmi.h"
  24. #include "systeminfo.h"
  25. //
  26. // private function prototype(s)
  27. //
  28. CSystemInfo::CSystemInfo()
  29. /*++
  30. // Routine Description:
  31. // Initializes the class member variables
  32. // Arguments:
  33. // none
  34. // Return Value:
  35. // none
  36. --*/
  37. {
  38. //initialize the class member variables
  39. m_dwFormat = 0;
  40. m_bUsage = FALSE;
  41. m_pWbemLocator = NULL;
  42. m_pWbemServices = NULL;
  43. m_pAuthIdentity = NULL;
  44. m_arrData = NULL;
  45. m_bNeedPassword = FALSE;
  46. m_pColumns = FALSE;
  47. m_hOutput = NULL;
  48. }
  49. CSystemInfo::~CSystemInfo()
  50. /*++
  51. // Routine Description:
  52. // release the memory for member variables
  53. //
  54. // Arguments:none
  55. //
  56. // Return Value: none
  57. //
  58. --*/
  59. {
  60. // connection to the remote system has to be closed which is established thru win32 api
  61. if ( m_bCloseConnection == TRUE )
  62. {
  63. CloseConnection( m_strServer );
  64. }
  65. // release memory
  66. DESTROY_ARRAY( m_arrData );
  67. // release the interfaces
  68. SAFE_RELEASE( m_pWbemLocator );
  69. SAFE_RELEASE( m_pWbemServices );
  70. // release the memory allocated for output columns
  71. FreeMemory( (LPVOID *)&m_pColumns );
  72. // uninitialize the com library
  73. CoUninitialize();
  74. }
  75. BOOL
  76. CSystemInfo::Initialize()
  77. /*++
  78. // Routine Description:
  79. // Initialize the data
  80. //
  81. // Arguments: none
  82. //
  83. // Return Value:
  84. // FALSE on failure
  85. // TRUE on success
  86. --*/
  87. {
  88. //
  89. // memory allocations
  90. // allocate for storage dynamic array
  91. if ( m_arrData == NULL )
  92. {
  93. m_arrData = CreateDynamicArray();
  94. if ( m_arrData == NULL )
  95. {
  96. SetLastError( (DWORD)E_OUTOFMEMORY );
  97. SaveLastError();
  98. return FALSE;
  99. }
  100. // make the array as a 2-dimensinal array
  101. DynArrayAppendRow( m_arrData, 0 );
  102. // put the default values
  103. for( DWORD dw = 0; dw < MAX_COLUMNS; dw++ )
  104. {
  105. switch( dw )
  106. {
  107. case CI_PROCESSOR:
  108. case CI_PAGEFILE_LOCATION:
  109. case CI_HOTFIX:
  110. case CI_NETWORK_CARD:
  111. {
  112. // create the array
  113. TARRAY arr = NULL;
  114. arr = CreateDynamicArray();
  115. if ( arr == NULL )
  116. {
  117. SetLastError( (DWORD)E_OUTOFMEMORY );
  118. SaveLastError();
  119. return FALSE;
  120. }
  121. // set the default value
  122. DynArrayAppendString( arr, V_NOT_AVAILABLE, 0 );
  123. // add this array to the array
  124. DynArrayAppendEx2( m_arrData, 0, arr );
  125. // break the switch
  126. break;
  127. }
  128. default:
  129. // string type
  130. DynArrayAppendString2( m_arrData, 0, V_NOT_AVAILABLE, 0 );
  131. }
  132. }
  133. }
  134. //
  135. // allocate for output columns
  136. if ( AllocateColumns() == FALSE )
  137. {
  138. return FALSE;
  139. }
  140. //
  141. // init the console scree buffer structure to zero's
  142. // and then get the console handle and screen buffer information
  143. //
  144. // prepare for status display.
  145. // for this get a handle to the screen output buffer
  146. // but this handle will be null if the output is being redirected. so do not check
  147. // for the validity of the handle. instead try to get the console buffer information
  148. // only in case you have a valid handle to the output screen buffer
  149. //
  150. // NOTE:
  151. // here we will be dynamically decide whether to print the status messages onto STDOUT
  152. // or STDERR based on the redirection choosed by the user
  153. // By default, we will try to display the status messages onto STDOUT -- and in case
  154. // STDOUT is redirected, then we will print the messages onto STDERR
  155. // if even STDERR is redirected, then we wont display any status messages
  156. m_hOutput = NULL;
  157. SecureZeroMemory( &m_csbi, sizeof( CONSOLE_SCREEN_BUFFER_INFO ) ); // zero the memory structure
  158. // determine the redirection choice of the user and based on that get the handle to the
  159. // appropriate console file
  160. if ( IsConsoleFile( stdout ) == TRUE )
  161. {
  162. // stdout
  163. m_hOutput = GetStdHandle( STD_OUTPUT_HANDLE );
  164. }
  165. else if ( IsConsoleFile( stderr ) == TRUE )
  166. {
  167. // stderr
  168. m_hOutput = GetStdHandle( STD_ERROR_HANDLE );
  169. }
  170. // if we get any pointer to console file, then get the screen buffer info
  171. if ( m_hOutput != NULL )
  172. {
  173. GetConsoleScreenBufferInfo( m_hOutput, &m_csbi );
  174. }
  175. //
  176. // initialize the COM library
  177. if ( InitializeCom( &m_pWbemLocator ) == FALSE )
  178. {
  179. return FALSE;
  180. }
  181. // initialization is successful
  182. SetLastError( NO_ERROR ); // clear the error
  183. SetReason( L"" ); // clear the reason
  184. return TRUE;
  185. }
  186. BOOL
  187. CSystemInfo::AllocateColumns()
  188. /*++
  189. // Routine Description:
  190. // Allocates and adjust the columns to display
  191. //
  192. // Arguments: none
  193. //
  194. // Return Value:
  195. // FALSE on failure
  196. // TRUE on success
  197. //
  198. --*/
  199. {
  200. // local variables
  201. PTCOLUMNS pCurrentColumn = NULL;
  202. //
  203. // allocate memory for columns
  204. m_pColumns = (TCOLUMNS*) AllocateMemory ( MAX_COLUMNS * sizeof( TCOLUMNS ));
  205. if ( m_pColumns == NULL )
  206. {
  207. // generate error info
  208. SetLastError( (DWORD)E_OUTOFMEMORY );
  209. SaveLastError();
  210. // prepare the error message
  211. CHString strBuffer;
  212. strBuffer.Format( _T( "%s %s" ), TAG_ERROR, GetReason() );
  213. DISPLAY_MESSAGE( stderr, strBuffer );
  214. // return
  215. return FALSE;
  216. }
  217. // init with null's
  218. SecureZeroMemory( m_pColumns, sizeof( TCOLUMNS ) * MAX_COLUMNS );
  219. // host name
  220. pCurrentColumn = m_pColumns + CI_HOSTNAME;
  221. pCurrentColumn->dwWidth = COLWIDTH_HOSTNAME;
  222. pCurrentColumn->dwFlags = SR_TYPE_STRING;
  223. StringCopy( pCurrentColumn->szColumn, COLHEAD_HOSTNAME, MAX_STRING_LENGTH );
  224. // OS Name
  225. pCurrentColumn = m_pColumns + CI_OS_NAME;
  226. pCurrentColumn->dwWidth = COLWIDTH_OS_NAME;
  227. pCurrentColumn->dwFlags = SR_TYPE_STRING;
  228. StringCopy( pCurrentColumn->szColumn, COLHEAD_OS_NAME, MAX_STRING_LENGTH );
  229. // OS Version
  230. pCurrentColumn = m_pColumns + CI_OS_VERSION;
  231. pCurrentColumn->dwWidth = COLWIDTH_OS_VERSION;
  232. pCurrentColumn->dwFlags = SR_TYPE_STRING;
  233. StringCopy( pCurrentColumn->szColumn, COLHEAD_OS_VERSION, MAX_STRING_LENGTH );
  234. // OS Manufacturer
  235. pCurrentColumn = m_pColumns + CI_OS_MANUFACTURER;
  236. pCurrentColumn->dwWidth = COLWIDTH_OS_MANUFACTURER;
  237. pCurrentColumn->dwFlags = SR_TYPE_STRING;
  238. StringCopy( pCurrentColumn->szColumn, COLHEAD_OS_MANUFACTURER, MAX_STRING_LENGTH );
  239. // OS Configuration
  240. pCurrentColumn = m_pColumns + CI_OS_CONFIG;
  241. pCurrentColumn->dwWidth = COLWIDTH_OS_CONFIG;
  242. pCurrentColumn->dwFlags = SR_TYPE_STRING;
  243. StringCopy( pCurrentColumn->szColumn, COLHEAD_OS_CONFIG, MAX_STRING_LENGTH );
  244. // OS Build Type
  245. pCurrentColumn = m_pColumns + CI_OS_BUILDTYPE;
  246. pCurrentColumn->dwWidth = COLWIDTH_OS_BUILDTYPE;
  247. pCurrentColumn->dwFlags = SR_TYPE_STRING;
  248. StringCopy( pCurrentColumn->szColumn, COLHEAD_OS_BUILDTYPE, MAX_STRING_LENGTH );
  249. // Registered Owner
  250. pCurrentColumn = m_pColumns + CI_REG_OWNER;
  251. pCurrentColumn->dwWidth = COLWIDTH_REG_OWNER;
  252. pCurrentColumn->dwFlags = SR_TYPE_STRING;
  253. StringCopy( pCurrentColumn->szColumn, COLHEAD_REG_OWNER, MAX_STRING_LENGTH );
  254. // Registered Organization
  255. pCurrentColumn = m_pColumns + CI_REG_ORG;
  256. pCurrentColumn->dwWidth = COLWIDTH_REG_ORG;
  257. pCurrentColumn->dwFlags = SR_TYPE_STRING;
  258. StringCopy( pCurrentColumn->szColumn, COLHEAD_REG_ORG, MAX_STRING_LENGTH );
  259. // Product ID
  260. pCurrentColumn = m_pColumns + CI_PRODUCT_ID;
  261. pCurrentColumn->dwWidth = COLWIDTH_PRODUCT_ID;
  262. pCurrentColumn->dwFlags = SR_TYPE_STRING;
  263. StringCopy( pCurrentColumn->szColumn, COLHEAD_PRODUCT_ID, MAX_STRING_LENGTH );
  264. // install date
  265. pCurrentColumn = m_pColumns + CI_INSTALL_DATE;
  266. pCurrentColumn->dwWidth = COLWIDTH_INSTALL_DATE;
  267. pCurrentColumn->dwFlags = SR_TYPE_STRING;
  268. StringCopy( pCurrentColumn->szColumn, COLHEAD_INSTALL_DATE, MAX_STRING_LENGTH );
  269. // system up time
  270. pCurrentColumn = m_pColumns + CI_SYSTEM_UPTIME;
  271. pCurrentColumn->dwWidth = COLWIDTH_SYSTEM_UPTIME;
  272. pCurrentColumn->dwFlags = SR_TYPE_STRING;
  273. StringCopy( pCurrentColumn->szColumn, COLHEAD_SYSTEM_UPTIME, MAX_STRING_LENGTH );
  274. // system manufacturer
  275. pCurrentColumn = m_pColumns + CI_SYSTEM_MANUFACTURER;
  276. pCurrentColumn->dwWidth = COLWIDTH_SYSTEM_MANUFACTURER;
  277. pCurrentColumn->dwFlags = SR_TYPE_STRING;
  278. StringCopy( pCurrentColumn->szColumn, COLHEAD_SYSTEM_MANUFACTURER, MAX_STRING_LENGTH );
  279. // system model
  280. pCurrentColumn = m_pColumns + CI_SYSTEM_MODEL;
  281. pCurrentColumn->dwWidth = COLWIDTH_SYSTEM_MODEL;
  282. pCurrentColumn->dwFlags = SR_TYPE_STRING;
  283. StringCopy( pCurrentColumn->szColumn, COLHEAD_SYSTEM_MODEL, MAX_STRING_LENGTH );
  284. // system type
  285. pCurrentColumn = m_pColumns + CI_SYSTEM_TYPE;
  286. pCurrentColumn->dwWidth = COLWIDTH_SYSTEM_TYPE;
  287. pCurrentColumn->dwFlags = SR_TYPE_STRING;
  288. StringCopy( pCurrentColumn->szColumn, COLHEAD_SYSTEM_TYPE, MAX_STRING_LENGTH );
  289. // processor
  290. pCurrentColumn = m_pColumns + CI_PROCESSOR;
  291. pCurrentColumn->dwWidth = COLWIDTH_PROCESSOR;
  292. pCurrentColumn->dwFlags = SR_ARRAY | SR_TYPE_STRING;
  293. StringCopy( pCurrentColumn->szColumn, COLHEAD_PROCESSOR, MAX_STRING_LENGTH );
  294. // bios version
  295. pCurrentColumn = m_pColumns + CI_BIOS_VERSION;
  296. pCurrentColumn->dwWidth = COLWIDTH_BIOS_VERSION;
  297. pCurrentColumn->dwFlags = SR_TYPE_STRING;
  298. StringCopy( pCurrentColumn->szColumn, COLHEAD_BIOS_VERSION, MAX_STRING_LENGTH );
  299. // windows directory
  300. pCurrentColumn = m_pColumns + CI_WINDOWS_DIRECTORY;
  301. pCurrentColumn->dwWidth = COLWIDTH_WINDOWS_DIRECTORY;
  302. pCurrentColumn->dwFlags = SR_TYPE_STRING;
  303. StringCopy( pCurrentColumn->szColumn, COLHEAD_WINDOWS_DIRECTORY, MAX_STRING_LENGTH );
  304. // system directory
  305. pCurrentColumn = m_pColumns + CI_SYSTEM_DIRECTORY;
  306. pCurrentColumn->dwWidth = COLWIDTH_SYSTEM_DIRECTORY;
  307. pCurrentColumn->dwFlags = SR_TYPE_STRING;
  308. StringCopy( pCurrentColumn->szColumn, COLHEAD_SYSTEM_DIRECTORY, MAX_STRING_LENGTH );
  309. // boot device
  310. pCurrentColumn = m_pColumns + CI_BOOT_DEVICE;
  311. pCurrentColumn->dwWidth = COLWIDTH_BOOT_DEVICE;
  312. pCurrentColumn->dwFlags = SR_TYPE_STRING;
  313. StringCopy( pCurrentColumn->szColumn, COLHEAD_BOOT_DEVICE, MAX_STRING_LENGTH );
  314. // system locale
  315. pCurrentColumn = m_pColumns + CI_SYSTEM_LOCALE;
  316. pCurrentColumn->dwWidth = COLWIDTH_SYSTEM_LOCALE;
  317. pCurrentColumn->dwFlags = SR_TYPE_STRING;
  318. StringCopy( pCurrentColumn->szColumn, COLHEAD_SYSTEM_LOCALE, MAX_STRING_LENGTH );
  319. // input locale
  320. pCurrentColumn = m_pColumns + CI_INPUT_LOCALE;
  321. pCurrentColumn->dwWidth = COLWIDTH_INPUT_LOCALE;
  322. pCurrentColumn->dwFlags = SR_TYPE_STRING;
  323. StringCopy( pCurrentColumn->szColumn, COLHEAD_INPUT_LOCALE, MAX_STRING_LENGTH );
  324. // time zone
  325. pCurrentColumn = m_pColumns + CI_TIME_ZONE;
  326. pCurrentColumn->dwWidth = COLWIDTH_TIME_ZONE;
  327. pCurrentColumn->dwFlags = SR_TYPE_STRING;
  328. StringCopy( pCurrentColumn->szColumn, COLHEAD_TIME_ZONE, MAX_STRING_LENGTH );
  329. // total physical memory
  330. pCurrentColumn = m_pColumns + CI_TOTAL_PHYSICAL_MEMORY;
  331. pCurrentColumn->dwWidth = COLWIDTH_TOTAL_PHYSICAL_MEMORY;
  332. pCurrentColumn->dwFlags = SR_TYPE_STRING;
  333. StringCopy( pCurrentColumn->szColumn, COLHEAD_TOTAL_PHYSICAL_MEMORY, MAX_STRING_LENGTH );
  334. // available physical memory
  335. pCurrentColumn = m_pColumns + CI_AVAILABLE_PHYSICAL_MEMORY;
  336. pCurrentColumn->dwWidth = COLWIDTH_AVAILABLE_PHYSICAL_MEMORY;
  337. pCurrentColumn->dwFlags = SR_TYPE_STRING;
  338. StringCopy( pCurrentColumn->szColumn, COLHEAD_AVAILABLE_PHYSICAL_MEMORY, MAX_STRING_LENGTH );
  339. // virtual memory max
  340. pCurrentColumn = m_pColumns + CI_VIRTUAL_MEMORY_MAX;
  341. pCurrentColumn->dwWidth = COLWIDTH_VIRTUAL_MEMORY_MAX;
  342. pCurrentColumn->dwFlags = SR_TYPE_STRING;
  343. StringCopy( pCurrentColumn->szColumn, COLHEAD_VIRTUAL_MEMORY_MAX, MAX_STRING_LENGTH );
  344. // virtual memory available
  345. pCurrentColumn = m_pColumns + CI_VIRTUAL_MEMORY_AVAILABLE;
  346. pCurrentColumn->dwWidth = COLWIDTH_VIRTUAL_MEMORY_AVAILABLE;
  347. pCurrentColumn->dwFlags = SR_TYPE_STRING;
  348. StringCopy( pCurrentColumn->szColumn, COLHEAD_VIRTUAL_MEMORY_AVAILABLE, MAX_STRING_LENGTH );
  349. // virtual memory usage
  350. pCurrentColumn = m_pColumns + CI_VIRTUAL_MEMORY_INUSE;
  351. pCurrentColumn->dwWidth = COLWIDTH_VIRTUAL_MEMORY_INUSE;
  352. pCurrentColumn->dwFlags = SR_TYPE_STRING;
  353. StringCopy( pCurrentColumn->szColumn, COLHEAD_VIRTUAL_MEMORY_INUSE, MAX_STRING_LENGTH );
  354. // page file location
  355. pCurrentColumn = m_pColumns + CI_PAGEFILE_LOCATION;
  356. pCurrentColumn->dwWidth = COLWIDTH_PAGEFILE_LOCATION;
  357. pCurrentColumn->dwFlags = SR_ARRAY | SR_TYPE_STRING;
  358. StringCopy( pCurrentColumn->szColumn, COLHEAD_PAGEFILE_LOCATION, MAX_STRING_LENGTH );
  359. // domain
  360. pCurrentColumn = m_pColumns + CI_DOMAIN;
  361. pCurrentColumn->dwWidth = COLWIDTH_DOMAIN;
  362. pCurrentColumn->dwFlags = SR_TYPE_STRING;
  363. StringCopy( pCurrentColumn->szColumn, COLHEAD_DOMAIN, MAX_STRING_LENGTH );
  364. // logon server
  365. pCurrentColumn = m_pColumns + CI_LOGON_SERVER;
  366. pCurrentColumn->dwWidth = COLWIDTH_LOGON_SERVER;
  367. pCurrentColumn->dwFlags = SR_TYPE_STRING;
  368. StringCopy( pCurrentColumn->szColumn, COLHEAD_LOGON_SERVER, MAX_STRING_LENGTH );
  369. // hotfix
  370. pCurrentColumn = m_pColumns + CI_HOTFIX;
  371. pCurrentColumn->dwWidth = COLWIDTH_HOTFIX;
  372. pCurrentColumn->dwFlags = SR_ARRAY | SR_TYPE_STRING;
  373. StringCopy( pCurrentColumn->szColumn, COLHEAD_HOTFIX, MAX_STRING_LENGTH );
  374. // network card
  375. pCurrentColumn = m_pColumns + CI_NETWORK_CARD;
  376. pCurrentColumn->dwWidth = COLWIDTH_NETWORK_CARD;
  377. pCurrentColumn->dwFlags = SR_ARRAY | SR_TYPE_STRING;
  378. StringCopy( pCurrentColumn->szColumn, COLHEAD_NETWORK_CARD, MAX_STRING_LENGTH );
  379. // return success
  380. return TRUE;
  381. }