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.

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