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.

2809 lines
93 KiB

  1. // *********************************************************************************
  2. //
  3. // Copyright (c) Microsoft Corporation
  4. //
  5. // Module Name:
  6. //
  7. // DriverQuery.cpp
  8. //
  9. // Abstract:
  10. //
  11. // This modules Queries the information of the various drivers present in the
  12. // system .
  13. //
  14. // Syntax:
  15. // ------
  16. // DriverQuery [-s server ] [-u [domain\]username [-p password]]
  17. // [-fo format ] [-n|noheader ] [-v]
  18. //
  19. // Author:
  20. //
  21. // J.S.Vasu ([email protected])
  22. //
  23. // Revision History:
  24. //
  25. // Created on 31-0ct-2000 by J.S.Vasu
  26. // Modified on 9-Dec-2000 by Santhosh Brahmappa Added a new function IsWin64()
  27. //
  28. //
  29. // *********************************************************************************
  30. //
  31. #include "pch.h"
  32. #include "Resource.h"
  33. #include "LOCALE.H"
  34. #include "shlwapi.h"
  35. #include "DriverQuery.h"
  36. #ifndef _WIN64
  37. BOOL IsWin64(void);
  38. #define countof(x) (sizeof(x) / sizeof((x)[0]))
  39. #endif
  40. // function prototypes
  41. LCID GetSupportedUserLocale( BOOL& bLocaleChanged );
  42. #define MAX_ENUM_DRIVERS 10
  43. DWORD
  44. _cdecl wmain(
  45. IN DWORD argc,
  46. IN LPCTSTR argv[]
  47. )
  48. /*++
  49. Routine Description:
  50. This the entry point to this utility.
  51. Arguments:
  52. [ in ] argc : argument(s) count specified at the command prompt
  53. [ in ] argv : argument(s) specified at the command prompt
  54. Return Value:
  55. 0 : If the utility successfully displayed the driver information.
  56. 1 : If the utility completely failed to display the driver information
  57. --*/
  58. {
  59. BOOL bResult = FALSE ;
  60. BOOL bNeedPassword = FALSE ;
  61. BOOL bUsage = FALSE ;
  62. BOOL bHeader= FALSE ;
  63. LPTSTR szUserName = NULL ;
  64. LPTSTR szPassword = NULL;
  65. LPTSTR szServer = NULL ;
  66. WCHAR szTmpPassword[MAX_RES_STRING]= NULL_STRING;
  67. __MAX_SIZE_STRING szFormat = NULL_STRING ;
  68. DWORD dwSystemType = 0;
  69. HRESULT hQueryResult = S_OK ;
  70. DWORD dwExitCode = 0;
  71. DWORD dwErrCode = 0;
  72. BOOL bLocalFlag = TRUE ;
  73. BOOL bLocalSystem = TRUE;
  74. BOOL bVerbose = FALSE ;
  75. BOOL bComInitFlag = FALSE ;
  76. IWbemLocator* pIWbemLocator = NULL ;
  77. IWbemServices* pIWbemServReg = NULL ;
  78. LPCTSTR szToken = NULL ;
  79. COAUTHIDENTITY *pAuthIdentity = NULL;
  80. BOOL bFlag = FALSE ;
  81. BOOL bSigned = FALSE ;
  82. CHString strUserName = NULL_STRING;
  83. CHString strPassword = NULL_STRING;
  84. CHString strMachineName = NULL_STRING;
  85. _tsetlocale( LC_ALL, _T(""));
  86. bResult = ProcessOptions(argc,argv,&bUsage,&szServer,&szUserName,szTmpPassword,szFormat,
  87. &bHeader,&bNeedPassword,&bVerbose,&bSigned);
  88. if(bResult == FALSE)
  89. {
  90. ShowMessage(stderr,GetReason());
  91. ReleaseGlobals();
  92. FreeMemory((LPVOID*) &szUserName );
  93. FreeMemory((LPVOID*) &szServer );
  94. WbemFreeAuthIdentity( &pAuthIdentity );
  95. return(EXIT_FAILURE);
  96. }
  97. // check if the help option has been specified.
  98. if(bUsage==TRUE)
  99. {
  100. ShowUsage() ;
  101. ReleaseGlobals();
  102. FreeMemory((LPVOID*) &szUserName );
  103. FreeMemory((LPVOID*) &szServer );
  104. WbemFreeAuthIdentity( &pAuthIdentity );
  105. return(EXIT_SUCCESS);
  106. }
  107. bComInitFlag = InitialiseCom(&pIWbemLocator);
  108. if(bComInitFlag == FALSE )
  109. {
  110. CloseConnection(szServer);
  111. ReleaseGlobals();
  112. FreeMemory((LPVOID*) &szUserName );
  113. FreeMemory((LPVOID*) &szServer );
  114. WbemFreeAuthIdentity( &pAuthIdentity );
  115. return(EXIT_FAILURE);
  116. }
  117. try
  118. {
  119. strUserName = (LPCWSTR) szUserName ;
  120. strMachineName = (LPCWSTR) szServer ;
  121. strPassword = (LPCWSTR)szTmpPassword ;
  122. bFlag = ConnectWmiEx( pIWbemLocator, &pIWbemServReg, strMachineName,
  123. strUserName, strPassword, &pAuthIdentity, bNeedPassword, DEFAULT_NAMESPACE, &bLocalSystem );
  124. //if unable to connect to wmi exit failure
  125. if( bFlag == FALSE )
  126. {
  127. SAFEIRELEASE( pIWbemLocator);
  128. WbemFreeAuthIdentity( &pAuthIdentity );
  129. FreeMemory((LPVOID*) &szUserName );
  130. FreeMemory((LPVOID*) &szServer );
  131. CoUninitialize();
  132. ReleaseGlobals();
  133. return( EXIT_FAILURE );
  134. }
  135. //free the memory of these variables
  136. FreeMemory((LPVOID*) &szUserName );
  137. FreeMemory((LPVOID*) &szServer );
  138. //get the changed server name, user name, password back
  139. szUserName = strUserName.GetBuffer(strUserName.GetLength());
  140. szPassword = strPassword.GetBuffer(strPassword.GetLength()) ;
  141. szServer = strMachineName.GetBuffer(strPassword.GetLength());
  142. }
  143. catch(CHeap_Exception)
  144. {
  145. SetLastError( E_OUTOFMEMORY );
  146. SaveLastError();
  147. ShowLastErrorEx( stdout, SLE_TYPE_ERROR | SLE_INTERNAL );
  148. WbemFreeAuthIdentity( &pAuthIdentity );
  149. CoUninitialize();
  150. ReleaseGlobals();
  151. return( EXIT_FAILURE );
  152. }
  153. //display warning message if local credentials supplied
  154. if( ( IsLocalSystem( szServer ) == TRUE )&&(StringLengthW(szUserName, 0)!=0) )
  155. {
  156. ShowMessage(stderr,GetResString(IDS_IGNORE_LOCAL_CRED));
  157. }
  158. // establish connection to remote system by using win32api function
  159. if ( bLocalSystem == FALSE )
  160. {
  161. LPCWSTR pwszUser = NULL;
  162. LPCWSTR pwszPassword = NULL;
  163. // identify the password to connect to the remote system
  164. if ( pAuthIdentity != NULL )
  165. {
  166. pwszPassword = pAuthIdentity->Password;
  167. if ( strUserName.GetLength() != 0 )
  168. pwszUser =(LPCWSTR) strUserName;
  169. }
  170. DWORD dwConnect = 0 ;
  171. dwConnect = ConnectServer( (LPCWSTR)strMachineName, pwszUser, pwszPassword );
  172. if(dwConnect !=NO_ERROR )
  173. {
  174. dwErrCode = GetLastError();
  175. if(dwErrCode == ERROR_SESSION_CREDENTIAL_CONFLICT)
  176. {
  177. ShowLastErrorEx( stderr, SLE_TYPE_WARNING | SLE_INTERNAL );
  178. }
  179. else if( dwConnect == ERROR_EXTENDED_ERROR )
  180. {
  181. ShowLastErrorEx( stdout, SLE_TYPE_ERROR | SLE_INTERNAL );
  182. WbemFreeAuthIdentity( &pAuthIdentity );
  183. strMachineName.ReleaseBuffer();
  184. strUserName.ReleaseBuffer();
  185. strPassword.ReleaseBuffer();
  186. CoUninitialize();
  187. ReleaseGlobals();
  188. return( EXIT_FAILURE );
  189. }
  190. else
  191. {
  192. SetLastError( dwConnect );
  193. SaveLastError();
  194. ShowLastErrorEx( stdout, SLE_TYPE_ERROR | SLE_INTERNAL );
  195. WbemFreeAuthIdentity( &pAuthIdentity );
  196. strMachineName.ReleaseBuffer();
  197. strUserName.ReleaseBuffer();
  198. strPassword.ReleaseBuffer();
  199. CoUninitialize();
  200. ReleaseGlobals();
  201. return( EXIT_FAILURE );
  202. }
  203. }
  204. else
  205. {
  206. bLocalFlag = FALSE ;
  207. }
  208. }
  209. else
  210. {
  211. StringCopy( szServer, _T( "" ), StringLengthW(szServer, 0) );
  212. }
  213. hQueryResult = QueryDriverInfo(szServer, szUserName,szPassword,szFormat,bHeader,bVerbose,pIWbemLocator,pAuthIdentity,pIWbemServReg,bSigned);
  214. if((DWORD)hQueryResult == FAILURE)
  215. {
  216. // close connection to the specified system and exit with failure.
  217. if (bLocalFlag == FALSE )
  218. {
  219. CloseConnection(szServer);
  220. }
  221. WbemFreeAuthIdentity( &pAuthIdentity );
  222. strMachineName.ReleaseBuffer();
  223. strUserName.ReleaseBuffer();
  224. strPassword.ReleaseBuffer();
  225. CoUninitialize();
  226. ReleaseGlobals();
  227. return(EXIT_FAILURE);
  228. }
  229. // close connection to the specified system and exit
  230. if (bLocalFlag == FALSE )
  231. {
  232. CloseConnection(szServer);
  233. }
  234. strMachineName.ReleaseBuffer();
  235. strUserName.ReleaseBuffer();
  236. strPassword.ReleaseBuffer();
  237. WbemFreeAuthIdentity( &pAuthIdentity );
  238. SAFEIRELEASE( pIWbemLocator);
  239. CoUninitialize();
  240. ReleaseGlobals();
  241. return (EXIT_SUCCESS);
  242. }
  243. void ShowUsage()
  244. /*++
  245. Routine Description:
  246. This function fetches usage information from resource file and displays it
  247. Arguments:
  248. None
  249. Return Value:
  250. None
  251. --*/
  252. {
  253. DWORD dwIndex = ID_USAGE_BEGIN;
  254. for(;dwIndex<=ID_USAGE_ENDING; dwIndex++)
  255. {
  256. ShowMessage(stdout,GetResString( dwIndex ));
  257. }
  258. }
  259. DWORD
  260. QueryDriverInfo(
  261. IN LPWSTR szServer,
  262. IN LPWSTR szUserName,
  263. IN LPWSTR szPassword,
  264. IN LPWSTR szFormat,
  265. IN BOOL bHeader,
  266. IN BOOL bVerbose,
  267. IN IWbemLocator* pIWbemLocator,
  268. IN COAUTHIDENTITY* pAuthIdentity,
  269. IN IWbemServices* pIWbemServReg,
  270. IN BOOL bSigned
  271. )
  272. /**+
  273. Routine Description:
  274. This function queries the driverinfo of the specified system by connecting to WMI
  275. Arguments:
  276. [ in ] szServer : server name on which DriverInformation has to be queried.
  277. [ in ] szUserName : User name for whom DriverInformation has to be queried.
  278. [ in ] szPassword : Password for the user
  279. [ in ] szFormat : Format in which the results are to be displayed.
  280. [ in ] bHeader : Boolean indicating if the header is required.
  281. [ in ] bVerbose : Boolean indicating if the output is to be in verbose mode or not.
  282. [ in ] IWbemLocater : Pointer to IWbemLocater.
  283. [ in ] pAuthIdentity : Pointer to AuthIdentity structure.
  284. [ in ] IWbemServReg : Pointer to IWbemServices object
  285. [ in ] bSigned : Boolean variable indicating if signed drivers to show or not
  286. Return Value:
  287. SUCCESS : if the function is successful in querying
  288. FAILURE : if the function is unsuccessful in querying.
  289. --*/
  290. {
  291. HRESULT hRes = S_OK ;
  292. HRESULT hConnect = S_OK;
  293. _bstr_t bstrUserName ;
  294. _bstr_t bstrPassword ;
  295. _bstr_t bstrNamespace ;
  296. _bstr_t bstrServer ;
  297. DWORD dwProcessResult = 0;
  298. DWORD dwSystemType = 0 ;
  299. LPTSTR lpMsgBuf = NULL;
  300. IWbemServices *pIWbemServices = NULL;
  301. IEnumWbemClassObject *pSystemSet = NULL;
  302. HRESULT hISecurity = S_FALSE;
  303. HANDLE h_Mutex = 0;
  304. try
  305. {
  306. bstrNamespace = CIMV2_NAMESPACE ;
  307. bstrServer = szServer ;
  308. }
  309. catch(...)
  310. {
  311. ShowMessage( stderr,ERROR_RETREIVE_INFO);
  312. return FAILURE;
  313. }
  314. if ( IsLocalSystem( szServer ) == FALSE )
  315. {
  316. try
  317. {
  318. //appending UNC paths to form the complete path.
  319. bstrNamespace = TOKEN_BACKSLASH2 + _bstr_t( szServer ) + TOKEN_BACKSLASH + CIMV2_NAMESPACE;
  320. // if user name is specified then only take user name and password
  321. if ( StringLengthW( szUserName, 0 ) != 0 )
  322. {
  323. bstrUserName = szUserName;
  324. if (StringLengthW(szPassword, 0)==0)
  325. {
  326. bstrPassword = L"";
  327. }
  328. else
  329. {
  330. bstrPassword = szPassword ;
  331. }
  332. }
  333. }
  334. catch(...)
  335. {
  336. ShowMessage( stderr,ERROR_RETREIVE_INFO);
  337. return FAILURE;
  338. }
  339. }
  340. dwSystemType = GetSystemType(pAuthIdentity,pIWbemServReg);
  341. if (dwSystemType == ERROR_WMI_VALUES)
  342. {
  343. ShowMessage( stderr,ERROR_RETREIVE_INFO);
  344. return FAILURE;
  345. }
  346. // Connect to the Root\Cimv2 namespace of the specified system with the current user.
  347. // If no system is specified then connect to the local system.
  348. // To pass the appropriate Username to connectserver
  349. // depending upon whether the user has entered domain\user or only username at the command prompt.
  350. // connect to the server with the credentials supplied.
  351. hConnect = pIWbemLocator->ConnectServer(bstrNamespace,
  352. bstrUserName,
  353. bstrPassword,
  354. 0L,
  355. 0L,
  356. NULL,
  357. NULL,
  358. &pIWbemServices );
  359. if((StringLengthW(szUserName, 0)!=0) && FAILED(hConnect) && (hConnect == E_ACCESSDENIED))
  360. {
  361. hConnect = pIWbemLocator->ConnectServer(bstrNamespace,
  362. bstrUserName,
  363. NULL,
  364. 0L,
  365. 0L,
  366. NULL,
  367. NULL,
  368. &pIWbemServices );
  369. }
  370. if(hConnect == WBEM_S_NO_ERROR)
  371. {
  372. // Set the proxy so that impersonation of the client occurs.
  373. hISecurity = SetInterfaceSecurity(pIWbemServices,pAuthIdentity);
  374. if(FAILED(hISecurity))
  375. {
  376. GetWbemErrorText(hISecurity);
  377. ShowLastErrorEx( stdout, SLE_TYPE_ERROR | SLE_INTERNAL );
  378. SAFEIRELEASE(pIWbemServices);
  379. return FAILURE ;
  380. }
  381. // Use the IWbemServices pointer to make requests of WMI.
  382. // Create enumeration of Win32_Systemdriver class
  383. if(bSigned == FALSE)
  384. {
  385. hRes = pIWbemServices->CreateInstanceEnum(_bstr_t(CLASS_SYSTEMDRIVER),
  386. WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY,
  387. NULL,
  388. &pSystemSet);
  389. }
  390. else
  391. {
  392. h_Mutex = CreateMutex( NULL, FALSE, DRIVERQUERY_MUTEX );
  393. if( h_Mutex != NULL && GetLastError() == ERROR_ALREADY_EXISTS )
  394. {
  395. ShowMessage(stdout, GetResString(IDS_MORE_INSTANCES_SIGNEDDRIVERS));
  396. SAFEIRELEASE(pIWbemServices);
  397. return SUCCESS ;
  398. }
  399. hRes = pIWbemServices->ExecQuery(_bstr_t(LANGUAGE_WQL),_bstr_t(WQL_QUERY),WBEM_FLAG_RETURN_IMMEDIATELY| WBEM_FLAG_FORWARD_ONLY,NULL,&pSystemSet);
  400. }
  401. //if ( hRes == S_OK)
  402. if ( SUCCEEDED(hRes ))
  403. {
  404. hISecurity = SetInterfaceSecurity(pSystemSet,pAuthIdentity);
  405. if(FAILED(hISecurity))
  406. {
  407. GetWbemErrorText(hISecurity);
  408. ShowLastErrorEx( stdout, SLE_TYPE_ERROR | SLE_INTERNAL );
  409. SAFEIRELEASE(pSystemSet);
  410. return FAILURE ;
  411. }
  412. if(bSigned == FALSE)
  413. {
  414. dwProcessResult = ProcessCompSysEnum(szServer,pSystemSet,szFormat,bHeader,dwSystemType,bVerbose);
  415. }
  416. else
  417. {
  418. dwProcessResult = ProcessSignedDriverInfo(szServer,pSystemSet,szFormat,bHeader,dwSystemType,bVerbose);
  419. }
  420. switch(dwProcessResult)
  421. {
  422. case EXIT_FAILURE_MALLOC :
  423. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  424. SaveLastError();
  425. ShowLastErrorEx( stdout, SLE_TYPE_ERROR | SLE_INTERNAL );
  426. SAFEIRELEASE(pSystemSet);
  427. SAFEIRELEASE(pIWbemServices);
  428. return FAILURE ;
  429. case EXIT_FAILURE_FORMAT:
  430. ShowMessage(stderr, ERROR_ENUMERATE_INSTANCE);
  431. SAFEIRELEASE(pSystemSet);
  432. SAFEIRELEASE(pIWbemServices);
  433. return FAILURE ;
  434. case VERSION_MISMATCH_ERROR:
  435. SAFEIRELEASE(pSystemSet);
  436. SAFEIRELEASE(pIWbemServices);
  437. return FAILURE ;
  438. case EXIT_SUCCESSFUL:
  439. SAFEIRELEASE(pSystemSet);
  440. SAFEIRELEASE(pIWbemServices);
  441. return SUCCESS ;
  442. break ;
  443. case EXIT_FAILURE_RESULTS:
  444. ShowMessage(stdout, NEWLINE );
  445. ShowMessage(stdout,GetResString(IDS_NO_DRIVERS_FOUND));
  446. SAFEIRELEASE(pSystemSet);
  447. SAFEIRELEASE(pIWbemServices);
  448. return SUCCESS ;
  449. default :
  450. break;
  451. }
  452. }
  453. else
  454. {
  455. ShowMessage( stderr, ERROR_ENUMERATE_INSTANCE);
  456. SAFEIRELEASE(pIWbemServices);
  457. return FAILURE;
  458. }
  459. }
  460. else
  461. {
  462. //display the error if connect server fails
  463. //unauthorized user
  464. if(hRes == WBEM_E_ACCESS_DENIED)
  465. {
  466. ShowMessage( stderr, ERROR_AUTHENTICATION_FAILURE);
  467. }
  468. //local system credentials
  469. else if(hRes == WBEM_E_LOCAL_CREDENTIALS)
  470. {
  471. ShowMessage( stderr, ERROR_LOCAL_CREDENTIALS);
  472. }
  473. //some error
  474. else
  475. {
  476. ShowMessage( stderr, ERROR_WMI_FAILURE );
  477. }
  478. return (FAILURE);
  479. }
  480. SAFEIRELEASE(pIWbemServices);
  481. SAFEIRELEASE(pSystemSet);
  482. return (hRes);
  483. }
  484. DWORD
  485. ProcessCompSysEnum(
  486. IN CHString szHost,
  487. IN IEnumWbemClassObject *pSystemSet,
  488. IN LPTSTR szFormat,
  489. IN BOOL bHeader,
  490. IN DWORD dwSystemType,
  491. IN BOOL bVerbose
  492. )
  493. /*++
  494. Routine Description:
  495. Processes enumeration of Win32_SystemDirver instances
  496. Arguments:
  497. [ in ] szHost : HostName to connect to
  498. [ in ] pSystemSet : pointer to the structure containing system properties.
  499. [ in ] szFormat : specifies the format
  500. [ in ] bHeader : specifies if the header is required or not.
  501. [ in ] dwSystemType : specifies the sytem type.
  502. [ in ] bVerbose : Boolean varibale tells whether verbose mode is on or not.
  503. Return Value:
  504. 0 no error
  505. 1 error occured while allocating memory.
  506. --*/
  507. {
  508. HRESULT hRes = S_OK;
  509. ULONG ulReturned = 1;
  510. // declare variant type variables
  511. VARIANT vtPathName;
  512. //declaration of normal variables
  513. CHString szPathName ;
  514. DWORD dwLength = 0;
  515. LPCTSTR szPath = NULL;
  516. LPTSTR szHostName = NULL ;
  517. CHString szAcceptPauseVal ;
  518. CHString szAcceptStopVal ;
  519. LPTSTR szSysManfact = NULL;
  520. TARRAY arrResults = NULL ;
  521. TCHAR szPrintPath[MAX_STRING_LENGTH+1] =TOKEN_EMPTYSTRING ;
  522. DWORD dwRow = 0 ;
  523. BOOL bValue = FALSE ;
  524. DWORD dwValue = 0 ;
  525. TCHAR szDelimiter[MAX_RES_STRING+1] = NULL_STRING ;
  526. DWORD dwPosn1 = 0;
  527. DWORD dwStrlen = 0;
  528. DWORD dwFormatType = SR_FORMAT_TABLE ;
  529. MODULE_DATA Current ;
  530. BOOL bResult = FALSE ;
  531. NUMBERFMT *pNumberFmt = NULL;
  532. TCOLUMNS ResultHeader[ MAX_COLUMNS ];
  533. IWbemClassObject *pSystem = NULL;
  534. LPTSTR szCodeSize = NULL;
  535. LPTSTR szInitSize = NULL;
  536. LPTSTR szBssSize = NULL ;
  537. LPTSTR szAcceptStop = NULL ;
  538. int iLen = 0;
  539. LPTSTR szAcceptPause = NULL;
  540. LPTSTR szPagedSize = NULL ;
  541. TCHAR szDriverTypeVal[MAX_RES_STRING+1] = NULL_STRING;
  542. DWORD dwLocale = 0 ;
  543. WCHAR wszStrVal[MAX_RES_STRING+1] = NULL_STRING;
  544. CHString szValue ;
  545. CHString szSysName ;
  546. CHString szStartMode ;
  547. CHString szDispName ;
  548. CHString szDescription ;
  549. CHString szStatus ;
  550. CHString szState ;
  551. CHString szDriverType ;
  552. BOOL bBlankLine = FALSE;
  553. BOOL bFirstTime = TRUE;
  554. //get the paged pool acc to the Locale
  555. BOOL bFValue = FALSE ;
  556. // Fill up the NUMBERFMT structure acc to the locale specific information
  557. LPTSTR szGroupSep = NULL;
  558. LPTSTR szDecimalSep = NULL ;
  559. LPTSTR szGroupThousSep = NULL ;
  560. pNumberFmt = (NUMBERFMT *) AllocateMemory(sizeof(NUMBERFMT));
  561. if(pNumberFmt == NULL)
  562. {
  563. return EXIT_FAILURE_MALLOC ;
  564. }
  565. // Initialise the structure to Zero.
  566. SecureZeroMemory(&Current,sizeof(Current));
  567. // assign the appropriate format type to the dwFormattype flag
  568. if( StringCompare(szFormat,TABLE_FORMAT, TRUE, 0) == 0 )
  569. {
  570. dwFormatType = SR_FORMAT_TABLE;
  571. }
  572. else if( StringCompare(szFormat,LIST_FORMAT, TRUE, 0) == 0 )
  573. {
  574. dwFormatType = SR_FORMAT_LIST;
  575. }
  576. else if( StringCompare(szFormat,CSV_FORMAT, TRUE, 0) == 0 )
  577. {
  578. dwFormatType = SR_FORMAT_CSV;
  579. }
  580. // formulate the Column headers and show results appropriately
  581. FormHeader(dwFormatType,bHeader,ResultHeader,bVerbose);
  582. // loop till there are results.
  583. bFirstTime = TRUE;
  584. while ( ulReturned == 1 )
  585. {
  586. // Create new Dynamic Array to hold the result
  587. arrResults = CreateDynamicArray();
  588. if(arrResults == NULL)
  589. {
  590. FreeMemory((LPVOID*) &pNumberFmt);
  591. return EXIT_FAILURE_MALLOC ;
  592. }
  593. // Enumerate through the resultset.
  594. hRes = pSystemSet->Next(WBEM_INFINITE,
  595. 1, // return just one system
  596. &pSystem, // pointer to system
  597. &ulReturned ); // number obtained: one or zero
  598. if ( SUCCEEDED( hRes ) && (ulReturned == 1) )
  599. {
  600. // initialise the variant variables to empty
  601. VariantInit(&vtPathName);
  602. szValue = NO_DATA_AVAILABLE;
  603. szSysName = NO_DATA_AVAILABLE ;
  604. szStartMode = NO_DATA_AVAILABLE ;
  605. szDispName = NO_DATA_AVAILABLE ;
  606. szDescription = NO_DATA_AVAILABLE ;
  607. szStatus = NO_DATA_AVAILABLE ;
  608. szState = NO_DATA_AVAILABLE ;
  609. szDriverType = NO_DATA_AVAILABLE ;
  610. try
  611. {
  612. hRes = PropertyGet(pSystem,PROPERTY_NAME,szValue);
  613. ONFAILTHROWERROR(hRes);
  614. hRes = PropertyGet(pSystem,PROPERTY_SYSTEMNAME,szSysName);
  615. ONFAILTHROWERROR(hRes);
  616. hRes = PropertyGet(pSystem,PROPERTY_STARTMODE,szStartMode);
  617. ONFAILTHROWERROR(hRes);
  618. hRes = PropertyGet(pSystem,PROPERTY_DISPLAYNAME,szDispName);
  619. ONFAILTHROWERROR(hRes);
  620. hRes = PropertyGet(pSystem,PROPERTY_DESCRIPTION,szDescription);
  621. ONFAILTHROWERROR(hRes);
  622. hRes = PropertyGet(pSystem,PROPERTY_STATUS,szStatus);
  623. ONFAILTHROWERROR(hRes);
  624. hRes = PropertyGet(pSystem,PROPERTY_STATE,szState);
  625. ONFAILTHROWERROR(hRes);
  626. hRes = PropertyGet(pSystem,PROPERTY_ACCEPTPAUSE,szAcceptPauseVal);
  627. ONFAILTHROWERROR(hRes);
  628. hRes = PropertyGet(pSystem,PROPERTY_ACCEPTSTOP,szAcceptStopVal);
  629. ONFAILTHROWERROR(hRes);
  630. hRes = PropertyGet(pSystem,PROPERTY_SERVICETYPE,szDriverType);
  631. ONFAILTHROWERROR(hRes);
  632. }
  633. catch(_com_error)
  634. {
  635. ShowMessage(stderr,ERROR_GET_VALUE);
  636. SAFEIRELEASE(pSystem);
  637. DestroyDynamicArray(&arrResults);
  638. FreeMemory((LPVOID*) &pNumberFmt);
  639. return FAILURE;
  640. }
  641. // retreive the PathName property
  642. szPath = NULL;
  643. try
  644. {
  645. hRes = pSystem->Get( PROPERTY_PATHNAME, 0,&vtPathName,0,NULL );
  646. if (( hRes == WBEM_S_NO_ERROR) && (vtPathName.vt != VT_NULL) && (vtPathName.vt != VT_EMPTY))
  647. {
  648. szPathName = ( LPWSTR ) _bstr_t(vtPathName);
  649. szSysManfact = (LPTSTR) AllocateMemory ((MAX_RES_STRING) * sizeof(TCHAR));
  650. if (szSysManfact == NULL)
  651. {
  652. SAFEIRELEASE(pSystem);
  653. DestroyDynamicArray(&arrResults);
  654. FreeMemory((LPVOID *) &pNumberFmt);
  655. return EXIT_FAILURE_MALLOC;
  656. }
  657. dwLength = szPathName.GetLength();
  658. GetCompatibleStringFromUnicode( szPathName, szSysManfact, dwLength+2 );
  659. szPath = szSysManfact ;
  660. // Initialise the structure to Zero.
  661. SecureZeroMemory(&Current,sizeof(Current));
  662. // convert the szHost variable (containing hostname) into LPCTSTR and pass it to the GETAPI function
  663. szHostName = (LPTSTR) AllocateMemory((MAX_RES_STRING) * (sizeof(TCHAR)));
  664. if (szHostName == NULL)
  665. {
  666. SAFEIRELEASE(pSystem);
  667. DestroyDynamicArray(&arrResults);
  668. FreeMemory((LPVOID *) &pNumberFmt);
  669. FreeMemory((LPVOID *) &szSysManfact);
  670. return EXIT_FAILURE_MALLOC;
  671. }
  672. GetCompatibleStringFromUnicode( szHost, szHostName,dwLength+2 );
  673. StringCopy(szPrintPath,szPath, SIZE_OF_ARRAY(szPrintPath));
  674. BOOL bApiInfo = GetApiInfo(szHostName,szPath,&Current, dwSystemType);
  675. if(bApiInfo == FAILURE)
  676. {
  677. DestroyDynamicArray(&arrResults);
  678. FreeMemory((LPVOID*) &szHostName);
  679. FreeMemory((LPVOID*) &szSysManfact);
  680. continue ;
  681. }
  682. }
  683. else
  684. {
  685. DestroyDynamicArray(&arrResults);
  686. FreeMemory((LPVOID*) &szHostName);
  687. FreeMemory((LPVOID*) &szSysManfact);
  688. continue ; // ignore exception
  689. }
  690. }
  691. catch(...)
  692. {
  693. // If the path is empty then ignore the present continue with next iteration
  694. DestroyDynamicArray(&arrResults);
  695. FreeMemory((LPVOID*) &szHostName);
  696. FreeMemory((LPVOID*) &szSysManfact);
  697. continue ; // ignore exception
  698. }
  699. //create a new empty row with required no of columns
  700. dwRow = DynArrayAppendRow(arrResults,MAX_COLUMNS) ;
  701. // Insert the results into the Dynamic Array
  702. DynArraySetString2( arrResults,dwRow,COL0,szSysName,0 );
  703. DynArraySetString2( arrResults,dwRow,COL1,szValue,0 );
  704. DynArraySetString2( arrResults,dwRow,COL2,szDispName,0 );
  705. DynArraySetString2( arrResults,dwRow,COL3,szDescription,0 );
  706. // strip off the word Driver from the display.
  707. dwLength = StringLengthW(szDriverType, 0) ;
  708. GetCompatibleStringFromUnicode( szDriverType, szDriverTypeVal,dwLength+2 );
  709. StringCopy(szDelimiter,DRIVER_TAG, SIZE_OF_ARRAY(szDelimiter));
  710. dwPosn1 = _tcslen(szDelimiter);
  711. dwStrlen = _tcslen(szDriverTypeVal);
  712. szDriverTypeVal[dwStrlen-dwPosn1] = _T('\0');
  713. DynArraySetString2( arrResults,dwRow,COL4,szDriverTypeVal,0 );
  714. DynArraySetString2( arrResults,dwRow,COL5,szStartMode,0 );
  715. DynArraySetString2( arrResults,dwRow,COL6,szState,0 );
  716. DynArraySetString2( arrResults,dwRow,COL7,szStatus,0 );
  717. iLen = StringLengthW(szAcceptStopVal, 0);
  718. szAcceptStop = (LPTSTR) AllocateMemory((MAX_RES_STRING) * (sizeof(TCHAR )));
  719. if (szAcceptStop == NULL)
  720. {
  721. SAFEIRELEASE(pSystem);
  722. FreeMemory((LPVOID *) &pNumberFmt);
  723. FreeMemory((LPVOID *) &szHostName);
  724. FreeMemory((LPVOID *) &szSysManfact);
  725. DestroyDynamicArray(&arrResults);
  726. return EXIT_FAILURE_MALLOC;
  727. }
  728. GetCompatibleStringFromUnicode(szAcceptStopVal,szAcceptStop,iLen + 2 );
  729. szAcceptStop[iLen] = '\0';
  730. if (StringCompare(szAcceptStop,_T("0"), TRUE, 0)==0)
  731. {
  732. StringCopy(szAcceptStop,FALSE_VALUE, SIZE_OF_ARRAY_IN_CHARS(szAcceptStop));
  733. }
  734. else
  735. {
  736. StringCopy(szAcceptStop,TRUE_VALUE, SIZE_OF_ARRAY_IN_CHARS(szAcceptStop));
  737. }
  738. DynArraySetString2( arrResults,dwRow,COL8,szAcceptStop,0 );
  739. iLen = StringLengthW(szAcceptPauseVal, 0);
  740. szAcceptPause = (LPTSTR) AllocateMemory((MAX_RES_STRING) * (sizeof(TCHAR )));
  741. if (szAcceptPause == NULL)
  742. {
  743. SAFEIRELEASE(pSystem);
  744. FreeMemory((LPVOID *) &pNumberFmt);
  745. FreeMemory((LPVOID *) &szHostName);
  746. FreeMemory((LPVOID *) &szSysManfact);
  747. FreeMemory((LPVOID *) &szAcceptStop);
  748. DestroyDynamicArray(&arrResults);
  749. return EXIT_FAILURE_MALLOC;
  750. }
  751. GetCompatibleStringFromUnicode(szAcceptPauseVal,szAcceptPause,iLen + 2 );
  752. szAcceptPause[iLen] = '\0';
  753. if (StringCompare(szAcceptPause,_T("0"), TRUE, 0)==0)
  754. {
  755. StringCopy(szAcceptPause,FALSE_VALUE, SIZE_OF_ARRAY_IN_CHARS(szAcceptPause));
  756. }
  757. else
  758. {
  759. StringCopy(szAcceptPause,TRUE_VALUE, SIZE_OF_ARRAY_IN_CHARS(szAcceptPause));
  760. }
  761. DynArraySetString2( arrResults,dwRow,COL9,szAcceptPause,0 );
  762. bFValue = FormatAccToLocale(pNumberFmt, &szGroupSep,&szDecimalSep,&szGroupThousSep);
  763. if (bFValue == FALSE)
  764. {
  765. SAFEIRELEASE(pSystem);
  766. FreeMemory((LPVOID *) &pNumberFmt);
  767. FreeMemory((LPVOID *) &szHostName);
  768. FreeMemory((LPVOID *) &szSysManfact);
  769. FreeMemory((LPVOID *) &szAcceptStop);
  770. FreeMemory((LPVOID *) &szAcceptPause);
  771. FreeMemory((LPVOID *) &szGroupThousSep);
  772. FreeMemory((LPVOID *) &szDecimalSep);
  773. FreeMemory((LPVOID *) &szGroupSep);
  774. DestroyDynamicArray(&arrResults);
  775. return EXIT_FAILURE_FORMAT ;
  776. }
  777. szPagedSize = (LPTSTR) AllocateMemory((MAX_RES_STRING) * (sizeof(TCHAR )));
  778. if (szPagedSize == NULL)
  779. {
  780. SAFEIRELEASE(pSystem);
  781. FreeMemory((LPVOID *) &pNumberFmt);
  782. FreeMemory((LPVOID *) &szHostName);
  783. FreeMemory((LPVOID *) &szSysManfact);
  784. FreeMemory((LPVOID *) &szAcceptStop);
  785. FreeMemory((LPVOID *) &szAcceptPause);
  786. FreeMemory((LPVOID *) &szGroupThousSep);
  787. FreeMemory((LPVOID *) &szDecimalSep);
  788. FreeMemory((LPVOID *) &szGroupSep);
  789. DestroyDynamicArray(&arrResults);
  790. return EXIT_FAILURE_MALLOC;
  791. }
  792. _ltow(Current.ulPagedSize, wszStrVal,10);
  793. dwLocale = GetNumberFormat(LOCALE_USER_DEFAULT,0,wszStrVal,pNumberFmt,
  794. szPagedSize,(MAX_RES_STRING + 1));
  795. if(dwLocale == 0)
  796. {
  797. FreeMemory((LPVOID *) &pNumberFmt);
  798. FreeMemory((LPVOID *) &szHostName);
  799. FreeMemory((LPVOID *) &szSysManfact);
  800. FreeMemory((LPVOID *) &szAcceptStop);
  801. FreeMemory((LPVOID *) &szAcceptPause);
  802. FreeMemory((LPVOID *) &szPagedSize);
  803. FreeMemory((LPVOID *) &szGroupThousSep);
  804. FreeMemory((LPVOID *) &szDecimalSep);
  805. FreeMemory((LPVOID *) &szGroupSep);
  806. DestroyDynamicArray(&arrResults);
  807. return EXIT_FAILURE_FORMAT;
  808. }
  809. DynArraySetString2( arrResults,dwRow,COL10, szPagedSize,0 );
  810. // get the CodeSize info acc to the locale
  811. szCodeSize = (LPTSTR) AllocateMemory ((MAX_RES_STRING) * (sizeof(TCHAR )));
  812. if (szCodeSize == NULL)
  813. {
  814. SAFEIRELEASE(pSystem);
  815. FreeMemory((LPVOID *) &pNumberFmt);
  816. FreeMemory((LPVOID *) &szHostName);
  817. FreeMemory((LPVOID *) &szSysManfact);
  818. FreeMemory((LPVOID *) &szAcceptStop);
  819. FreeMemory((LPVOID *) &szAcceptPause);
  820. FreeMemory((LPVOID *) &szPagedSize);
  821. FreeMemory((LPVOID *) &szGroupThousSep);
  822. FreeMemory((LPVOID *) &szDecimalSep);
  823. FreeMemory((LPVOID *) &szGroupSep);
  824. DestroyDynamicArray(&arrResults);
  825. return EXIT_FAILURE_MALLOC;
  826. }
  827. _ltow(Current.ulCodeSize, wszStrVal,10);
  828. dwLocale = GetNumberFormat(LOCALE_USER_DEFAULT,0,wszStrVal,pNumberFmt,szCodeSize,(MAX_RES_STRING + 1));
  829. if(dwLocale == 0)
  830. {
  831. SAFEIRELEASE(pSystem);
  832. FreeMemory((LPVOID *) &pNumberFmt);
  833. FreeMemory((LPVOID *) &szHostName);
  834. FreeMemory((LPVOID *) &szSysManfact);
  835. FreeMemory((LPVOID *) &szAcceptStop);
  836. FreeMemory((LPVOID *) &szAcceptPause);
  837. FreeMemory((LPVOID *) &szCodeSize);
  838. FreeMemory((LPVOID *) &szGroupThousSep);
  839. FreeMemory((LPVOID *) &szDecimalSep);
  840. FreeMemory((LPVOID *) &szGroupSep);
  841. DestroyDynamicArray(&arrResults);
  842. return EXIT_FAILURE_FORMAT ;
  843. }
  844. DynArraySetString2( arrResults,dwRow,COL11, szCodeSize,0 );
  845. // retreive the bss info acc to the locale
  846. szBssSize = (LPTSTR) AllocateMemory((MAX_RES_STRING) * (sizeof(TCHAR )));
  847. if (szBssSize == NULL)
  848. {
  849. SAFEIRELEASE(pSystem);
  850. FreeMemory((LPVOID *) &pNumberFmt);
  851. FreeMemory((LPVOID *) &szHostName);
  852. FreeMemory((LPVOID *) &szSysManfact);
  853. FreeMemory((LPVOID *) &szAcceptStop);
  854. FreeMemory((LPVOID *) &szAcceptPause);
  855. FreeMemory((LPVOID *) &szCodeSize);
  856. FreeMemory((LPVOID *) &szGroupThousSep);
  857. FreeMemory((LPVOID *) &szDecimalSep);
  858. FreeMemory((LPVOID *) &szGroupSep);
  859. DestroyDynamicArray(&arrResults);
  860. return EXIT_FAILURE_MALLOC ;
  861. }
  862. _ltow(Current.ulBssSize, wszStrVal,10);
  863. dwLocale = GetNumberFormat(LOCALE_USER_DEFAULT,0,wszStrVal,pNumberFmt,
  864. szBssSize,(MAX_RES_STRING + 1));
  865. if(dwLocale == 0)
  866. {
  867. SAFEIRELEASE(pSystem);
  868. FreeMemory((LPVOID *) &pNumberFmt);
  869. FreeMemory((LPVOID *) &szHostName);
  870. FreeMemory((LPVOID *) &szSysManfact);
  871. FreeMemory((LPVOID *) &szAcceptStop);
  872. FreeMemory((LPVOID *) &szAcceptPause);
  873. FreeMemory((LPVOID *) &szCodeSize);
  874. FreeMemory((LPVOID *) &szBssSize);
  875. FreeMemory((LPVOID *) &szGroupThousSep);
  876. FreeMemory((LPVOID *) &szDecimalSep);
  877. FreeMemory((LPVOID *) &szGroupSep);
  878. DestroyDynamicArray(&arrResults);
  879. return EXIT_FAILURE_FORMAT ;
  880. }
  881. DynArraySetString2( arrResults,dwRow,COL12, szBssSize,0 );
  882. //link date
  883. DynArraySetString2(arrResults,dwRow,COL13,(LPTSTR)(Current.szTimeDateStamp),0);
  884. //Path of the File
  885. if(szPath != NULL)
  886. {
  887. DynArraySetString2(arrResults,dwRow,COL14,(LPTSTR)szPrintPath,0); //
  888. }
  889. else
  890. {
  891. szPath= NO_DATA_AVAILABLE;
  892. DynArraySetString2(arrResults,dwRow,COL14,(LPTSTR)szPath,0); //
  893. }
  894. // get the initsize info acc to the locale
  895. szInitSize = (LPTSTR) AllocateMemory((MAX_RES_STRING) * (sizeof(TCHAR )));
  896. if (szInitSize == NULL)
  897. {
  898. SAFEIRELEASE(pSystem);
  899. FreeMemory((LPVOID *) &pNumberFmt);
  900. FreeMemory((LPVOID *) &szHostName);
  901. FreeMemory((LPVOID *) &szSysManfact);
  902. FreeMemory((LPVOID *) &szAcceptStop);
  903. FreeMemory((LPVOID *) &szAcceptPause);
  904. FreeMemory((LPVOID *) &szCodeSize);
  905. FreeMemory((LPVOID *) &szBssSize);
  906. FreeMemory((LPVOID *) &szGroupThousSep);
  907. FreeMemory((LPVOID *) &szDecimalSep);
  908. FreeMemory((LPVOID *) &szGroupSep);
  909. DestroyDynamicArray(&arrResults);
  910. return EXIT_FAILURE_MALLOC ;
  911. }
  912. _ltow(Current.ulInitSize, wszStrVal,10);
  913. dwLocale = GetNumberFormat(LOCALE_USER_DEFAULT,0,wszStrVal,pNumberFmt,
  914. szInitSize,(MAX_RES_STRING + 1));
  915. if(dwLocale == 0)
  916. {
  917. SAFEIRELEASE(pSystem);
  918. FreeMemory((LPVOID *) &pNumberFmt);
  919. FreeMemory((LPVOID *) &szHostName);
  920. FreeMemory((LPVOID *) &szSysManfact);
  921. FreeMemory((LPVOID *) &szAcceptStop);
  922. FreeMemory((LPVOID *) &szAcceptPause);
  923. FreeMemory((LPVOID *) &szCodeSize);
  924. FreeMemory((LPVOID *) &szBssSize);;
  925. FreeMemory((LPVOID *) &szInitSize);
  926. FreeMemory((LPVOID *) &szGroupThousSep);
  927. FreeMemory((LPVOID *) &szDecimalSep);
  928. FreeMemory((LPVOID *) &szGroupSep);
  929. DestroyDynamicArray(&arrResults);
  930. return EXIT_FAILURE_FORMAT ;
  931. }
  932. DynArraySetString2( arrResults,dwRow,COL15, szInitSize,0 );
  933. if ( bBlankLine == TRUE && (dwFormatType & SR_FORMAT_MASK) == SR_FORMAT_LIST )
  934. ShowMessage( stdout, _T( "\n" ) );
  935. if ( bFirstTime == TRUE && (dwFormatType & SR_FORMAT_MASK) != SR_FORMAT_CSV)
  936. {
  937. ShowMessage( stdout, _T( "\n" ) );
  938. bFirstTime = FALSE;
  939. }
  940. if(bHeader)
  941. {
  942. ShowResults(MAX_COLUMNS, ResultHeader, dwFormatType|SR_NOHEADER,arrResults ) ;
  943. }
  944. else
  945. {
  946. ShowResults(MAX_COLUMNS, ResultHeader, dwFormatType,arrResults ) ;
  947. }
  948. //set the header flag to true
  949. bHeader = TRUE ;
  950. bBlankLine = TRUE;
  951. //set the bResult to true indicating that driver information has been displyed.
  952. bResult = TRUE ;
  953. // free the allocated memory
  954. FreeMemory((LPVOID *) &szSysManfact);
  955. FreeMemory((LPVOID *) &szHostName);
  956. FreeMemory((LPVOID *) &szAcceptStop);
  957. FreeMemory((LPVOID *) &szAcceptPause);
  958. FreeMemory((LPVOID *) &szPagedSize);
  959. FreeMemory((LPVOID *) &szBssSize);
  960. FreeMemory((LPVOID *) &szInitSize);
  961. FreeMemory((LPVOID *) &szCodeSize);
  962. FreeMemory((LPVOID *) &szGroupThousSep);
  963. FreeMemory((LPVOID *) &szDecimalSep);
  964. FreeMemory((LPVOID *) &szGroupSep);
  965. SAFEIRELEASE(pSystem);
  966. } // If System Succeeded
  967. // Destroy the Dynamic arrays
  968. DestroyDynamicArray(&arrResults);
  969. }// While SystemSet returning objects
  970. FreeMemory((LPVOID *) &pNumberFmt);
  971. FreeMemory((LPVOID *) &szSysManfact);
  972. FreeMemory((LPVOID *) &szHostName);
  973. FreeMemory((LPVOID *) &szAcceptStop);
  974. FreeMemory((LPVOID *) &szAcceptPause);
  975. FreeMemory((LPVOID *) &szPagedSize);
  976. FreeMemory((LPVOID *) &szBssSize);
  977. FreeMemory((LPVOID *) &szInitSize);
  978. FreeMemory((LPVOID *) &szCodeSize);
  979. FreeMemory((LPVOID *) &szGroupThousSep);
  980. FreeMemory((LPVOID *) &szDecimalSep);
  981. FreeMemory((LPVOID *) &szGroupSep);
  982. // return the error value or success value
  983. if (bResult == TRUE)
  984. {
  985. return SUCCESS ;
  986. }
  987. else
  988. {
  989. return EXIT_FAILURE_RESULTS ;
  990. }
  991. }
  992. BOOL GetApiInfo( IN LPTSTR szHostName,
  993. IN LPCTSTR pszPath,
  994. OUT PMODULE_DATA Mod,
  995. IN DWORD dwSystemType
  996. )
  997. /*++
  998. Routine Description:
  999. This function queries the system properties using API's .
  1000. Arguments:
  1001. [ in ] szHostName : HostName to connect to
  1002. [ in ] pszPath : pointer to the string containing the Path of the file.
  1003. [ out] Mod : pointer to the structure containing system properties.
  1004. [ in ] dwSystemType : variable specifies the system type
  1005. Return Value:
  1006. SUCCESS : If successful in getting the information using API's.
  1007. FAILURE : If unable to get the information using API's.
  1008. --*/
  1009. {
  1010. HANDLE hMappedFile = NULL;
  1011. PIMAGE_DOS_HEADER DosHeader;
  1012. LOADED_IMAGE LoadedImage;
  1013. ULONG ulSectionAlignment = 0;
  1014. PIMAGE_SECTION_HEADER Section;
  1015. DWORD dwI = 0;
  1016. ULONG ulSize = 0;
  1017. TCHAR szTmpServer[ MAX_STRING_LENGTH + 1 ] = NULL_STRING;
  1018. HANDLE hFile = NULL ;
  1019. PTCHAR pszToken = NULL;
  1020. StringCopy(szTmpServer,TOKEN_BACKSLASH2, SIZE_OF_ARRAY(szTmpServer));
  1021. TCHAR szFinalPath[MAX_STRING_LENGTH+1] =TOKEN_EMPTYSTRING ;
  1022. PTCHAR pdest = NULL ;
  1023. #ifndef _WIN64
  1024. BOOL bIsWin64;
  1025. #endif
  1026. //copy the path into a variable
  1027. StringCopy(szFinalPath,pszPath, SIZE_OF_ARRAY(szFinalPath));
  1028. //get the token upto the delimiter ":"
  1029. pszToken = _tcstok(szFinalPath, COLON_SYMBOL );
  1030. //form the string for getting the absolute path in the required format if it is a remote system.
  1031. if(_tcslen(szHostName) != 0)
  1032. {
  1033. pdest = (PTCHAR)FindString(pszPath,COLON_SYMBOL, 0);
  1034. if(pdest== NULL)
  1035. {
  1036. return FAILURE ;
  1037. }
  1038. _tcsnset(pdest,TOKEN_DOLLAR,1);
  1039. StringConcat(szTmpServer,szHostName, SIZE_OF_ARRAY(szTmpServer));
  1040. StringConcat(szTmpServer,TOKEN_BACKSLASH, SIZE_OF_ARRAY(szTmpServer));
  1041. StringConcat(szTmpServer,pszToken, SIZE_OF_ARRAY(szTmpServer));
  1042. StringConcat(szTmpServer,pdest, SIZE_OF_ARRAY(szTmpServer));
  1043. }
  1044. else
  1045. {
  1046. StringCopy(szTmpServer,pszPath, SIZE_OF_ARRAY(szTmpServer)) ;
  1047. }
  1048. #ifndef _WIN64
  1049. bIsWin64 = IsWin64();
  1050. if(bIsWin64)
  1051. Wow64DisableFilesystemRedirector((LPCTSTR)szTmpServer);
  1052. #endif
  1053. // create a file on the specified system and return a handle to it.
  1054. hFile = CreateFile(szTmpServer,
  1055. GENERIC_READ,
  1056. FILE_SHARE_READ,
  1057. NULL,
  1058. OPEN_EXISTING,
  1059. 0,
  1060. NULL);
  1061. //if the filehandle is invalid then return a error
  1062. if(hFile == INVALID_HANDLE_VALUE)
  1063. {
  1064. return FAILURE ;
  1065. }
  1066. #ifndef _WIN64
  1067. if(bIsWin64)
  1068. Wow64EnableFilesystemRedirector();
  1069. #endif
  1070. // create a mapping to the specified file
  1071. hMappedFile = CreateFileMapping(hFile,
  1072. NULL,
  1073. PAGE_READONLY,
  1074. 0,
  1075. 0,
  1076. NULL);
  1077. if (hMappedFile == NULL)
  1078. {
  1079. CloseHandle(hFile);
  1080. return FAILURE ;
  1081. }
  1082. LoadedImage.MappedAddress = (PUCHAR)MapViewOfFile(hMappedFile,
  1083. FILE_MAP_READ,
  1084. 0,
  1085. 0,
  1086. 0);
  1087. // close the opened file handles
  1088. CloseHandle(hMappedFile);
  1089. CloseHandle(hFile);
  1090. if ( !LoadedImage.MappedAddress )
  1091. {
  1092. return FAILURE ;
  1093. }
  1094. // check the image and find nt image headers
  1095. DosHeader = (PIMAGE_DOS_HEADER)LoadedImage.MappedAddress;
  1096. //exit if the DOS header does not match
  1097. if ( DosHeader->e_magic != IMAGE_DOS_SIGNATURE )
  1098. {
  1099. UnmapViewOfFile(LoadedImage.MappedAddress);
  1100. return FAILURE ;
  1101. }
  1102. LoadedImage.FileHeader = (PIMAGE_NT_HEADERS)((ULONG_PTR)DosHeader + DosHeader->e_lfanew);
  1103. if ( LoadedImage.FileHeader->Signature != IMAGE_NT_SIGNATURE )
  1104. {
  1105. UnmapViewOfFile(LoadedImage.MappedAddress);
  1106. return FAILURE ;
  1107. }
  1108. //get the number of sections present
  1109. LoadedImage.NumberOfSections = LoadedImage.FileHeader->FileHeader.NumberOfSections;
  1110. if(dwSystemType == SYSTEM_64_BIT )
  1111. {
  1112. LoadedImage.Sections = (PIMAGE_SECTION_HEADER)((ULONG_PTR)LoadedImage.FileHeader + sizeof(IMAGE_NT_HEADERS64));
  1113. }
  1114. else
  1115. {
  1116. LoadedImage.Sections = (PIMAGE_SECTION_HEADER)((ULONG_PTR)LoadedImage.FileHeader + sizeof(IMAGE_NT_HEADERS32));
  1117. }
  1118. LoadedImage.LastRvaSection = LoadedImage.Sections;
  1119. // Walk through the sections and tally the dater
  1120. ulSectionAlignment = LoadedImage.FileHeader->OptionalHeader.SectionAlignment;
  1121. for(Section = LoadedImage.Sections,dwI=0;dwI < LoadedImage.NumberOfSections; dwI++,Section++)
  1122. {
  1123. ulSize = Section->Misc.VirtualSize;
  1124. if (ulSize == 0)
  1125. {
  1126. ulSize = Section->SizeOfRawData;
  1127. }
  1128. ulSize = (ulSize + ulSectionAlignment - 1) & ~(ulSectionAlignment - 1);
  1129. if (!_strnicmp((char *)(Section->Name),EXTN_PAGE, 4 ))
  1130. {
  1131. Mod->ulPagedSize += ulSize;
  1132. }
  1133. else if (!_stricmp((char *)(Section->Name),EXTN_INIT ))
  1134. {
  1135. Mod->ulInitSize += ulSize;
  1136. }
  1137. else if(!_stricmp((char *)(Section->Name),EXTN_BSS))
  1138. {
  1139. Mod->ulBssSize = ulSize;
  1140. }
  1141. else if (!_stricmp((char *)(Section->Name),EXTN_EDATA))
  1142. {
  1143. Mod->ulExportDataSize = ulSize ;
  1144. }
  1145. else if (!_stricmp((char *)(Section->Name),EXTN_IDATA ))
  1146. {
  1147. Mod->ulImportDataSize = ulSize;
  1148. }
  1149. else if (!_stricmp((char *)(Section->Name),EXTN_RSRC))
  1150. {
  1151. Mod->ulResourceDataSize = ulSize;
  1152. }
  1153. else if (Section->Characteristics & IMAGE_SCN_MEM_EXECUTE)
  1154. {
  1155. Mod->ulCodeSize += ulSize;
  1156. }
  1157. else if (Section->Characteristics & IMAGE_SCN_MEM_WRITE)
  1158. {
  1159. Mod->ulDataSize += ulSize;
  1160. }
  1161. else
  1162. {
  1163. Mod->ulDataSize += ulSize;
  1164. }
  1165. }
  1166. #ifndef _WIN64
  1167. LONG lTimeVal ;
  1168. #else
  1169. LONG64 lTimeVal;
  1170. #endif
  1171. lTimeVal = LoadedImage.FileHeader->FileHeader.TimeDateStamp ;
  1172. struct tm *tmVal = NULL;
  1173. tmVal = localtime(&lTimeVal);
  1174. // proceed furthur only if we successfully got the localtime
  1175. if ( tmVal != NULL )
  1176. {
  1177. LCID lcid;
  1178. SYSTEMTIME systime;
  1179. __STRING_64 szBuffer;
  1180. BOOL bLocaleChanged = FALSE;
  1181. systime.wYear = (WORD) (DWORD_PTR)( tmVal->tm_year + 1900 ); // tm -> Year - 1900 SYSTEMTIME -> Year = Year
  1182. systime.wMonth = (WORD) (DWORD_PTR) tmVal->tm_mon + 1; // tm -> Jan = 0 SYSTEMTIME -> Jan = 1
  1183. systime.wDayOfWeek = (WORD) (DWORD_PTR) tmVal->tm_wday;
  1184. systime.wDay = (WORD) (DWORD_PTR) tmVal->tm_mday;
  1185. systime.wHour = (WORD) (DWORD_PTR) tmVal->tm_hour;
  1186. systime.wMinute = (WORD) (DWORD_PTR) tmVal->tm_min;
  1187. systime.wSecond = (WORD) (DWORD_PTR) tmVal->tm_sec;
  1188. systime.wMilliseconds = 0;
  1189. // verify whether console supports the current locale 100% or not
  1190. lcid = GetSupportedUserLocale( bLocaleChanged );
  1191. // get the formatted date
  1192. GetDateFormat( lcid, 0, &systime,
  1193. ((bLocaleChanged == TRUE) ? L"MM/dd/yyyy" : NULL), szBuffer, SIZE_OF_ARRAY( szBuffer ) );
  1194. // copy the date info
  1195. StringCopy( Mod->szTimeDateStamp, szBuffer, MAX_STRING_LENGTH+1 );
  1196. // now format the date
  1197. GetTimeFormat( LOCALE_USER_DEFAULT, 0, &systime,
  1198. ((bLocaleChanged == TRUE) ? L"HH:mm:ss" : NULL), szBuffer, SIZE_OF_ARRAY( szBuffer ) );
  1199. // now copy time info
  1200. StringConcat( Mod->szTimeDateStamp, _T( " " ), MAX_STRING_LENGTH );
  1201. StringConcat( Mod->szTimeDateStamp, szBuffer, MAX_STRING_LENGTH );
  1202. }
  1203. UnmapViewOfFile(LoadedImage.MappedAddress);
  1204. return SUCCESS;
  1205. }
  1206. BOOL
  1207. ProcessOptions(
  1208. IN LONG argc,
  1209. IN LPCTSTR argv[],
  1210. OUT PBOOL pbShowUsage,
  1211. OUT LPTSTR *pszServer,
  1212. OUT LPTSTR *pszUserName,
  1213. OUT LPTSTR pszPassword,
  1214. OUT LPTSTR pszFormat,
  1215. OUT PBOOL pbHeader,
  1216. OUT PBOOL bNeedPassword,
  1217. OUT PBOOL pbVerbose,
  1218. OUT PBOOL pbSigned
  1219. )
  1220. /*++
  1221. Routine Description:
  1222. This function parses the options specified at the command prompt
  1223. Arguments:
  1224. [ in ] argc : count of elements in argv
  1225. [ in ] argv : command-line parameterd specified by the user
  1226. [ out ] pbShowUsage : set to TRUE if -? exists in 'argv'
  1227. [ out ] pszServer : value(s) specified with -s ( server ) option in 'argv'
  1228. [ out ] pszUserName : value of -u ( username ) option in 'argv'
  1229. [ out ] pszPassword : value of -p ( password ) option in 'argv'
  1230. [ out ] pszFormat : Display format
  1231. [ out ] bHeader : specifies whether to display a header or not.
  1232. [ in ] bNeedPassword : specifies if the password is required or not.
  1233. [ in ] pbVerbose : Boolean variable returns back if Verbose option specified
  1234. [ in ] pbSigned : Boolean variable returns back if /si switch is specified.
  1235. Return Value:
  1236. TRUE : if the parsing is successful
  1237. FALSE : if errors occured in parsing
  1238. --*/
  1239. {
  1240. PTCMDPARSER2 pcmdOption = NULL; //pointer to the structure
  1241. TCMDPARSER2 cmdOptions[MAX_OPTIONS] ;
  1242. BOOL bval = TRUE ;
  1243. LPCTSTR szToken = NULL ;
  1244. // init the password
  1245. if ( pszPassword != NULL )
  1246. {
  1247. StringCopy( pszPassword, _T( "*" ), MAX_RES_STRING );
  1248. }
  1249. // help option
  1250. pcmdOption = &cmdOptions[OI_HELP] ;
  1251. pcmdOption->dwType = CP_TYPE_BOOLEAN;
  1252. pcmdOption->dwCount = 1 ;
  1253. pcmdOption->dwActuals = 0;
  1254. pcmdOption->dwFlags = CP_USAGE ;
  1255. pcmdOption->pValue = pbShowUsage ;
  1256. pcmdOption->pFunction = NULL ;
  1257. pcmdOption->pFunctionData = NULL ;
  1258. pcmdOption->dwLength = 0;
  1259. pcmdOption->pwszFriendlyName=NULL;
  1260. pcmdOption->dwReserved = 0;
  1261. pcmdOption->pReserved1 = NULL;
  1262. pcmdOption->pReserved2 = NULL;
  1263. pcmdOption->pReserved3 = NULL;
  1264. pcmdOption->pwszValues=NULL;
  1265. pcmdOption->pwszOptions=OPTION_HELP;
  1266. StringCopyA(cmdOptions[OI_HELP].szSignature, "PARSER2", 8 );
  1267. //server name option
  1268. pcmdOption = &cmdOptions[OI_SERVER] ;
  1269. pcmdOption->dwType = CP_TYPE_TEXT;
  1270. pcmdOption->dwCount = 1 ;
  1271. pcmdOption->dwActuals = 0;
  1272. pcmdOption->dwFlags = CP2_ALLOCMEMORY | CP_VALUE_MANDATORY | CP2_VALUE_TRIMINPUT | CP2_VALUE_NONULL;
  1273. pcmdOption->pValue = NULL ;
  1274. pcmdOption->pFunction = NULL ;
  1275. pcmdOption->pFunctionData = NULL ;
  1276. pcmdOption->dwType = CP_TYPE_TEXT;
  1277. pcmdOption->dwLength = 0;
  1278. pcmdOption->dwReserved = 0;
  1279. pcmdOption->pReserved1 = NULL;
  1280. pcmdOption->pReserved2 = NULL;
  1281. pcmdOption->pReserved3 = NULL;
  1282. pcmdOption->pwszValues=NULL;
  1283. pcmdOption->pwszFriendlyName=NULL;
  1284. pcmdOption->pwszOptions=OPTION_SERVER; // _T("s")
  1285. StringCopyA(cmdOptions[OI_SERVER].szSignature, "PARSER2", 8 );
  1286. //domain\user option
  1287. pcmdOption = &cmdOptions[OI_USERNAME] ;
  1288. pcmdOption->dwCount = 1 ;
  1289. pcmdOption->dwActuals = 0;
  1290. pcmdOption->dwFlags = CP2_ALLOCMEMORY | CP_VALUE_MANDATORY | CP2_VALUE_TRIMINPUT | CP2_VALUE_NONULL ;
  1291. pcmdOption->pValue = NULL;
  1292. pcmdOption->pFunction = NULL ;
  1293. pcmdOption->pFunctionData = NULL ;
  1294. pcmdOption->dwType = CP_TYPE_TEXT;
  1295. pcmdOption->dwLength = 0;
  1296. pcmdOption->pwszFriendlyName=NULL;
  1297. pcmdOption->dwReserved = 0;
  1298. pcmdOption->pReserved1 = NULL;
  1299. pcmdOption->pReserved2 = NULL;
  1300. pcmdOption->pReserved3 = NULL;
  1301. pcmdOption->pwszValues=NULL;
  1302. pcmdOption->pwszOptions=OPTION_USERNAME; // _T("u")
  1303. StringCopyA(cmdOptions[OI_USERNAME].szSignature, "PARSER2", 8 );
  1304. //password option
  1305. pcmdOption = &cmdOptions[OI_PASSWORD] ;
  1306. pcmdOption->dwCount = 1 ;
  1307. pcmdOption->dwActuals = 0;
  1308. pcmdOption->dwFlags = CP2_VALUE_OPTIONAL;
  1309. pcmdOption->pValue = pszPassword;
  1310. pcmdOption->pFunction = NULL ;
  1311. pcmdOption->pFunctionData = NULL ;
  1312. pcmdOption->dwType = CP_TYPE_TEXT;
  1313. pcmdOption->dwLength = MAX_RES_STRING;
  1314. pcmdOption->dwReserved = 0;
  1315. pcmdOption->pReserved1 = NULL;
  1316. pcmdOption->pReserved2 = NULL;
  1317. pcmdOption->pReserved3 = NULL;
  1318. pcmdOption->pwszValues=NULL;
  1319. pcmdOption->pwszFriendlyName=NULL;
  1320. pcmdOption->pwszOptions=OPTION_PASSWORD; // _T("p")
  1321. StringCopyA(cmdOptions[OI_PASSWORD].szSignature, "PARSER2", 8 );
  1322. //format option.
  1323. pcmdOption = &cmdOptions[OI_FORMAT] ;
  1324. pcmdOption->dwCount = 1 ;
  1325. pcmdOption->dwActuals = 0;
  1326. pcmdOption->dwFlags = CP2_MODE_VALUES | CP_VALUE_MANDATORY;
  1327. pcmdOption->pValue = pszFormat ;
  1328. pcmdOption->pFunction = NULL ;
  1329. pcmdOption->pFunctionData = NULL ;
  1330. pcmdOption->dwType = CP_TYPE_TEXT;
  1331. pcmdOption->dwLength = MAX_RES_STRING;
  1332. pcmdOption->dwReserved = 0;
  1333. pcmdOption->pReserved1 = NULL;
  1334. pcmdOption->pReserved2 = NULL;
  1335. pcmdOption->pReserved3 = NULL;
  1336. pcmdOption->pwszValues=FORMAT_VALUES;
  1337. pcmdOption->pwszFriendlyName=NULL;
  1338. pcmdOption->pwszOptions=OPTION_FORMAT; // _T("fo")
  1339. StringCopyA(cmdOptions[OI_FORMAT].szSignature, "PARSER2", 8 );
  1340. //no header option
  1341. pcmdOption = &cmdOptions[OI_HEADER] ;
  1342. pcmdOption->dwCount = 1 ;
  1343. pcmdOption->dwActuals = 0;
  1344. pcmdOption->dwFlags = 0;
  1345. pcmdOption->pValue = pbHeader;
  1346. pcmdOption->pFunction = NULL ;
  1347. pcmdOption->pFunctionData = NULL ;
  1348. pcmdOption->dwType = CP_TYPE_BOOLEAN;
  1349. pcmdOption->dwLength = 0;
  1350. pcmdOption->dwReserved = 0;
  1351. pcmdOption->pReserved1 = NULL;
  1352. pcmdOption->pReserved2 = NULL;
  1353. pcmdOption->pReserved3 = NULL;
  1354. pcmdOption->pwszValues=NULL_STRING;
  1355. pcmdOption->pwszFriendlyName=NULL;
  1356. pcmdOption->pwszOptions=OPTION_HEADER; // _T("nh")
  1357. StringCopyA(cmdOptions[OI_HEADER].szSignature, "PARSER2", 8 );
  1358. //verbose option..
  1359. pcmdOption = &cmdOptions[OI_VERBOSE] ;
  1360. pcmdOption->dwCount = 1 ;
  1361. pcmdOption->dwActuals = 0;
  1362. pcmdOption->dwFlags = 0 ;
  1363. pcmdOption->pValue = pbVerbose;
  1364. pcmdOption->pFunction = NULL ;
  1365. pcmdOption->pFunctionData = NULL ;
  1366. pcmdOption->dwType = CP_TYPE_BOOLEAN;
  1367. pcmdOption->dwLength = 0;
  1368. pcmdOption->dwReserved = 0;
  1369. pcmdOption->pReserved1 = NULL;
  1370. pcmdOption->pReserved2 = NULL;
  1371. pcmdOption->pReserved3 = NULL;
  1372. pcmdOption->pwszValues=NULL_STRING;
  1373. pcmdOption->pwszFriendlyName=NULL;
  1374. pcmdOption->pwszOptions=OPTION_VERBOSE; // _T("v")
  1375. StringCopyA(cmdOptions[OI_VERBOSE].szSignature, "PARSER2", 8 );
  1376. pcmdOption = &cmdOptions[OI_SIGNED] ;
  1377. pcmdOption->dwCount = 1 ;
  1378. pcmdOption->dwActuals = 0;
  1379. pcmdOption->dwFlags = 0 ;
  1380. pcmdOption->pValue = pbSigned;
  1381. pcmdOption->pFunction = NULL ;
  1382. pcmdOption->pFunctionData = NULL ;
  1383. pcmdOption->dwType = CP_TYPE_BOOLEAN;
  1384. pcmdOption->dwLength = 0;
  1385. pcmdOption->dwReserved = 0;
  1386. pcmdOption->pwszFriendlyName=NULL;
  1387. pcmdOption->pReserved1 = NULL;
  1388. pcmdOption->pReserved2 = NULL;
  1389. pcmdOption->pReserved3 = NULL;
  1390. pcmdOption->pwszValues=NULL_STRING;
  1391. pcmdOption->pwszOptions=OPTION_SIGNED; // _T("di")
  1392. StringCopyA(cmdOptions[OI_SIGNED].szSignature, "PARSER2", 8 );
  1393. bval = DoParseParam2(argc,argv,-1, MAX_OPTIONS,cmdOptions, 0) ;
  1394. if( bval== FALSE)
  1395. {
  1396. ShowMessage(stderr,ERROR_TAG);
  1397. return FALSE ;
  1398. }
  1399. if((*pbShowUsage == TRUE)&&(argc > 2))
  1400. {
  1401. SetReason(ERROR_SYNTAX);
  1402. return FALSE ;
  1403. }
  1404. *pszServer = (LPTSTR) cmdOptions[OI_SERVER].pValue;
  1405. *pszUserName = (LPTSTR) cmdOptions[OI_USERNAME].pValue;
  1406. // checking if -u is specified when -s options is not specified and display error msg .
  1407. if ((cmdOptions[OI_SERVER].dwActuals == 0) && (cmdOptions[OI_USERNAME].dwActuals !=0 ))
  1408. {
  1409. SetReason(ERROR_USERNAME_BUT_NOMACHINE);
  1410. return FALSE ;
  1411. }
  1412. // checking if -u is specified when -p options is not specified and display error msg .
  1413. if ((cmdOptions[OI_USERNAME].dwActuals == 0) && (cmdOptions[OI_PASSWORD].dwActuals !=0 ))
  1414. {
  1415. SetReason(ERROR_PASSWORD_BUT_NOUSERNAME);
  1416. return FALSE ;
  1417. }
  1418. // checking if -p is specified when -u options is not specified and display error msg .
  1419. if ((cmdOptions[OI_SERVER].dwActuals != 0) && (StringLengthW(*pszServer, 0)==0 ))
  1420. {
  1421. SetReason(ERROR_INVALID_SERVER);
  1422. return FALSE ;
  1423. }
  1424. // checking if -p is specified when -u options is not specified and display error msg .
  1425. if ((cmdOptions[OI_USERNAME].dwActuals != 0) && (StringLengthW(*pszUserName, 0)==0 ))
  1426. {
  1427. SetReason(ERROR_INVALID_USER);
  1428. return FALSE ;
  1429. }
  1430. if((cmdOptions[OI_FORMAT].dwActuals != 0)&&(StringCompare((LPCTSTR)cmdOptions[OI_FORMAT].pValue,LIST_FORMAT, TRUE, 0) == 0)&&(cmdOptions[OI_HEADER].dwActuals != 0))
  1431. {
  1432. SetReason(ERROR_NO_HEADERS);
  1433. return FALSE ;
  1434. }
  1435. if((cmdOptions[OI_SIGNED].dwActuals != 0)&&(cmdOptions[OI_VERBOSE].dwActuals != 0))
  1436. {
  1437. SetReason(INVALID_SIGNED_SYNTAX);
  1438. return FALSE ;
  1439. }
  1440. if(StrCmpN(*pszServer,TOKEN_BACKSLASH2,2)==0)
  1441. {
  1442. if(!StrCmpN(*pszServer,TOKEN_BACKSLASH3,3)==0)
  1443. {
  1444. szToken = *pszServer+2;
  1445. StringCopy( *pszServer, szToken, SIZE_OF_ARRAY_IN_CHARS(*pszServer) );
  1446. // szToken = _tcstok(*pszServer,TOKEN_BACKSLASH2);
  1447. // StringCopy(*pszServer,szToken, MAX_STRING_LENGTH);
  1448. }
  1449. }
  1450. if(IsLocalSystem( *pszServer ) == FALSE )
  1451. {
  1452. // set the bNeedPassword to True or False .
  1453. if ( cmdOptions[ OI_PASSWORD ].dwActuals != 0 &&
  1454. pszPassword != NULL && pszPassword != NULL && StringCompare( pszPassword, _T( "*" ), TRUE, 0 ) == 0 )
  1455. {
  1456. // user wants the utility to prompt for the password before trying to connect
  1457. *bNeedPassword = TRUE;
  1458. }
  1459. else if ( cmdOptions[ OI_PASSWORD ].dwActuals == 0 &&
  1460. ( cmdOptions[ OI_SERVER ].dwActuals != 0 || cmdOptions[ OI_USERNAME ].dwActuals != 0 ) )
  1461. {
  1462. // -s, -u is specified without password ...
  1463. // utility needs to try to connect first and if it fails then prompt for the password
  1464. *bNeedPassword = TRUE;
  1465. if ( pszPassword != NULL && pszPassword != NULL )
  1466. {
  1467. StringCopy( pszPassword, _T( "" ), MAX_STRING_LENGTH );
  1468. }
  1469. }
  1470. }
  1471. return TRUE ;
  1472. }
  1473. VOID
  1474. FormHeader(
  1475. IN DWORD dwFormatType,
  1476. IN BOOL bHeader,
  1477. OUT TCOLUMNS *ResultHeader,
  1478. IN BOOL bVerbose
  1479. )
  1480. /*++
  1481. Routine Description:
  1482. This function is used to build the header and also display the
  1483. result in the required format as specified by the user.
  1484. Arguments:
  1485. [ in ] arrResults : argument(s) count specified at the command prompt
  1486. [ in ] dwFormatType : format flags
  1487. [ in ] bHeader : Boolean for specifying if the header is required or not.
  1488. Return Value:
  1489. none
  1490. --*/
  1491. {
  1492. // host name
  1493. ResultHeader[COL0].dwWidth = COL_HOSTNAME_WIDTH;
  1494. ResultHeader[COL0].dwFlags = SR_TYPE_STRING|SR_HIDECOLUMN;
  1495. ResultHeader[COL0].pFunction = NULL;
  1496. ResultHeader[COL0].pFunctionData = NULL;
  1497. StringCopy( ResultHeader[COL0].szFormat, NULL_STRING, 65 );
  1498. StringCopy( ResultHeader[COL0].szColumn,COL_HOSTNAME, MAX_STRING_LENGTH );
  1499. //File Name header
  1500. ResultHeader[COL1].dwWidth = COL_FILENAME_WIDTH ;
  1501. ResultHeader[COL1].dwFlags = SR_TYPE_STRING;
  1502. ResultHeader[COL1].pFunction = NULL;
  1503. ResultHeader[COL1].pFunctionData = NULL;
  1504. StringCopy( ResultHeader[COL1].szFormat, NULL_STRING, 65 );
  1505. StringCopy( ResultHeader[COL1].szColumn,COL_FILENAME, MAX_STRING_LENGTH );
  1506. // Forming the DisplayName header Column
  1507. ResultHeader[COL2].dwWidth = COL_DISPLAYNAME_WIDTH ;
  1508. ResultHeader[COL2].dwFlags = SR_TYPE_STRING;
  1509. ResultHeader[COL2].pFunction = NULL;
  1510. ResultHeader[COL2].pFunctionData = NULL;
  1511. StringCopy( ResultHeader[COL2].szFormat, NULL_STRING, 65 );
  1512. StringCopy( ResultHeader[COL2].szColumn,COL_DISPLAYNAME, MAX_STRING_LENGTH );
  1513. // Forming the Description header Column
  1514. ResultHeader[COL3].dwWidth = COL_DESCRIPTION_WIDTH;
  1515. if(!bVerbose)
  1516. {
  1517. ResultHeader[COL3].dwFlags = SR_TYPE_STRING|SR_HIDECOLUMN;
  1518. }
  1519. else
  1520. {
  1521. ResultHeader[COL3].dwFlags = SR_TYPE_STRING;
  1522. }
  1523. ResultHeader[COL3].pFunction = NULL;
  1524. ResultHeader[COL3].pFunctionData = NULL;
  1525. StringCopy( ResultHeader[COL3].szFormat, NULL_STRING, 65 );
  1526. StringCopy( ResultHeader[COL3].szColumn,COL_DESCRIPTION, MAX_STRING_LENGTH );
  1527. // Forming the Drivertype header Column
  1528. ResultHeader[COL4].dwWidth = COL_DRIVERTYPE_WIDTH ;
  1529. ResultHeader[COL4].dwFlags = SR_TYPE_STRING;
  1530. ResultHeader[COL4].pFunction = NULL;
  1531. ResultHeader[COL4].pFunctionData = NULL;
  1532. StringCopy( ResultHeader[COL4].szFormat, NULL_STRING, 65 );
  1533. StringCopy( ResultHeader[COL4].szColumn,COL_DRIVERTYPE, MAX_STRING_LENGTH );
  1534. // Forming the StartMode header Column
  1535. ResultHeader[COL5].dwWidth = COL_STARTMODE_WIDTH;
  1536. if(!bVerbose)
  1537. {
  1538. ResultHeader[COL5].dwFlags = SR_TYPE_STRING|SR_HIDECOLUMN;
  1539. }
  1540. else
  1541. {
  1542. ResultHeader[COL5].dwFlags = SR_TYPE_STRING;
  1543. }
  1544. ResultHeader[COL5].pFunction = NULL;
  1545. ResultHeader[COL5].pFunctionData = NULL;
  1546. StringCopy( ResultHeader[COL5].szFormat, NULL_STRING, 65 );
  1547. StringCopy( ResultHeader[COL5].szColumn,COL_STARTMODE, MAX_STRING_LENGTH );
  1548. // Forming the State header Column
  1549. ResultHeader[COL6].dwWidth = COL_STATE_WIDTH ;
  1550. if(!bVerbose)
  1551. {
  1552. ResultHeader[COL6].dwFlags = SR_TYPE_STRING|SR_HIDECOLUMN;
  1553. }
  1554. else
  1555. {
  1556. ResultHeader[COL6].dwFlags = SR_TYPE_STRING;
  1557. }
  1558. ResultHeader[COL6].pFunction = NULL;
  1559. ResultHeader[COL6].pFunctionData = NULL;
  1560. StringCopy( ResultHeader[COL6].szFormat, NULL_STRING, 65 );
  1561. StringCopy( ResultHeader[COL6].szColumn,COL_STATE, MAX_STRING_LENGTH );
  1562. // Forming the Status header Column
  1563. ResultHeader[COL7].dwWidth = COL_STATUS_WIDTH;
  1564. if(!bVerbose)
  1565. {
  1566. ResultHeader[COL7].dwFlags = SR_TYPE_STRING|SR_HIDECOLUMN;
  1567. }
  1568. else
  1569. {
  1570. ResultHeader[COL7].dwFlags = SR_TYPE_STRING;
  1571. }
  1572. ResultHeader[COL7].pFunction = NULL;
  1573. ResultHeader[COL7].pFunctionData = NULL;
  1574. StringCopy( ResultHeader[COL7].szFormat, NULL_STRING, 65 );
  1575. StringCopy( ResultHeader[COL7].szColumn,COL_STATUS, MAX_STRING_LENGTH );
  1576. // Forming the AcceptStop header Column
  1577. ResultHeader[COL8].dwWidth = COL_ACCEPTSTOP_WIDTH ;
  1578. if(!bVerbose)
  1579. {
  1580. ResultHeader[COL8].dwFlags = SR_TYPE_STRING|SR_HIDECOLUMN;
  1581. }
  1582. else
  1583. {
  1584. ResultHeader[COL8].dwFlags = SR_TYPE_STRING;
  1585. }
  1586. ResultHeader[COL8].pFunction = NULL;
  1587. ResultHeader[COL8].pFunctionData = NULL;
  1588. StringCopy( ResultHeader[COL8].szFormat, NULL_STRING, 65 );
  1589. StringCopy( ResultHeader[COL8].szColumn,COL_ACCEPTSTOP, MAX_STRING_LENGTH );
  1590. // Forming the AcceptPause header Column
  1591. ResultHeader[COL9].dwWidth = COL_ACCEPTPAUSE_WIDTH;
  1592. if(!bVerbose)
  1593. {
  1594. ResultHeader[COL9].dwFlags = SR_TYPE_STRING|SR_HIDECOLUMN;
  1595. }
  1596. else
  1597. {
  1598. ResultHeader[COL9].dwFlags = SR_TYPE_STRING;
  1599. }
  1600. ResultHeader[COL9].pFunction = NULL;
  1601. ResultHeader[COL9].pFunctionData = NULL;
  1602. StringCopy( ResultHeader[COL9].szFormat, NULL_STRING, 65 );
  1603. StringCopy( ResultHeader[COL9].szColumn,COL_ACCEPTPAUSE, MAX_STRING_LENGTH );
  1604. // Forming the PagedPool header Column
  1605. ResultHeader[COL10].dwWidth = COL_PAGEDPOOL_WIDTH ;
  1606. if(!bVerbose)
  1607. {
  1608. ResultHeader[COL10].dwFlags = SR_TYPE_STRING|SR_HIDECOLUMN ;
  1609. }
  1610. else
  1611. {
  1612. ResultHeader[COL10].dwFlags = SR_TYPE_STRING;
  1613. }
  1614. ResultHeader[COL10].pFunction = NULL;
  1615. ResultHeader[COL10].pFunctionData = NULL;
  1616. StringCopy( ResultHeader[COL10].szFormat, NULL_STRING, 65 );
  1617. StringCopy(ResultHeader[COL10].szColumn,COL_PAGEDPOOL, MAX_STRING_LENGTH) ;
  1618. // Forming the Executable Code header Column
  1619. ResultHeader[COL11].dwWidth = COL_EXECCODE_WIDTH ;
  1620. if(!bVerbose)
  1621. {
  1622. ResultHeader[COL11].dwFlags = SR_TYPE_STRING|SR_HIDECOLUMN ;
  1623. }
  1624. else
  1625. {
  1626. ResultHeader[COL11].dwFlags = SR_TYPE_STRING;
  1627. }
  1628. ResultHeader[COL11].pFunction = NULL;
  1629. ResultHeader[COL11].pFunctionData = NULL;
  1630. StringCopy( ResultHeader[COL11].szFormat, NULL_STRING, 65 );
  1631. StringCopy(ResultHeader[COL11].szColumn ,COL_EXECCODE, MAX_STRING_LENGTH) ;
  1632. // Forming the BlockStorage Segment header Column
  1633. ResultHeader[COL12].dwWidth = COL_BSS_WIDTH ;
  1634. if(!bVerbose)
  1635. {
  1636. ResultHeader[COL12].dwFlags = SR_TYPE_STRING|SR_HIDECOLUMN;
  1637. }
  1638. else
  1639. {
  1640. ResultHeader[COL12].dwFlags = SR_TYPE_STRING ;
  1641. }
  1642. ResultHeader[COL12].pFunction = NULL;
  1643. ResultHeader[COL12].pFunctionData = NULL;
  1644. StringCopy( ResultHeader[COL12].szFormat, NULL_STRING, 65 );
  1645. StringCopy(ResultHeader[COL12].szColumn ,COL_BSS, MAX_STRING_LENGTH );
  1646. // Forming the LinkDate header Column
  1647. ResultHeader[COL13].dwWidth = COL_LINKDATE_WIDTH;
  1648. ResultHeader[COL13].dwFlags = SR_TYPE_STRING;
  1649. ResultHeader[COL13].pFunction = NULL;
  1650. ResultHeader[COL13].pFunctionData = NULL;
  1651. StringCopy( ResultHeader[COL13].szFormat, NULL_STRING, 65 );
  1652. StringCopy( ResultHeader[COL13].szColumn,COL_LINKDATE, MAX_STRING_LENGTH );
  1653. // Forming the Location header Column
  1654. ResultHeader[COL14].dwWidth = COL_LOCATION_WIDTH ;
  1655. if(!bVerbose)
  1656. {
  1657. ResultHeader[COL14].dwFlags = SR_TYPE_STRING|SR_HIDECOLUMN;
  1658. }
  1659. else
  1660. {
  1661. ResultHeader[COL14].dwFlags = SR_TYPE_STRING;
  1662. }
  1663. ResultHeader[COL14].pFunction = NULL;
  1664. ResultHeader[COL14].pFunctionData = NULL;
  1665. StringCopy( ResultHeader[COL14].szFormat, NULL_STRING, 65 );
  1666. StringCopy(ResultHeader[COL14].szColumn,COL_LOCATION, MAX_STRING_LENGTH);
  1667. // Forming the Init Code header Column
  1668. ResultHeader[COL15].dwWidth = COL_INITSIZE_WIDTH ;
  1669. if(!bVerbose)
  1670. {
  1671. ResultHeader[COL15].dwFlags = SR_TYPE_STRING|SR_HIDECOLUMN;
  1672. }
  1673. else
  1674. {
  1675. ResultHeader[COL15].dwFlags = SR_TYPE_STRING;
  1676. }
  1677. ResultHeader[COL15].pFunction = NULL;
  1678. ResultHeader[COL15].pFunctionData = NULL;
  1679. StringCopy( ResultHeader[COL15].szFormat, NULL_STRING, 65 );
  1680. StringCopy(ResultHeader[COL15].szColumn,COL_INITSIZE, MAX_STRING_LENGTH);
  1681. }
  1682. #ifndef _WIN64
  1683. /*-------------------------------------------------------------------------*
  1684. // IsWin64
  1685. //
  1686. // Arguments :
  1687. // none
  1688. // Returns true if we're running on Win64, false otherwise.
  1689. *--------------------------------------------------------------------------*/
  1690. BOOL IsWin64(void)
  1691. {
  1692. #ifdef UNICODE
  1693. // get a pointer to kernel32!GetSystemWow64Directory
  1694. HMODULE hmod = GetModuleHandle (_T("kernel32.dll"));
  1695. if (hmod == NULL)
  1696. return (FALSE);
  1697. UINT (WINAPI* pfnGetSystemWow64Directory)(LPTSTR, UINT);
  1698. (FARPROC&)pfnGetSystemWow64Directory = GetProcAddress (hmod, "GetSystemWow64DirectoryW");
  1699. if (pfnGetSystemWow64Directory == NULL)
  1700. return (FALSE);
  1701. /*
  1702. * if GetSystemWow64Directory fails and sets the last error to
  1703. * ERROR_CALL_NOT_IMPLEMENTED, we're on a 32-bit OS
  1704. */
  1705. TCHAR szWow64Dir[MAX_PATH];
  1706. if (((pfnGetSystemWow64Directory)(szWow64Dir, countof(szWow64Dir)) == 0) &&
  1707. (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
  1708. {
  1709. return (FALSE);
  1710. }
  1711. // we're on Win64
  1712. return (TRUE);
  1713. #else
  1714. // non-Unicode platforms cannot be Win64
  1715. return (FALSE);
  1716. #endif // UNICODE
  1717. }
  1718. #endif // _WIN64
  1719. BOOL
  1720. FormatAccToLocale(
  1721. OUT NUMBERFMT *pNumberFmt,
  1722. OUT LPTSTR* pszGroupSep,
  1723. OUT LPTSTR* pszDecimalSep,
  1724. OUT LPTSTR* pszGroupThousSep
  1725. )
  1726. /*++
  1727. Routine Description:
  1728. Formats the Number to the locale with thousands position
  1729. Arguments:
  1730. NUMBERFMT *pNumberFmt[in] - NUMBERFMT Structure to be filled with .
  1731. Return Value:
  1732. VOID
  1733. --*/
  1734. {
  1735. TCHAR szFormatedString[MAX_RES_STRING + 1] = NULL_STRING;
  1736. HRESULT hResult = 0;
  1737. DWORD dwLocale = 0;
  1738. if( GetInfo( LOCALE_SGROUPING, pszGroupSep ) == FALSE)
  1739. {
  1740. pNumberFmt = NULL;
  1741. return FALSE ;
  1742. }
  1743. if( GetInfo( LOCALE_SDECIMAL, pszDecimalSep ) == FALSE)
  1744. {
  1745. pNumberFmt = NULL;
  1746. return FALSE ;
  1747. }
  1748. if( GetInfo( LOCALE_STHOUSAND, pszGroupThousSep ) == FALSE)
  1749. {
  1750. pNumberFmt = NULL;
  1751. return FALSE ;
  1752. }
  1753. if(pNumberFmt != NULL)
  1754. {
  1755. pNumberFmt->LeadingZero = 0;
  1756. pNumberFmt->NegativeOrder = 0;
  1757. pNumberFmt->NumDigits = 0;
  1758. if(StringCompare(*pszGroupSep, GROUP_FORMAT_32, TRUE, 0) == 0)
  1759. {
  1760. pNumberFmt->Grouping = GROUP_32_VALUE;
  1761. }
  1762. else
  1763. {
  1764. pNumberFmt->Grouping = UINT( _ttoi( *pszGroupSep ) );
  1765. }
  1766. pNumberFmt->lpDecimalSep = *pszDecimalSep;
  1767. pNumberFmt->lpThousandSep = *pszGroupThousSep;
  1768. }
  1769. return TRUE ;
  1770. }
  1771. BOOL
  1772. GetInfo(
  1773. IN LCTYPE lctype,
  1774. OUT LPTSTR* pszData
  1775. )
  1776. /*++
  1777. Routine Description:
  1778. Gets the Locale information
  1779. Arguments:
  1780. [ in ] lctype -- Locale Information to get
  1781. [ out ] pszData -- Locale value corresponding to the given information
  1782. Return Value:
  1783. BOOL
  1784. --*/
  1785. {
  1786. LONG lSize = 0;
  1787. // get the locale specific info
  1788. lSize = GetLocaleInfo( LOCALE_USER_DEFAULT, lctype, NULL, 0 );
  1789. if ( lSize != 0 )
  1790. {
  1791. *pszData = (LPTSTR)AllocateMemory((lSize + 1)*sizeof(TCHAR));
  1792. if ( *pszData != NULL )
  1793. {
  1794. // get the locale specific time seperator
  1795. GetLocaleInfo( LOCALE_USER_DEFAULT, lctype, *pszData, lSize );
  1796. return TRUE;
  1797. }
  1798. }
  1799. return FALSE;
  1800. }//end of GetInfo
  1801. DWORD
  1802. GetSystemType(
  1803. IN COAUTHIDENTITY* pAuthIdentity,
  1804. IN IWbemServices* pIWbemServReg
  1805. )
  1806. /*++
  1807. Routine Description:
  1808. Gets the type os the specified System ( 32 bit or 64 bit)
  1809. Arguments:
  1810. IWbemLocator *pLocator[in] -- Pointer to the locator interface.
  1811. _bstr_t bstrServer[in] -- Server Name
  1812. _bstr_t bstrUserName[in] -- User Name
  1813. _bstr_t bstrPassword [in] -- Password information
  1814. Return Value:
  1815. DWORD : SYSTEM_32_BIT -- If the system is 32 bit system.
  1816. SYSTEM_64_BIT -- If the system is 32 bit system.
  1817. ERROR_WMI_VALUES -- If error occured while retreiving values from WMI.
  1818. --*/
  1819. {
  1820. IWbemClassObject * pInClass = NULL;
  1821. IWbemClassObject * pClass = NULL;
  1822. IWbemClassObject * pOutInst = NULL;
  1823. IWbemClassObject * pInInst = NULL;
  1824. VARIANT varConnectName;
  1825. VARIANT varSvalue ;
  1826. VARIANT varHkey;
  1827. VARIANT varVaue ;
  1828. VARIANT varRetVal ;
  1829. HRESULT hRes = S_OK;
  1830. LPTSTR szSysName = NULL ;
  1831. CHString szSystemName ;
  1832. DWORD dwSysType = 0 ;
  1833. VariantInit(&varConnectName) ;
  1834. VariantInit(&varSvalue) ;
  1835. VariantInit(&varHkey) ;
  1836. VariantInit(&varVaue) ;
  1837. VariantInit(&varRetVal) ;
  1838. try
  1839. {
  1840. hRes = pIWbemServReg->GetObject(bstr_t(STD_REG_CLASS), 0, NULL, &pClass, NULL);
  1841. ONFAILTHROWERROR(hRes);
  1842. if(hRes != WBEM_S_NO_ERROR)
  1843. {
  1844. hRes = FreeMemoryAll(pInClass,pClass,pOutInst,pInInst,pIWbemServReg,&varConnectName,&varSvalue,&varHkey,&varRetVal,&varVaue,szSysName );
  1845. return (ERROR_WMI_VALUES);
  1846. }
  1847. // Get the input argument and set the property
  1848. hRes = pClass->GetMethod(_bstr_t(PROPERTY_GETSTRINGVAL), 0, &pInClass, NULL);
  1849. ONFAILTHROWERROR(hRes);
  1850. if(hRes != WBEM_S_NO_ERROR)
  1851. {
  1852. FreeMemoryAll(pInClass,pClass,pOutInst,pInInst,pIWbemServReg,&varConnectName,&varSvalue,&varHkey,&varRetVal,&varVaue,szSysName );
  1853. return (ERROR_WMI_VALUES);
  1854. }
  1855. hRes = pInClass->SpawnInstance(0, &pInInst);
  1856. ONFAILTHROWERROR(hRes);
  1857. if(hRes != WBEM_S_NO_ERROR)
  1858. {
  1859. FreeMemoryAll(pInClass,pClass,pOutInst,pInInst,pIWbemServReg,&varConnectName,&varSvalue,&varHkey,&varRetVal,&varVaue,szSysName );
  1860. return (ERROR_WMI_VALUES);
  1861. }
  1862. //the registry path to get the connection name
  1863. varConnectName.vt = VT_BSTR;
  1864. varConnectName.bstrVal= SysAllocString(REG_PATH);
  1865. hRes = pInInst->Put(REG_SUB_KEY_VALUE, 0, &varConnectName, 0);
  1866. ONFAILTHROWERROR(hRes);
  1867. //set the svalue name
  1868. varSvalue.vt = VT_BSTR;
  1869. varSvalue.bstrVal= SysAllocString(REG_SVALUE);
  1870. hRes = pInInst->Put(REG_VALUE_NAME, 0, &varSvalue, 0);
  1871. ONFAILTHROWERROR(hRes);
  1872. varHkey.vt = VT_I4 ;
  1873. varHkey.lVal = HEF_KEY_VALUE;
  1874. hRes = pInInst->Put(HKEY_VALUE, 0, &varHkey, 0);
  1875. ONFAILTHROWERROR(hRes);
  1876. // Call the method
  1877. hRes = pIWbemServReg->ExecMethod(_bstr_t(STD_REG_CLASS), _bstr_t(REG_METHOD), 0, NULL, pInInst, &pOutInst, NULL);
  1878. ONFAILTHROWERROR(hRes);
  1879. if(pOutInst == NULL)
  1880. {
  1881. FreeMemoryAll(pInClass,pClass,pOutInst,pInInst,pIWbemServReg,&varConnectName,&varSvalue,&varHkey,&varRetVal,&varVaue,szSysName );
  1882. return (ERROR_WMI_VALUES);
  1883. }
  1884. hRes = pOutInst->Get(PROPERTY_RETURNVAL,0,&varRetVal,NULL,NULL);
  1885. ONFAILTHROWERROR(hRes);
  1886. if(varRetVal.lVal != 0)
  1887. {
  1888. FreeMemoryAll(pInClass,pClass,pOutInst,pInInst,pIWbemServReg,&varConnectName,&varSvalue,&varHkey,&varRetVal,&varVaue,szSysName );
  1889. return (ERROR_WMI_VALUES);
  1890. }
  1891. hRes = pOutInst->Get(REG_RETURN_VALUE,0,&varVaue,NULL,NULL);
  1892. ONFAILTHROWERROR(hRes);
  1893. if(hRes != WBEM_S_NO_ERROR)
  1894. {
  1895. FreeMemoryAll(pInClass,pClass,pOutInst,pInInst,pIWbemServReg,&varConnectName,&varSvalue,&varHkey,&varRetVal,&varVaue,szSysName );
  1896. return (ERROR_WMI_VALUES);
  1897. }
  1898. }
  1899. catch(_com_error& e)
  1900. {
  1901. ShowMessage(stderr,ERROR_GET_VALUE);
  1902. FreeMemoryAll(pInClass,pClass,pOutInst,pInInst,pIWbemServReg,&varConnectName,&varSvalue,&varHkey,&varRetVal,&varVaue,szSysName );
  1903. return (ERROR_WMI_VALUES);
  1904. }
  1905. szSystemName = V_BSTR(&varVaue);
  1906. szSysName = (LPTSTR)AllocateMemory((MAX_RES_STRING)*sizeof(TCHAR));
  1907. if(szSysName == NULL)
  1908. {
  1909. FreeMemoryAll(pInClass,pClass,pOutInst,pInInst,pIWbemServReg,&varConnectName,&varSvalue,&varHkey,&varRetVal,&varVaue,szSysName );
  1910. return (ERROR_WMI_VALUES);
  1911. }
  1912. GetCompatibleStringFromUnicode( szSystemName, szSysName, StringLengthW( szSystemName, 0 )+2 );
  1913. dwSysType = FindString(szSysName,X86_MACHINE, 0) ? SYSTEM_32_BIT:SYSTEM_64_BIT ;
  1914. FreeMemoryAll(pInClass, pClass, pOutInst, pInInst, pIWbemServReg, &varConnectName, &varSvalue, &varHkey, &varRetVal, &varVaue, szSysName );
  1915. return (dwSysType);
  1916. }
  1917. BOOL
  1918. InitialiseCom(
  1919. IN IWbemLocator** ppIWbemLocator
  1920. )
  1921. /*++
  1922. Routine Description:
  1923. Gets the type os the specified System ( 32 bit or 64 bit)
  1924. Arguments:
  1925. IWbemLocator** pLocator[in] -- Pointer to the locator interface.
  1926. Return Value:
  1927. BOOL : TRUE on Successfully initialising COM.
  1928. FALSE on Failure to initialise COM.
  1929. --*/
  1930. {
  1931. HRESULT hRes = S_OK ;
  1932. try
  1933. {
  1934. // To initialize the COM library.
  1935. hRes = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED );
  1936. ONFAILTHROWERROR(hRes);
  1937. // Initialize COM security for DCOM services, Adjust security to
  1938. // allow client impersonation
  1939. hRes = CoInitializeSecurity( NULL, -1, NULL, NULL,
  1940. RPC_C_AUTHN_LEVEL_NONE,
  1941. RPC_C_IMP_LEVEL_IMPERSONATE,
  1942. NULL,
  1943. EOAC_NONE, 0 );
  1944. ONFAILTHROWERROR(hRes);
  1945. // get a pointer to the Interface IWbemLocator
  1946. hRes = CoCreateInstance(CLSID_WbemLocator, NULL,
  1947. CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) ppIWbemLocator);
  1948. ONFAILTHROWERROR(hRes);
  1949. }
  1950. catch(_com_error& e )
  1951. {
  1952. GetWbemErrorText( e.Error() );
  1953. ShowMessage( stderr, ERROR_TAG );
  1954. ShowMessage( stderr, GetReason() );
  1955. return( FALSE );
  1956. }
  1957. // if successfully initialised COM then return true
  1958. return TRUE ;
  1959. }
  1960. HRESULT
  1961. PropertyGet(
  1962. IN IWbemClassObject* pWmiObject,
  1963. IN LPCTSTR pszInputVal,
  1964. OUT CHString &szOutPutVal
  1965. )
  1966. /*++
  1967. Routine Description:
  1968. This routine extract the value of the property into szOutputVal from given wmi class object
  1969. Arguments:
  1970. [ in ] pWmiObject -- Pointer to the locator interface.
  1971. [ in ] pszInputVal -- Input string containing the desired value.
  1972. [ in ] szOutPutVal-- String containing the retreived value.
  1973. Return Value:
  1974. BOOL : TRUE on Successfully initialising COM.
  1975. FALSE on Failure to initialise COM.
  1976. --*/
  1977. {
  1978. HRESULT hRes = S_FALSE ;
  1979. VARIANT vtValue ;
  1980. VariantInit(&vtValue);
  1981. hRes = pWmiObject->Get(pszInputVal,0,&vtValue,NULL,NULL);
  1982. if (hRes != WBEM_S_NO_ERROR)
  1983. {
  1984. hRes = VariantClear(&vtValue);
  1985. return (hRes);
  1986. }
  1987. if ((hRes == WBEM_S_NO_ERROR)&&(vtValue.vt != VT_NULL) && (vtValue.vt != VT_EMPTY))
  1988. {
  1989. szOutPutVal = (LPCWSTR)_bstr_t(vtValue);
  1990. }
  1991. hRes = VariantClear(&vtValue);
  1992. if(hRes != S_OK)
  1993. {
  1994. return hRes ;
  1995. }
  1996. return TRUE ;
  1997. }
  1998. HRESULT
  1999. FreeMemoryAll(
  2000. IN IWbemClassObject *pInClass,
  2001. IN IWbemClassObject * pClass,
  2002. IN IWbemClassObject * pOutInst ,
  2003. IN IWbemClassObject * pInInst,
  2004. IN IWbemServices *pIWbemServReg,
  2005. IN VARIANT *varConnectName,
  2006. IN VARIANT *varSvalue,
  2007. IN VARIANT *varHkey,
  2008. IN VARIANT *varRetVal,
  2009. IN VARIANT *varVaue,
  2010. IN LPTSTR szSysName
  2011. )
  2012. /*++
  2013. Routine Description:
  2014. This function frees the memory allocated in the function.
  2015. Arguments:
  2016. [ in ] pInClass - Interface ptr pointing to the IWbemClassObject interface
  2017. [ in ] pClass - Interface ptr pointing to the IWbemClassObject interface
  2018. [ in ] pOutInst - Interface ptr pointing to the IWbemClassObject interface
  2019. [ in ] pInInst - Interface ptr pointing to the IWbemClassObject interface
  2020. [ in ] pIWbemServReg - Interface ptr pointing to the IWbemServices interface
  2021. [ in ] varConnectName - variant to be cleared
  2022. [ in ] varSvalue - variant to be cleared
  2023. [ in ] varHkey - variant to be cleared
  2024. [ in ] varRetVal - variant to be cleared
  2025. [ in ] varVaue - variant to be cleared
  2026. [ in ] szSysName - LPTSTR varaible containing the system name.
  2027. Return Value:
  2028. None
  2029. --*/
  2030. {
  2031. HRESULT hRes = S_OK ;
  2032. SAFEIRELEASE(pInInst);
  2033. SAFEIRELEASE(pInClass);
  2034. SAFEIRELEASE(pClass);
  2035. SAFEIRELEASE(pIWbemServReg);
  2036. FreeMemory((LPVOID *) &szSysName);
  2037. hRes = VariantClear(varConnectName);
  2038. if(hRes != S_OK)
  2039. {
  2040. return hRes ;
  2041. }
  2042. hRes = VariantClear(varSvalue);
  2043. if(hRes != S_OK)
  2044. {
  2045. return hRes ;
  2046. }
  2047. hRes = VariantClear(varHkey);
  2048. if(hRes != S_OK)
  2049. {
  2050. return hRes ;
  2051. }
  2052. hRes = VariantClear(varVaue);
  2053. if(hRes != S_OK)
  2054. {
  2055. return hRes ;
  2056. }
  2057. hRes = VariantClear(varRetVal);
  2058. if(hRes != S_OK)
  2059. {
  2060. return hRes ;
  2061. }
  2062. return S_OK ;
  2063. }
  2064. HRESULT
  2065. PropertyGet_Bool(
  2066. IN IWbemClassObject* pWmiObject,
  2067. IN LPCTSTR pszInputVal,
  2068. OUT PBOOL pIsSigned
  2069. )
  2070. /*++
  2071. Routine Description:
  2072. This routine will get the property of boolean type from the class object
  2073. Arguments:
  2074. [ in ] pWmiObject[in] -- Pointer to the locator interface.
  2075. [ in ] pszInputVal[in] -- Input string containing the desired value.
  2076. [ in ] pIsSigned[out] -- String containing the retreived value.
  2077. Return Value:
  2078. HRESULT : hRes on Successfully retreiving the value.
  2079. S_FALSE on Failure in retreiving the value.
  2080. --*/
  2081. {
  2082. HRESULT hRes = S_FALSE ;
  2083. VARIANT vtValue ;
  2084. VariantInit(&vtValue);
  2085. hRes = pWmiObject->Get(pszInputVal,0,&vtValue,NULL,NULL);
  2086. if (hRes != WBEM_S_NO_ERROR)
  2087. {
  2088. hRes = VariantClear(&vtValue);
  2089. return (hRes);
  2090. }
  2091. if ((hRes == WBEM_S_NO_ERROR)&&(vtValue.vt != VT_NULL) && (vtValue.vt != VT_EMPTY))
  2092. {
  2093. if(vtValue.vt == VT_BOOL)
  2094. if(vtValue.boolVal == -1)
  2095. *pIsSigned = 1;
  2096. else
  2097. *pIsSigned = 0;
  2098. hRes = VariantClear(&vtValue);
  2099. if(hRes != S_OK)
  2100. {
  2101. return hRes ;
  2102. }
  2103. return hRes ;
  2104. }
  2105. else if ((hRes == WBEM_S_NO_ERROR)&&(vtValue.vt == VT_NULL) )
  2106. {
  2107. *pIsSigned = FALSE;
  2108. }
  2109. hRes = VariantClear(&vtValue);
  2110. return S_FALSE ;
  2111. }
  2112. VOID
  2113. FormSignedHeader(
  2114. IN DWORD dwFormatType,
  2115. IN BOOL bHeader,
  2116. OUT TCOLUMNS *ResultHeader)
  2117. /*++
  2118. Routine Description:
  2119. This function is used to build the header and also display the
  2120. result in the required format as specified by the user.
  2121. Arguments:
  2122. [ in ] dwFormatType : format flags
  2123. [ in ] bHeader : Boolean for specifying if the header is required or not.
  2124. [ out ] ResultHeader : The result header of tcolumns.
  2125. Return Value:
  2126. none
  2127. --*/
  2128. {
  2129. // Device name
  2130. ResultHeader[COL0].dwWidth = COL_DEVICE_WIDTH ;
  2131. ResultHeader[COL0].dwFlags = SR_TYPE_STRING;
  2132. ResultHeader[COL0].pFunction = NULL;
  2133. ResultHeader[COL0].pFunctionData = NULL;
  2134. STRING_COPY_STATIC( ResultHeader[COL0].szFormat, NULL_STRING );
  2135. STRING_COPY_STATIC( ResultHeader[COL0].szColumn,COL_DEVICENAME );
  2136. //Inf header
  2137. ResultHeader[COL1].dwWidth = COL_INF_WIDTH ;
  2138. ResultHeader[COL1].dwFlags = SR_TYPE_STRING;
  2139. ResultHeader[COL1].pFunction = NULL;
  2140. ResultHeader[COL1].pFunctionData = NULL;
  2141. STRING_COPY_STATIC( ResultHeader[COL1].szFormat, NULL_STRING );
  2142. STRING_COPY_STATIC( ResultHeader[COL1].szColumn,COL_INF_NAME);
  2143. // Forming the IsSigned header Column
  2144. ResultHeader[COL2].dwWidth = COL_ISSIGNED_WIDTH ;
  2145. ResultHeader[COL2].dwFlags = SR_TYPE_STRING;
  2146. ResultHeader[COL2].pFunction = NULL;
  2147. ResultHeader[COL2].pFunctionData = NULL;
  2148. STRING_COPY_STATIC( ResultHeader[COL2].szFormat, NULL_STRING );
  2149. STRING_COPY_STATIC( ResultHeader[COL2].szColumn,COL_ISSIGNED);
  2150. // Forming the Manufacturer header Column
  2151. ResultHeader[COL3].dwWidth = COL_MANUFACTURER_WIDTH ;
  2152. ResultHeader[COL3].dwFlags = SR_TYPE_STRING;
  2153. ResultHeader[COL3].pFunction = NULL;
  2154. ResultHeader[COL3].pFunctionData = NULL;
  2155. STRING_COPY_STATIC( ResultHeader[COL3].szFormat, NULL_STRING );
  2156. STRING_COPY_STATIC( ResultHeader[COL3].szColumn,COL_MANUFACTURER);
  2157. }
  2158. LCID
  2159. GetSupportedUserLocale(
  2160. OUT BOOL& bLocaleChanged
  2161. )
  2162. /*++
  2163. Routine Description: This function checks if the current locale is supported or not.
  2164. Arguments:
  2165. [ out ] bLocaleChanged : returns back if the current locale is supported or not.
  2166. Return Value: LCID of the current locale.
  2167. --*/
  2168. {
  2169. // local variables
  2170. LCID lcid;
  2171. // get the current locale
  2172. lcid = GetUserDefaultLCID();
  2173. // check whether the current locale is supported by our tool or not
  2174. // if not change the locale to the english which is our default locale
  2175. bLocaleChanged = FALSE;
  2176. if ( PRIMARYLANGID( lcid ) == LANG_ARABIC || PRIMARYLANGID( lcid ) == LANG_HEBREW ||
  2177. PRIMARYLANGID( lcid ) == LANG_THAI || PRIMARYLANGID( lcid ) == LANG_HINDI ||
  2178. PRIMARYLANGID( lcid ) == LANG_TAMIL || PRIMARYLANGID( lcid ) == LANG_FARSI )
  2179. {
  2180. bLocaleChanged = TRUE;
  2181. lcid = MAKELCID( MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ), SORT_DEFAULT ); // 0x409;
  2182. }
  2183. // return the locale
  2184. return lcid;
  2185. }
  2186. DWORD
  2187. ProcessSignedDriverInfo(
  2188. IN CHString szHost,
  2189. IN IEnumWbemClassObject *pSystemSet,
  2190. IN LPTSTR szFormat,
  2191. IN BOOL bNoHeader,
  2192. IN DWORD dwSystemType,
  2193. IN BOOL bVerbose
  2194. )
  2195. /*++
  2196. Routine Description:
  2197. Processes enumeration of Win32_PnpSignedDriver instances
  2198. Arguments:
  2199. [ in ] szHost : HostName to connect to
  2200. [ in ] pSystemSet : pointer to the structure containing system properties.
  2201. [ in ] szFormat : specifies the format
  2202. [ in ] bHeader : specifies if the header is required or not.
  2203. Return Value:
  2204. 0 no error
  2205. 1 error occured while allocating memory.
  2206. -*/
  2207. {
  2208. HRESULT hRes = S_OK;
  2209. ULONG ulReturned = 1;
  2210. DWORD dwCount = 0;
  2211. //declaration of normal variables
  2212. IWbemClassObject* pDriverObjects[ MAX_ENUM_DRIVERS ];
  2213. CHString szPnpDeviceName ;
  2214. CHString szPnpInfName ;
  2215. CHString szPnpMfg ;
  2216. CHString szSigned ;
  2217. BOOL bIsSigned = FALSE;
  2218. TCOLUMNS ResultHeader[ MAX_SIGNED_COLUMNS ];
  2219. TARRAY arrResults = NULL;
  2220. DWORD dwRow = 0;
  2221. DWORD dwFormatType = SR_FORMAT_TABLE ;
  2222. BOOL bHasNewResults = FALSE ;
  2223. // Create new Dynamic Array to hold the result
  2224. arrResults = CreateDynamicArray();
  2225. if(arrResults == NULL)
  2226. {
  2227. return EXIT_FAILURE_MALLOC ;
  2228. }
  2229. // determine the format of the output
  2230. if( StringCompare(szFormat,TABLE_FORMAT, TRUE, 0) == 0 )
  2231. {
  2232. dwFormatType = SR_FORMAT_TABLE;
  2233. }
  2234. else if( StringCompare(szFormat,LIST_FORMAT, TRUE, 0) == 0 )
  2235. {
  2236. dwFormatType = SR_FORMAT_LIST;
  2237. }
  2238. else if( StringCompare(szFormat,CSV_FORMAT, TRUE, 0) == 0 )
  2239. {
  2240. dwFormatType = SR_FORMAT_CSV;
  2241. }
  2242. // prepare the header structure
  2243. FormSignedHeader(dwFormatType,bNoHeader,ResultHeader);
  2244. // determine whether to show the header row or not
  2245. if ( bNoHeader == TRUE )
  2246. {
  2247. dwFormatType |= SR_NOHEADER;
  2248. }
  2249. // initialize all the objects to NULL
  2250. for(DWORD dw = 0 ;dw< MAX_ENUM_DRIVERS;dw++ )
  2251. {
  2252. pDriverObjects[ dw ] = NULL;
  2253. }
  2254. // loop till there are results.
  2255. bHasNewResults = FALSE;
  2256. while ( ulReturned > 0 )
  2257. {
  2258. // loop thru the object list and release them
  2259. for( DWORD dw = 0; dw < MAX_ENUM_DRIVERS; dw++ )
  2260. {
  2261. SAFEIRELEASE( pDriverObjects[ dw ] );
  2262. }
  2263. // delete all the information in the data array
  2264. szPnpDeviceName = NO_DATA_AVAILABLE;
  2265. szPnpInfName = NO_DATA_AVAILABLE;
  2266. szSigned = NO_DATA_AVAILABLE ;
  2267. szPnpMfg = NO_DATA_AVAILABLE ;
  2268. // Enumerate through the resultset.
  2269. hRes = pSystemSet->Next(WBEM_INFINITE,
  2270. MAX_ENUM_DRIVERS, // return just one system
  2271. pDriverObjects, // pointer to system
  2272. &ulReturned ); // number obtained: one or zero
  2273. // update the count of records we retrived so far
  2274. dwCount++;
  2275. if ( SUCCEEDED( hRes ) )
  2276. {
  2277. for(ULONG ul=0;ul< ulReturned;ul++)
  2278. {
  2279. // initialise the variant variables to empty
  2280. //create a new empty row with required no of columns
  2281. dwRow = DynArrayAppendRow(arrResults,MAX_COLUMNS) ;
  2282. try
  2283. {
  2284. hRes = PropertyGet(pDriverObjects[ul],PROPERTY_PNP_DEVICENAME,szPnpDeviceName);
  2285. ONFAILTHROWERROR(hRes);
  2286. hRes = PropertyGet(pDriverObjects[ul],PROPERTY_PNP_INFNAME,szPnpInfName);
  2287. ONFAILTHROWERROR(hRes);
  2288. hRes = PropertyGet_Bool(pDriverObjects[ul],PROPERTY_PNP_ISSIGNED,&bIsSigned);
  2289. ONFAILTHROWERROR(hRes);
  2290. hRes = PropertyGet(pDriverObjects[ul],PROPERTY_PNP_MFG,szPnpMfg);
  2291. ONFAILTHROWERROR(hRes);
  2292. }
  2293. catch(_com_error)
  2294. {
  2295. ShowMessage(stderr,ERROR_GET_VALUE);
  2296. SAFEIRELEASE(pDriverObjects[ul]);
  2297. return FAILURE;
  2298. }
  2299. // free the allocated memory
  2300. SAFEIRELEASE(pDriverObjects[ul]);
  2301. if(bIsSigned)
  2302. {
  2303. szSigned = TRUE_VALUE;
  2304. }
  2305. else
  2306. {
  2307. szSigned = FALSE_VALUE;
  2308. }
  2309. DynArraySetString2( arrResults,dwRow,COL0,szPnpDeviceName,0 );
  2310. DynArraySetString2( arrResults,dwRow,COL1,szPnpInfName,0 );
  2311. DynArraySetString2( arrResults,dwRow,COL2,szSigned,0 );
  2312. DynArraySetString2( arrResults,dwRow,COL3,szPnpMfg,0 );
  2313. //display one blank line for first time
  2314. if( FALSE == bHasNewResults && (dwFormatType & SR_FORMAT_MASK) != SR_FORMAT_CSV)
  2315. {
  2316. ShowMessage(stdout, L"\n" );
  2317. }
  2318. // this flag is to check if there are any results
  2319. // else display an error message.
  2320. bHasNewResults = TRUE ;
  2321. // delete all the information in the data array
  2322. szPnpDeviceName = NO_DATA_AVAILABLE;
  2323. szPnpInfName = NO_DATA_AVAILABLE;
  2324. szSigned = NO_DATA_AVAILABLE ;
  2325. szPnpMfg = NO_DATA_AVAILABLE ;
  2326. }
  2327. //display results
  2328. if( TRUE == bHasNewResults )
  2329. {
  2330. // show the results enumerated in this loop
  2331. ShowResults( MAX_SIGNED_COLUMNS, ResultHeader, dwFormatType, arrResults );
  2332. // clear the reuslts that we enumerated so far
  2333. DynArrayRemoveAll( arrResults );
  2334. // from next time onwards, we should not display the columns header
  2335. dwFormatType |= SR_NOHEADER;
  2336. if ( (dwFormatType | SR_FORMAT_LIST) == (SR_NOHEADER | SR_FORMAT_LIST) )
  2337. {
  2338. bHasNewResults = FALSE;
  2339. }
  2340. }
  2341. }
  2342. else
  2343. {
  2344. if( 0x80041010 == hRes )
  2345. {
  2346. ShowMessage( stderr, GetResString(IDS_VERSION_MISMATCH_ERROR) );
  2347. DestroyDynamicArray( &arrResults );
  2348. return VERSION_MISMATCH_ERROR;
  2349. }
  2350. else
  2351. {
  2352. WMISaveError( hRes );
  2353. ShowLastErrorEx( stderr, SLE_INTERNAL | SLE_TYPE_ERROR );
  2354. DestroyDynamicArray( &arrResults );
  2355. return VERSION_MISMATCH_ERROR;
  2356. }
  2357. }
  2358. }
  2359. if (dwCount == 0)
  2360. {
  2361. ShowMessage(stderr,GetResString(IDS_VERSION_MISMATCH_ERROR));
  2362. // free the allocated memory
  2363. for(DWORD dw = 0 ;dw< MAX_ENUM_DRIVERS;dw++ )
  2364. {
  2365. SAFEIRELEASE(pDriverObjects[dw]);
  2366. }
  2367. return VERSION_MISMATCH_ERROR ;
  2368. }
  2369. DestroyDynamicArray( &arrResults );
  2370. return SUCCESS ;
  2371. }