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.

2442 lines
85 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation
  3. Module Name:
  4. GETMAC.CPP
  5. Abstract:
  6. Get MAC addresses and the related information of the network
  7. adapters that exists either on a local system or on a remote system.
  8. Author:
  9. Vasundhara .G
  10. Revision History:
  11. Vasundhara .G 26-sep-2k : Created It.
  12. Vasundhara .G 31-oct-2k : Modified It.
  13. Moved all hard coded string to header file.
  14. --*/
  15. // Include files
  16. #include "pch.h"
  17. #include "getmac.h"
  18. #include "resource.h"
  19. // function prototypes
  20. BOOL
  21. ParseCmdLine(
  22. IN DWORD argc,
  23. IN LPCTSTR argv[],
  24. OUT CHString& strMachineName,
  25. OUT CHString& strUserName,
  26. OUT CHString& strPassword,
  27. OUT CHString& strFormat,
  28. OUT BOOL *pbVerbose,
  29. OUT BOOL *pbHeader,
  30. OUT BOOL *pbUsage,
  31. OUT BOOL *pbNeedPassword
  32. );
  33. BOOL
  34. DisplayUsage(
  35. );
  36. BOOL
  37. ComInitialize(
  38. OUT IWbemLocator **ppIWbemLocator
  39. );
  40. BOOL
  41. GetMacData(
  42. OUT TARRAY arrMacData,
  43. IN LPCTSTR lpMachineName,
  44. IN IEnumWbemClassObject *pAdapterConfig,
  45. IN COAUTHIDENTITY *pAuthIdentity,
  46. IN IWbemServices *pIWbemServiceDef,
  47. IN IWbemServices *pIWbemServices,
  48. IN TARRAY arrNetProtocol
  49. );
  50. BOOL
  51. GetW2kMacData(
  52. OUT TARRAY arrMacData,
  53. IN LPCTSTR lpMachineName,
  54. IN IEnumWbemClassObject *pAdapterConfig,
  55. IN IEnumWbemClassObject *pAdapterSetting,
  56. IN IWbemServices *pIWbemServiceDef,
  57. IN IWbemServices *pIWbemServices,
  58. IN COAUTHIDENTITY *pAuthIdentity,
  59. IN TARRAY arrNetProtocol
  60. );
  61. BOOL
  62. GetTransName(
  63. IN IWbemServices *pIWbemServiceDef,
  64. IN IWbemServices *pIWbemServices,
  65. IN TARRAY arrNetProtocol,
  66. OUT TARRAY arrTransName,
  67. IN COAUTHIDENTITY *pAuthIdentity,
  68. IN LPCTSTR lpMacAddr
  69. );
  70. BOOL
  71. GetConnectionName(
  72. OUT TARRAY arrMacData,
  73. IN DWORD dwIndex,
  74. IN LPCTSTR lpFormAddr,
  75. IN IEnumWbemClassObject *pAdapterSetting,
  76. IN IWbemServices *pIWbemServiceDef
  77. );
  78. BOOL
  79. GetNwkProtocol(
  80. OUT TARRAY arrNetProtocol,
  81. IN IEnumWbemClassObject *pNetProtocol
  82. );
  83. BOOL
  84. FormatHWAddr(
  85. IN LPTSTR lpRawAddr,
  86. OUT LPTSTR lpFormattedAddr,
  87. IN LPCTSTR lpFormatter
  88. );
  89. BOOL
  90. DisplayResults(
  91. IN TARRAY arrMacData,
  92. IN LPCTSTR lpFormat,
  93. IN BOOL bHeader,
  94. IN BOOL bVerbose
  95. );
  96. BOOL
  97. CallWin32Api(
  98. OUT LPBYTE *lpBufptr,
  99. IN LPCTSTR lpMachineName,
  100. OUT DWORD *pdwNumofEntriesRead
  101. );
  102. BOOL
  103. CheckVersion(
  104. IN BOOL bLocalSystem,
  105. IN COAUTHIDENTITY *pAuthIdentity,
  106. IN IWbemServices *pIWbemServices
  107. );
  108. DWORD
  109. _cdecl _tmain(
  110. IN DWORD argc,
  111. IN LPCTSTR argv[]
  112. )
  113. /*++
  114. Routine Description:
  115. Main function which calls all the other functions depending on the
  116. option specified by the user.
  117. Arguments:
  118. [IN] argc - Number of command line arguments.
  119. [IN] argv - Array containing command line arguments.
  120. Return Value:
  121. EXIT_FAILURE if Getmac utility is not successful.
  122. EXIT_SUCCESS if Getmac utility is successful.
  123. --*/
  124. {
  125. //local variables
  126. HRESULT hRes = WBEM_S_NO_ERROR;
  127. HRESULT hResult = WBEM_S_NO_ERROR;
  128. TARRAY arrMacData = NULL;
  129. TARRAY arrNetProtocol = NULL;
  130. IWbemLocator *pIWbemLocator = NULL;
  131. IWbemServices *pIWbemServices = NULL;
  132. IWbemServices *pIWbemServiceDef = NULL;
  133. IEnumWbemClassObject *pAdapterConfig = NULL;
  134. IEnumWbemClassObject *pNetProtocol = NULL;
  135. IEnumWbemClassObject *pAdapterSetting = NULL;
  136. COAUTHIDENTITY *pAuthIdentity = NULL;
  137. BOOL bHeader = FALSE;
  138. BOOL bUsage = FALSE;
  139. BOOL bVerbose = FALSE;
  140. BOOL bFlag = FALSE;
  141. BOOL bNeedPassword = FALSE;
  142. BOOL bCloseConnection = FALSE;
  143. BOOL bLocalSystem = FALSE;
  144. if ( NULL == argv )
  145. {
  146. SetLastError( ERROR_INVALID_PARAMETER );
  147. SaveLastError();
  148. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  149. ReleaseGlobals();
  150. return EXIT_FAILURE;
  151. }
  152. try
  153. {
  154. CHString strUserName = L"\0";
  155. CHString strPassword = L"\0";
  156. CHString strMachineName = L"\0";
  157. CHString strFormat = L"\0";
  158. //initialize dynamic array
  159. arrMacData = CreateDynamicArray();
  160. if( NULL == arrMacData )
  161. {
  162. SetLastError( ERROR_NOT_ENOUGH_MEMORY );
  163. SaveLastError();
  164. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  165. ReleaseGlobals();
  166. return( EXIT_FAILURE );
  167. }
  168. //parse the command line arguments
  169. bFlag = ParseCmdLine( argc, argv, strMachineName, strUserName,
  170. strPassword, strFormat, &bVerbose, &bHeader, &bUsage,
  171. &bNeedPassword );
  172. //if syntax of command line arguments is false display the error
  173. //and exit
  174. if( FALSE == bFlag )
  175. {
  176. DestroyDynamicArray( &arrMacData );
  177. ReleaseGlobals();
  178. return( EXIT_FAILURE );
  179. }
  180. //if usage is specified at command line, display usage
  181. if( TRUE == bUsage )
  182. {
  183. DisplayUsage();
  184. DestroyDynamicArray( &arrMacData );
  185. ReleaseGlobals();
  186. return( EXIT_SUCCESS );
  187. }
  188. //initialize com library
  189. bFlag = ComInitialize( &pIWbemLocator );
  190. //failed to initialize com or get IWbemLocator interface
  191. if( FALSE == bFlag )
  192. {
  193. SAFERELEASE( pIWbemLocator );
  194. CoUninitialize();
  195. DestroyDynamicArray( &arrMacData );
  196. ReleaseGlobals();
  197. return( EXIT_FAILURE );
  198. }
  199. // connect to CIMV2 name space
  200. bFlag = ConnectWmiEx( pIWbemLocator, &pIWbemServices, strMachineName,
  201. strUserName, strPassword, &pAuthIdentity, bNeedPassword,
  202. WMI_NAMESPACE_CIMV2, &bLocalSystem );
  203. //if unable to connect to wmi exit failure
  204. if( FALSE == bFlag )
  205. {
  206. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  207. SAFERELEASE( pIWbemLocator );
  208. SAFERELEASE( pIWbemServices );
  209. WbemFreeAuthIdentity( &pAuthIdentity );
  210. CoUninitialize();
  211. DestroyDynamicArray( &arrMacData );
  212. ReleaseGlobals();
  213. return( EXIT_FAILURE );
  214. }
  215. // set the security on the obtained interface
  216. hRes = SetInterfaceSecurity( pIWbemServices, pAuthIdentity );
  217. ONFAILTHROWERROR( hRes );
  218. //connect to default namespace
  219. bFlag = ConnectWmi( pIWbemLocator, &pIWbemServiceDef, strMachineName,
  220. strUserName, strPassword, &pAuthIdentity, bNeedPassword,
  221. WMI_NAMESPACE_DEFAULT, &hResult, &bLocalSystem );
  222. ONFAILTHROWERROR( hResult );
  223. // set the security on the obtained interface
  224. hRes = SetInterfaceSecurity( pIWbemServiceDef, pAuthIdentity );
  225. ONFAILTHROWERROR( hRes );
  226. //get handle to win32_networkadapter class
  227. hRes = pIWbemServices->CreateInstanceEnum(
  228. _bstr_t( NETWORK_ADAPTER_CLASS ),
  229. WBEM_FLAG_RETURN_IMMEDIATELY,
  230. NULL, &pAdapterConfig );
  231. //if failed to enumerate win32_networkadapter class
  232. ONFAILTHROWERROR( hRes );
  233. // set the security on the obtained interface
  234. hRes = SetInterfaceSecurity( pAdapterConfig, pAuthIdentity );
  235. //if failed to set security throw error
  236. ONFAILTHROWERROR( hRes );
  237. // get handle to win32_networkprotocol
  238. hRes = pIWbemServices->CreateInstanceEnum( _bstr_t( NETWORK_PROTOCOL ),
  239. WBEM_FLAG_RETURN_IMMEDIATELY,
  240. NULL, &pNetProtocol );
  241. //if failed to enumerate win32_networkprotocol class
  242. ONFAILTHROWERROR( hRes );
  243. // set the security on the obtained interface
  244. hRes = SetInterfaceSecurity( pNetProtocol, pAuthIdentity );
  245. //if failed to set security throw error
  246. ONFAILTHROWERROR( hRes );
  247. //get handle to win32_networkadapterconfiguration class
  248. hRes = pIWbemServices->CreateInstanceEnum(
  249. _bstr_t( NETWORK_ADAPTER_CONFIG_CLASS ),
  250. WBEM_FLAG_RETURN_IMMEDIATELY,
  251. NULL, &pAdapterSetting );
  252. //if failed to enumerate win32_networkadapterconfiguration class
  253. ONFAILTHROWERROR( hRes );
  254. // set the security on the obtained interface
  255. hRes = SetInterfaceSecurity( pAdapterSetting, pAuthIdentity );
  256. //if failed to set security throw error
  257. ONFAILTHROWERROR( hRes );
  258. arrNetProtocol = CreateDynamicArray();
  259. if( NULL == arrNetProtocol )
  260. {
  261. SetLastError( ERROR_NOT_ENOUGH_MEMORY );
  262. SaveLastError();
  263. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  264. SAFERELEASE( pIWbemLocator );
  265. SAFERELEASE( pIWbemServices );
  266. SAFERELEASE( pIWbemServiceDef );
  267. SAFERELEASE( pAdapterConfig );
  268. SAFERELEASE( pAdapterSetting );
  269. SAFERELEASE( pNetProtocol );
  270. WbemFreeAuthIdentity( &pAuthIdentity );
  271. CoUninitialize();
  272. DestroyDynamicArray( &arrMacData );
  273. ReleaseGlobals();
  274. return( EXIT_FAILURE );
  275. }
  276. //enumerate all the network protocols
  277. bFlag = GetNwkProtocol( arrNetProtocol, pNetProtocol );
  278. if( FALSE == bFlag )
  279. {
  280. SAFERELEASE( pIWbemLocator );
  281. SAFERELEASE( pIWbemServices );
  282. SAFERELEASE( pIWbemServiceDef );
  283. SAFERELEASE( pAdapterConfig );
  284. SAFERELEASE( pAdapterSetting );
  285. SAFERELEASE( pNetProtocol );
  286. WbemFreeAuthIdentity( &pAuthIdentity );
  287. CoUninitialize();
  288. DestroyDynamicArray( &arrMacData );
  289. DestroyDynamicArray( &arrNetProtocol );
  290. ReleaseGlobals();
  291. return( EXIT_FAILURE );
  292. }
  293. else if( 0 == DynArrayGetCount( arrNetProtocol ) )
  294. {
  295. ShowMessage( stdout, NO_NETWOK_PROTOCOLS );
  296. SAFERELEASE( pIWbemLocator );
  297. SAFERELEASE( pIWbemServices );
  298. SAFERELEASE( pIWbemServiceDef );
  299. SAFERELEASE( pAdapterConfig );
  300. SAFERELEASE( pAdapterSetting );
  301. SAFERELEASE( pNetProtocol );
  302. WbemFreeAuthIdentity( &pAuthIdentity );
  303. CoUninitialize();
  304. }
  305. else
  306. {
  307. //check whether the remote system under query is win2k or above
  308. if( TRUE == CheckVersion( bLocalSystem,
  309. pAuthIdentity, pIWbemServices ) )
  310. {
  311. // establish connection to remote system by using
  312. //win32api function
  313. if( FALSE == bLocalSystem )
  314. {
  315. LPCWSTR pwszUser = NULL;
  316. LPCWSTR pwszPassword = NULL;
  317. // identify the password to connect to the remote system
  318. if( NULL != pAuthIdentity )
  319. {
  320. pwszPassword = pAuthIdentity->Password;
  321. if( 0 != strUserName.GetLength() )
  322. {
  323. pwszUser = strUserName;
  324. }
  325. }
  326. // connect to the remote system
  327. DWORD dwConnect = 0;
  328. dwConnect = ConnectServer( strMachineName, pwszUser,
  329. pwszPassword );
  330. // check the result
  331. if( NO_ERROR != dwConnect )
  332. {
  333. //if session already exists display warning that
  334. //credentials conflict and proceed
  335. if( ERROR_SESSION_CREDENTIAL_CONFLICT == GetLastError() )
  336. {
  337. SetLastError( ERROR_SESSION_CREDENTIAL_CONFLICT );
  338. SaveLastError();
  339. ShowLastErrorEx( stderr, SLE_TYPE_WARNING | SLE_INTERNAL );
  340. }
  341. else if( ERROR_EXTENDED_ERROR == dwConnect )
  342. {
  343. ShowLastErrorEx( stdout, SLE_TYPE_ERROR | SLE_INTERNAL );
  344. SAFERELEASE( pIWbemLocator );
  345. SAFERELEASE( pIWbemServices );
  346. SAFERELEASE( pAdapterConfig );
  347. SAFERELEASE( pNetProtocol );
  348. SAFERELEASE( pAdapterSetting );
  349. SAFERELEASE( pIWbemServiceDef );
  350. WbemFreeAuthIdentity( &pAuthIdentity );
  351. CoUninitialize();
  352. DestroyDynamicArray( &arrMacData );
  353. DestroyDynamicArray( &arrNetProtocol );
  354. ReleaseGlobals();
  355. return( EXIT_SUCCESS );
  356. }
  357. else
  358. {
  359. SetLastError( dwConnect );
  360. SaveLastError();
  361. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  362. SAFERELEASE( pIWbemLocator );
  363. SAFERELEASE( pIWbemServices );
  364. SAFERELEASE( pAdapterConfig );
  365. SAFERELEASE( pNetProtocol );
  366. SAFERELEASE( pAdapterSetting );
  367. SAFERELEASE( pIWbemServiceDef );
  368. WbemFreeAuthIdentity( &pAuthIdentity );
  369. CoUninitialize();
  370. DestroyDynamicArray( &arrMacData );
  371. DestroyDynamicArray( &arrNetProtocol );
  372. ReleaseGlobals();
  373. return( EXIT_SUCCESS );
  374. }
  375. }
  376. else
  377. {
  378. // connection needs to be closed
  379. bCloseConnection = TRUE;
  380. }
  381. }
  382. bFlag = GetW2kMacData( arrMacData, strMachineName, pAdapterConfig,
  383. pAdapterSetting, pIWbemServiceDef, pIWbemServices,
  384. pAuthIdentity, arrNetProtocol );
  385. //close the connection that is established by win32api
  386. if( TRUE == bCloseConnection )
  387. {
  388. CloseConnection( strMachineName );
  389. }
  390. }
  391. else
  392. {
  393. bFlag = GetMacData( arrMacData, strMachineName, pAdapterConfig,
  394. pAuthIdentity, pIWbemServiceDef, pIWbemServices,
  395. arrNetProtocol );
  396. }
  397. // no more required
  398. WbemFreeAuthIdentity( &pAuthIdentity );
  399. SAFERELEASE( pIWbemLocator );
  400. SAFERELEASE( pIWbemServices );
  401. SAFERELEASE( pAdapterConfig );
  402. SAFERELEASE( pNetProtocol );
  403. SAFERELEASE( pAdapterSetting );
  404. SAFERELEASE( pIWbemServiceDef );
  405. CoUninitialize();
  406. // if getmacdata() function fails exit with failure code
  407. if( FALSE == bFlag )
  408. {
  409. DestroyDynamicArray( &arrMacData );
  410. DestroyDynamicArray( &arrNetProtocol );
  411. ReleaseGlobals();
  412. return( EXIT_FAILURE );
  413. }
  414. //show the results if atleast one entry exists in dynamic array
  415. if( 0 != DynArrayGetCount( arrMacData ) )
  416. {
  417. if( TRUE == bLocalSystem )
  418. {
  419. if( 0 < strUserName.GetLength() )
  420. {
  421. ShowMessage( stderr, IGNORE_LOCALCREDENTIALS );
  422. }
  423. }
  424. bFlag = DisplayResults( arrMacData, strFormat, bHeader, bVerbose );
  425. if( FALSE == bFlag )
  426. {
  427. DestroyDynamicArray( &arrMacData );
  428. DestroyDynamicArray( &arrNetProtocol );
  429. ReleaseGlobals();
  430. return( EXIT_FAILURE );
  431. }
  432. }
  433. else
  434. {
  435. ShowMessage( stdout, NO_NETWORK_ADAPTERS );
  436. }
  437. }
  438. //successfully retrieved the data then exit with EXIT_SUCCESS code
  439. DestroyDynamicArray( &arrMacData );
  440. DestroyDynamicArray( &arrNetProtocol );
  441. ReleaseGlobals();
  442. return( EXIT_SUCCESS );
  443. }
  444. catch(_com_error& e)
  445. {
  446. WMISaveError( e.Error() );
  447. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  448. SAFERELEASE( pIWbemLocator );
  449. SAFERELEASE( pIWbemServices );
  450. SAFERELEASE( pIWbemServiceDef );
  451. SAFERELEASE( pAdapterConfig );
  452. SAFERELEASE( pAdapterSetting );
  453. SAFERELEASE( pNetProtocol );
  454. WbemFreeAuthIdentity( &pAuthIdentity );
  455. CoUninitialize();
  456. DestroyDynamicArray( &arrMacData );
  457. ReleaseGlobals();
  458. return( EXIT_FAILURE );
  459. }
  460. catch( CHeap_Exception)
  461. {
  462. SetLastError( ERROR_NOT_ENOUGH_MEMORY );
  463. SaveLastError();
  464. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  465. SAFERELEASE( pIWbemLocator );
  466. SAFERELEASE( pIWbemServices );
  467. SAFERELEASE( pAdapterConfig );
  468. SAFERELEASE( pNetProtocol );
  469. SAFERELEASE( pAdapterSetting );
  470. SAFERELEASE( pIWbemServiceDef );
  471. WbemFreeAuthIdentity( &pAuthIdentity );
  472. CoUninitialize();
  473. DestroyDynamicArray( &arrMacData );
  474. DestroyDynamicArray( &arrNetProtocol );
  475. ReleaseGlobals();
  476. return( EXIT_FAILURE );
  477. }
  478. }
  479. BOOL
  480. ParseCmdLine(
  481. IN DWORD argc,
  482. IN LPCTSTR argv[],
  483. OUT CHString& strMachineName,
  484. OUT CHString& strUserName,
  485. OUT CHString& strPassword,
  486. OUT CHString& strFormat,
  487. OUT BOOL *pbVerbose,
  488. OUT BOOL *pbHeader,
  489. OUT BOOL *pbUsage,
  490. OUT BOOL *pbNeedPassword
  491. )
  492. /*++
  493. Routine Description:
  494. This function parses the command line arguments which
  495. are obtained as input parameters and gets the values
  496. into the corresponding variables which are pass by address
  497. parameters to this function.
  498. Arguments:
  499. [IN] argc - Number of command line arguments.
  500. [IN] argv - Array containing command line arguments.
  501. [OUT] strMachineName - To hold machine name.
  502. [OUT] strUserName - To hold User name.
  503. [OUT] strPassword - To hold Password.
  504. [OUT] strFormat - To hold the formatted string.
  505. [OUT] pbVerbose - tells whether verbose option is specified.
  506. [OUT] pbHeader - Header to required or not.
  507. [OUT] pbUsage - usage is mentioned at command line.
  508. [OUT] pbNeedPassword - Set to true if -p option is not specified
  509. at cmdline.
  510. Return Value:
  511. TRUE if command parser succeeds.
  512. FALSE if command parser fails .
  513. --*/
  514. {
  515. //local varibles
  516. BOOL bFlag = FALSE;
  517. TCMDPARSER2 tcmdOptions[MAX_OPTIONS];
  518. TCMDPARSER2 *pcmdParser = NULL;
  519. // temp variables
  520. LPWSTR pwszMachineName = NULL;
  521. LPWSTR pwszUserName = NULL;
  522. LPWSTR pwszPassword = NULL;
  523. LPWSTR pwszFormat = NULL;
  524. //validate input parameters
  525. if( ( NULL == pbVerbose ) || ( NULL == pbHeader )
  526. || ( NULL == pbUsage ) || ( NULL == pbNeedPassword ) )
  527. {
  528. SetLastError( ERROR_INVALID_PARAMETER );
  529. SaveLastError();
  530. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  531. return FALSE;
  532. }
  533. try
  534. {
  535. pwszMachineName = strMachineName.GetBufferSetLength( MAX_STRING_LENGTH );
  536. pwszUserName = strUserName.GetBufferSetLength( MAX_STRING_LENGTH );
  537. pwszPassword = strPassword.GetBufferSetLength( MAX_STRING_LENGTH );
  538. pwszFormat = strFormat.GetBufferSetLength( MAX_STRING_LENGTH );
  539. // init the password with '*'
  540. StringCopy( pwszPassword, L"*", MAX_STRING_LENGTH );
  541. }
  542. catch( ... )
  543. {
  544. SetLastError( ERROR_NOT_ENOUGH_MEMORY );
  545. SaveLastError();
  546. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  547. return FALSE;
  548. }
  549. //Initialize the valid command line arguments
  550. //machine option
  551. pcmdParser = tcmdOptions + CMD_PARSE_SERVER;
  552. StringCopyA( pcmdParser->szSignature, "PARSER2\0", 8 );
  553. pcmdParser->dwType = CP_TYPE_TEXT;
  554. pcmdParser->pwszOptions = CMDOPTION_SERVER;
  555. pcmdParser->pwszFriendlyName = NULL;
  556. pcmdParser->pwszValues = NULL;
  557. pcmdParser->dwFlags = CP2_VALUE_TRIMINPUT|CP2_VALUE_NONULL;
  558. pcmdParser->dwCount = 1;
  559. pcmdParser->dwActuals = 0;
  560. pcmdParser->pValue = pwszMachineName;
  561. pcmdParser->dwLength = MAX_STRING_LENGTH;
  562. pcmdParser->pFunction = NULL;
  563. pcmdParser->pFunctionData = NULL;
  564. pcmdParser->dwReserved = 0;
  565. pcmdParser->pReserved1 = NULL;
  566. pcmdParser->pReserved2 = NULL;
  567. pcmdParser->pReserved3 = NULL;
  568. // username option
  569. pcmdParser = tcmdOptions + CMD_PARSE_USER;
  570. StringCopyA( pcmdParser->szSignature, "PARSER2\0", 8 );
  571. pcmdParser->dwType = CP_TYPE_TEXT;
  572. pcmdParser->pwszOptions = CMDOPTION_USER;
  573. pcmdParser->pwszFriendlyName = NULL;
  574. pcmdParser->pwszValues = NULL;
  575. pcmdParser->dwFlags = CP2_VALUE_TRIMINPUT|CP2_VALUE_NONULL;
  576. pcmdParser->dwCount = 1;
  577. pcmdParser->dwActuals = 0;
  578. pcmdParser->pValue = pwszUserName;
  579. pcmdParser->dwLength = MAX_STRING_LENGTH;
  580. pcmdParser->pFunction = NULL;
  581. pcmdParser->pFunctionData = NULL;
  582. pcmdParser->dwReserved = 0;
  583. pcmdParser->pReserved1 = NULL;
  584. pcmdParser->pReserved2 = NULL;
  585. pcmdParser->pReserved3 = NULL;
  586. // password option
  587. pcmdParser = tcmdOptions + CMD_PARSE_PWD;
  588. StringCopyA( pcmdParser->szSignature, "PARSER2\0", 8 );
  589. pcmdParser->dwType = CP_TYPE_TEXT;
  590. pcmdParser->pwszOptions = CMDOPTION_PASSWORD;
  591. pcmdParser->pwszFriendlyName = NULL;
  592. pcmdParser->pwszValues = NULL;
  593. pcmdParser->dwFlags = CP2_VALUE_OPTIONAL;
  594. pcmdParser->dwCount = 1;
  595. pcmdParser->dwActuals = 0;
  596. pcmdParser->pValue = pwszPassword;
  597. pcmdParser->dwLength = MAX_STRING_LENGTH;
  598. pcmdParser->pFunction = NULL;
  599. pcmdParser->pFunctionData = NULL;
  600. pcmdParser->dwReserved = 0;
  601. pcmdParser->pReserved1 = NULL;
  602. pcmdParser->pReserved2 = NULL;
  603. pcmdParser->pReserved3 = NULL;
  604. // format option
  605. pcmdParser = tcmdOptions + CMD_PARSE_FMT;
  606. StringCopyA( pcmdParser->szSignature, "PARSER2\0", 8 );
  607. pcmdParser->dwType = CP_TYPE_TEXT;
  608. pcmdParser->pwszOptions = CMDOPTION_FORMAT;
  609. pcmdParser->pwszFriendlyName = NULL;
  610. pcmdParser->pwszValues = FORMAT_TYPES;
  611. pcmdParser->dwFlags = CP2_MODE_VALUES|CP2_VALUE_TRIMINPUT|CP2_VALUE_NONULL;
  612. pcmdParser->dwCount = 1;
  613. pcmdParser->dwActuals = 0;
  614. pcmdParser->pValue = pwszFormat;
  615. pcmdParser->dwLength = MAX_STRING_LENGTH;
  616. pcmdParser->pFunction = NULL;
  617. pcmdParser->pFunctionData = NULL;
  618. pcmdParser->dwReserved = 0;
  619. pcmdParser->pReserved1 = NULL;
  620. pcmdParser->pReserved2 = NULL;
  621. pcmdParser->pReserved3 = NULL;
  622. //usage option
  623. pcmdParser = tcmdOptions + CMD_PARSE_USG;
  624. StringCopyA( pcmdParser->szSignature, "PARSER2\0", 8 );
  625. pcmdParser->dwType = CP_TYPE_BOOLEAN;
  626. pcmdParser->pwszOptions = CMDOPTION_USAGE;
  627. pcmdParser->pwszFriendlyName = NULL;
  628. pcmdParser->pwszValues = NULL;
  629. pcmdParser->dwFlags = CP2_USAGE;
  630. pcmdParser->dwCount = 1;
  631. pcmdParser->dwActuals = 0;
  632. pcmdParser->pValue = pbUsage;
  633. pcmdParser->dwLength = MAX_STRING_LENGTH;
  634. pcmdParser->pFunction = NULL;
  635. pcmdParser->pFunctionData = NULL;
  636. pcmdParser->dwReserved = 0;
  637. pcmdParser->pReserved1 = NULL;
  638. pcmdParser->pReserved2 = NULL;
  639. pcmdParser->pReserved3 = NULL;
  640. //header option
  641. pcmdParser = tcmdOptions + CMD_PARSE_HRD;
  642. StringCopyA( pcmdParser->szSignature, "PARSER2\0", 8 );
  643. pcmdParser->dwType = CP_TYPE_BOOLEAN;
  644. pcmdParser->pwszOptions = CMDOPTION_HEADER;
  645. pcmdParser->pwszFriendlyName = NULL;
  646. pcmdParser->pwszValues = NULL;
  647. pcmdParser->dwFlags = 0;
  648. pcmdParser->dwCount = 1;
  649. pcmdParser->dwActuals = 0;
  650. pcmdParser->pValue = pbHeader;
  651. pcmdParser->dwLength = MAX_STRING_LENGTH;
  652. pcmdParser->pFunction = NULL;
  653. pcmdParser->pFunctionData = NULL;
  654. pcmdParser->dwReserved = 0;
  655. pcmdParser->pReserved1 = NULL;
  656. pcmdParser->pReserved2 = NULL;
  657. pcmdParser->pReserved3 = NULL;
  658. //verbose option
  659. pcmdParser = tcmdOptions + CMD_PARSE_VER;
  660. StringCopyA( pcmdParser->szSignature, "PARSER2\0", 8 );
  661. pcmdParser->dwType = CP_TYPE_BOOLEAN;
  662. pcmdParser->pwszOptions = CMDOPTION_VERBOSE;
  663. pcmdParser->pwszFriendlyName = NULL;
  664. pcmdParser->pwszValues = NULL;
  665. pcmdParser->dwFlags = 0;
  666. pcmdParser->dwCount = 1;
  667. pcmdParser->dwActuals = 0;
  668. pcmdParser->pValue = pbVerbose;
  669. pcmdParser->dwLength = MAX_STRING_LENGTH;
  670. pcmdParser->pFunction = NULL;
  671. pcmdParser->pFunctionData = NULL;
  672. pcmdParser->dwReserved = 0;
  673. pcmdParser->pReserved1 = NULL;
  674. pcmdParser->pReserved2 = NULL;
  675. pcmdParser->pReserved3 = NULL;
  676. //parse the command line arguments
  677. bFlag = DoParseParam2( argc, argv, -1, SIZE_OF_ARRAY(tcmdOptions), tcmdOptions, 0);
  678. // release buffers allocated for temp variables
  679. strMachineName.ReleaseBuffer();
  680. strUserName.ReleaseBuffer();
  681. strPassword.ReleaseBuffer();
  682. strFormat.ReleaseBuffer();
  683. //if syntax of command line arguments is false display the error and exit
  684. if( FALSE == bFlag )
  685. {
  686. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  687. return( FALSE );
  688. }
  689. //if usage is specified at command line, then check whether any other
  690. //arguments are entered at the command line and if so display syntax
  691. //error
  692. if( ( TRUE == *pbUsage ) && ( 2 < argc ) )
  693. {
  694. SetLastError( (DWORD) MK_E_SYNTAX );
  695. SaveLastError();
  696. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  697. ShowMessage( stderr, ERROR_TYPE_REQUEST );
  698. return( FALSE );
  699. }
  700. // return false if username is entered without machine name
  701. if ( ( 0 != tcmdOptions[ CMD_PARSE_USER ].dwActuals ) &&
  702. ( 0 == tcmdOptions[ CMD_PARSE_SERVER ].dwActuals ) )
  703. {
  704. ShowMessage( stderr, ERROR_USER_WITH_NOSERVER );
  705. return( FALSE );
  706. }
  707. //if password entered without username then return false
  708. if( ( 0 == tcmdOptions[ CMD_PARSE_USER ].dwActuals ) &&
  709. ( 0 != tcmdOptions[ CMD_PARSE_PWD ].dwActuals ) )
  710. {
  711. ShowMessage( stderr, ERROR_SERVER_WITH_NOPASSWORD );
  712. return( FALSE );
  713. }
  714. //if no header is specified with list format return FALSE else return TRUE
  715. if( ( 0 == ( StringCompare( GetResString( FR_LIST ), strFormat, TRUE, 0 ) ) )
  716. && ( TRUE == *pbHeader ) )
  717. {
  718. ShowMessage( stderr, ERROR_INVALID_HEADER_OPTION );
  719. return( FALSE );
  720. }
  721. //if -s is entered with empty string
  722. if( ( 0 != tcmdOptions[ CMD_PARSE_SERVER ].dwActuals ) &&
  723. ( 0 == StringLength( strMachineName, 0 ) ) )
  724. {
  725. ShowMessage( stderr, ERROR_NULL_SERVER );
  726. return( FALSE );
  727. }
  728. //if -u is entered with empty string
  729. if( ( 0 != tcmdOptions[ CMD_PARSE_USER ].dwActuals ) &&
  730. ( 0 == StringLength( strUserName, 0 ) ) )
  731. {
  732. ShowMessage( stderr, ERROR_NULL_USER );
  733. return( FALSE );
  734. }
  735. if ( 0 != tcmdOptions[ CMD_PARSE_PWD ].dwActuals &&
  736. ( 0 == strPassword.Compare( L"*" ) ) )
  737. {
  738. // user wants the utility to prompt for the password before trying to connect
  739. *pbNeedPassword = TRUE;
  740. }
  741. else if ( ( 0 == tcmdOptions[ CMD_PARSE_PWD ].dwActuals ) &&
  742. ( 0 != tcmdOptions[ CMD_PARSE_SERVER ].dwActuals ||
  743. 0 !=tcmdOptions[ CMD_PARSE_USER ].dwActuals ) )
  744. {
  745. // -s, -u is specified without password ...
  746. // utility needs to try to connect first and if it fails then prompt for the password
  747. *pbNeedPassword = TRUE;
  748. strPassword.Empty();
  749. }
  750. //return true
  751. return( TRUE );
  752. }
  753. BOOL
  754. DisplayUsage(
  755. )
  756. /*++
  757. Routine Description:
  758. This function displays the usage of getmac.exe.
  759. Arguments:
  760. None.
  761. Return Value:
  762. TRUE
  763. --*/
  764. {
  765. DWORD dwIndex = 0;
  766. //redirect the usage to the console
  767. for( dwIndex = IDS_USAGE_BEGINING; dwIndex <= USAGE_END; dwIndex++ )
  768. {
  769. ShowMessage( stdout, GetResString( dwIndex ) );
  770. }
  771. return (TRUE);
  772. }
  773. BOOL
  774. ComInitialize(
  775. OUT IWbemLocator **ppIWbemLocator
  776. )
  777. /*++
  778. Routine Description:
  779. This function initializes the com and set security
  780. Arguments:
  781. [OUT] ppIWbemLocator - pointer to IWbemLocator.
  782. Return Value:
  783. TRUE if initialize is successful.
  784. FALSE if initialization fails.
  785. --*/
  786. {
  787. HRESULT hRes = S_OK;
  788. try
  789. {
  790. hRes = CoInitializeEx( NULL, COINIT_APARTMENTTHREADED );
  791. // COM initialization failed
  792. ONFAILTHROWERROR( hRes );
  793. // Initialize COM security for DCOM services, Adjust security to
  794. // allow client impersonation
  795. hRes = CoInitializeSecurity( NULL, -1, NULL, NULL,
  796. RPC_C_AUTHN_LEVEL_NONE,
  797. RPC_C_IMP_LEVEL_IMPERSONATE,
  798. NULL, EOAC_NONE, 0 );
  799. // COM security failed
  800. ONFAILTHROWERROR( hRes );
  801. //get IWbemLocator
  802. hRes = CoCreateInstance( CLSID_WbemLocator, 0,
  803. CLSCTX_INPROC_SERVER, IID_IWbemLocator,
  804. (LPVOID *) ppIWbemLocator );
  805. //unable to get IWbemLocator
  806. ONFAILTHROWERROR( hRes );
  807. }
  808. catch( _com_error& e )
  809. {
  810. WMISaveError( e.Error() );
  811. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  812. return( FALSE );
  813. }
  814. return( TRUE );
  815. }
  816. BOOL
  817. GetMacData(
  818. OUT TARRAY arrMacData,
  819. IN LPCTSTR lpMachineName,
  820. IN IEnumWbemClassObject *pAdapterConfig,
  821. IN COAUTHIDENTITY *pAuthIdentity,
  822. IN IWbemServices *pIWbemServiceDef,
  823. IN IWbemServices *pIWbemServices,
  824. IN TARRAY arrNetProtocol
  825. )
  826. /*++
  827. Routine Description:
  828. This function gets the media access control address of
  829. the network adapters which are there either in the local
  830. or on the remote network system of OS whistler and above.
  831. Arguments:
  832. [OUT] arrMacData - contains the MAC and other data of the
  833. network adapter.
  834. [IN] lpMachineName - holds machine name.
  835. [IN] pAdapterConfig - interface to win32_networkadapter class.
  836. [IN] pAuthIdentity - pointer to authentication structure.
  837. [IN] pIWbemServiceDef - interface to default name space.
  838. [IN] pIWbemServices - interface to cimv2 name space.
  839. [IN] arrNetProtocol - interface to win32_networkprotocol class.
  840. Return Value:
  841. TRUE if getmacdata is successful.
  842. FALSE if getmacdata failed.
  843. --*/
  844. {
  845. //local variables
  846. HRESULT hRes = S_OK;
  847. IWbemClassObject *pAdapConfig = NULL;
  848. DWORD dwReturned = 1;
  849. DWORD i = 0;
  850. BOOL bFlag = TRUE;
  851. TARRAY arrTransName = NULL;
  852. VARIANT varTemp;
  853. VARIANT varStatus;
  854. //validate input parametrs
  855. if( ( NULL == arrMacData ) || ( NULL == lpMachineName ) ||
  856. ( NULL == pAdapterConfig ) || ( NULL == pIWbemServiceDef ) ||
  857. ( NULL == pIWbemServices ) || ( NULL == arrNetProtocol ) )
  858. {
  859. SetLastError( ERROR_INVALID_PARAMETER );
  860. SaveLastError();
  861. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  862. return FALSE;
  863. }
  864. //get the mac ,network adapter type and other details
  865. VariantInit( &varTemp );
  866. VariantInit( &varStatus );
  867. try
  868. {
  869. CHString strAType = L"\0";
  870. while ( ( 1 == dwReturned ) )
  871. {
  872. // Enumerate through the resultset.
  873. hRes = pAdapterConfig->Next( WBEM_INFINITE,
  874. 1,
  875. &pAdapConfig,
  876. &dwReturned );
  877. ONFAILTHROWERROR( hRes );
  878. if( 0 == dwReturned )
  879. {
  880. break;
  881. }
  882. hRes = pAdapConfig->Get( NETCONNECTION_STATUS, 0 , &varStatus,
  883. 0, NULL );
  884. ONFAILTHROWERROR( hRes );
  885. if ( VT_NULL == varStatus.vt )
  886. {
  887. continue;
  888. }
  889. DynArrayAppendRow( arrMacData, 0 );
  890. hRes = pAdapConfig->Get( HOST_NAME, 0 , &varTemp, 0, NULL );
  891. ONFAILTHROWERROR( hRes );
  892. if( VT_NULL != varTemp.vt && VT_EMPTY != varTemp.vt )
  893. {
  894. strAType = varTemp.bstrVal;
  895. }
  896. else
  897. {
  898. strAType = NOT_AVAILABLE;
  899. }
  900. VariantClear( &varTemp );
  901. DynArrayAppendString2( arrMacData, i, strAType, 0 );//machine name
  902. hRes = pAdapConfig->Get( NETCONNECTION_ID, 0 , &varTemp, 0, NULL );
  903. ONFAILTHROWERROR( hRes );
  904. if( VT_NULL != varTemp.vt && VT_EMPTY != varTemp.vt )
  905. {
  906. strAType = varTemp.bstrVal;
  907. }
  908. else
  909. {
  910. strAType = NOT_AVAILABLE;
  911. }
  912. VariantClear( &varTemp );
  913. DynArrayAppendString2( arrMacData, i, strAType, 0 );// connection name
  914. hRes = pAdapConfig->Get( NAME, 0 , &varTemp, 0, NULL );
  915. ONFAILTHROWERROR( hRes );
  916. if( VT_NULL != varTemp.vt && VT_EMPTY != varTemp.vt )
  917. {
  918. strAType = varTemp.bstrVal;
  919. }
  920. else
  921. {
  922. strAType = NOT_AVAILABLE;
  923. }
  924. VariantClear( &varTemp );
  925. DynArrayAppendString2( arrMacData, i, strAType, 0 );//Network adapter
  926. hRes = pAdapConfig->Get( ADAPTER_MACADDR, 0 , &varTemp, 0, NULL );
  927. ONFAILTHROWERROR( hRes );
  928. if( VT_NULL != varTemp.vt && VT_EMPTY != varTemp.vt )
  929. {
  930. strAType = varTemp.bstrVal;
  931. for( int j = 2; j < strAType.GetLength();j += 3 )
  932. {
  933. strAType.SetAt( j, HYPHEN_CHAR );
  934. }
  935. }
  936. else if( 0 == varStatus.lVal )
  937. {
  938. strAType = DISABLED;
  939. }
  940. else
  941. {
  942. strAType = NOT_AVAILABLE;
  943. }
  944. VariantClear( &varTemp );
  945. DynArrayAppendString2( arrMacData, i, strAType, 0 ); //MAC address
  946. arrTransName = CreateDynamicArray();
  947. if( NULL == arrTransName )
  948. {
  949. SetLastError( ERROR_NOT_ENOUGH_MEMORY );
  950. SaveLastError();
  951. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  952. VariantClear( &varTemp );
  953. VariantClear( &varStatus );
  954. SAFERELEASE( pAdapConfig );
  955. return( FALSE );
  956. }
  957. if( 2 == varStatus.lVal )
  958. {
  959. hRes = pAdapConfig->Get( DEVICE_ID, 0 , &varTemp, 0, NULL );
  960. ONFAILTHROWERROR( hRes );
  961. if( VT_NULL != varTemp.vt && VT_EMPTY != varTemp.vt )
  962. {
  963. strAType = ( LPCWSTR ) _bstr_t( varTemp );
  964. }
  965. bFlag = GetTransName( pIWbemServiceDef, pIWbemServices,
  966. arrNetProtocol, arrTransName,pAuthIdentity, strAType );
  967. if( FALSE == bFlag )
  968. {
  969. VariantClear( &varTemp );
  970. VariantClear( &varStatus );
  971. SAFERELEASE( pAdapConfig );
  972. DestroyDynamicArray( &arrTransName );
  973. return( FALSE );
  974. }
  975. }
  976. else
  977. {
  978. switch(varStatus.lVal)
  979. {
  980. case 0 :
  981. strAType = GetResString( IDS_DISCONNECTED );
  982. break;
  983. case 1 :
  984. strAType = GetResString( IDS_CONNECTING );
  985. break;
  986. case 3 :
  987. strAType = GetResString( IDS_DISCONNECTING );
  988. break;
  989. case 4 :
  990. strAType = GetResString( IDS_HWNOTPRESENT );
  991. break;
  992. case 5 :
  993. strAType = GetResString( IDS_HWDISABLED );
  994. break;
  995. case 6 :
  996. strAType = GetResString( IDS_HWMALFUNCTION );
  997. break;
  998. case 7 :
  999. strAType = GetResString( IDS_MEDIADISCONNECTED );
  1000. break;
  1001. case 8 :
  1002. strAType = GetResString( IDS_AUTHENTICATION );
  1003. break;
  1004. case 9 :
  1005. strAType = GetResString( IDS_AUTHENSUCCEEDED );
  1006. break;
  1007. case 10 :
  1008. strAType = GetResString( IDS_AUTHENFAILED );
  1009. break;
  1010. default :
  1011. strAType = NOT_AVAILABLE;
  1012. break;
  1013. }//switch
  1014. if( strAType == L"\0" )
  1015. {
  1016. VariantClear( &varTemp );
  1017. VariantClear( &varStatus );
  1018. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  1019. SAFERELEASE( pAdapConfig );
  1020. return( FALSE );
  1021. }
  1022. DynArrayAppendString( arrTransName, strAType, 0 );
  1023. }//else
  1024. //insert transport name array into results array
  1025. DynArrayAppendEx2( arrMacData, i, arrTransName );
  1026. i++;
  1027. SAFERELEASE( pAdapConfig );
  1028. }//while
  1029. }//try
  1030. catch( _com_error& e )
  1031. {
  1032. VariantClear( &varTemp );
  1033. VariantClear( &varStatus );
  1034. WMISaveError( e.Error() );
  1035. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  1036. SAFERELEASE( pAdapConfig );
  1037. return( FALSE );
  1038. }
  1039. catch( CHeap_Exception)
  1040. {
  1041. SetLastError( ERROR_NOT_ENOUGH_MEMORY );
  1042. SaveLastError();
  1043. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  1044. VariantClear( &varTemp );
  1045. VariantClear( &varStatus );
  1046. SAFERELEASE( pAdapConfig );
  1047. return( FALSE );
  1048. }
  1049. //if arrmacdata and not arrtransname then set transname to N/A
  1050. if( 0 < DynArrayGetCount( arrMacData ) &&
  1051. DynArrayGetCount( arrTransName ) <= 0 )
  1052. {
  1053. DynArrayAppendString( arrTransName, NOT_AVAILABLE, 0 );
  1054. DynArrayAppendEx2( arrMacData, i, arrTransName );
  1055. }
  1056. VariantClear( &varTemp );
  1057. VariantClear( &varStatus );
  1058. SAFERELEASE( pAdapConfig );
  1059. return( TRUE );
  1060. }
  1061. BOOL
  1062. DisplayResults(
  1063. IN TARRAY arrMacData,
  1064. IN LPCTSTR lpFormat,
  1065. IN BOOL bHeader,
  1066. IN BOOL bVerbose
  1067. )
  1068. /*++
  1069. Routine Description:
  1070. This function display the results in the specified format.
  1071. Arguments:
  1072. [IN] arrMacData - Data to be displayed on to the console.
  1073. [IN] lpFormat - Holds the formatter string in which the results
  1074. are to be displayed.
  1075. [IN] bHeader - Holds whether the header has to be dislpayed in the
  1076. results or not.
  1077. [IN] bVerbose - Hold whether verbose option is specified or not.
  1078. Return Value:
  1079. TRUE if successful
  1080. FALSE if failed to display results..
  1081. --*/
  1082. {
  1083. //local variables
  1084. DWORD dwFormat = 0;
  1085. TCOLUMNS tColumn[MAX_COLUMNS];
  1086. //validate input parameters
  1087. if( NULL == arrMacData )
  1088. {
  1089. SetLastError( ERROR_INVALID_PARAMETER );
  1090. SaveLastError();
  1091. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  1092. return( FALSE );
  1093. }
  1094. for( DWORD i = 0; i < MAX_COLUMNS; i++ )
  1095. {
  1096. tColumn[i].pFunction = NULL;
  1097. tColumn[i].pFunctionData = NULL;
  1098. StringCopy( tColumn[i].szFormat, L"\0", MAX_STRING_LENGTH );
  1099. }
  1100. //host name
  1101. tColumn[ SH_RES_HOST ].dwWidth = HOST_NAME_WIDTH;
  1102. /*if( TRUE == bVerbose )
  1103. tColumn[ SH_RES_HOST ].dwFlags = SR_TYPE_STRING;
  1104. else*/
  1105. tColumn[ SH_RES_HOST ].dwFlags = SR_TYPE_STRING | SR_HIDECOLUMN;
  1106. StringCopy( tColumn[ SH_RES_HOST ].szColumn, RES_HOST_NAME, MAX_STRING_LENGTH );
  1107. //connection name
  1108. tColumn[ SH_RES_CON ].dwWidth = CONN_NAME_WIDTH;
  1109. if( TRUE == bVerbose )
  1110. {
  1111. tColumn[ SH_RES_CON ].dwFlags = SR_TYPE_STRING;
  1112. }
  1113. else
  1114. {
  1115. tColumn[ SH_RES_CON ].dwFlags = SR_TYPE_STRING | SR_HIDECOLUMN;
  1116. }
  1117. StringCopy( tColumn[ SH_RES_CON ].szColumn, RES_CONNECTION_NAME, MAX_STRING_LENGTH );
  1118. //Adpater type
  1119. tColumn[ SH_RES_TYPE ].dwWidth = ADAPT_TYPE_WIDTH;
  1120. if( TRUE == bVerbose )
  1121. {
  1122. tColumn[ SH_RES_TYPE ].dwFlags = SR_TYPE_STRING;
  1123. }
  1124. else
  1125. {
  1126. tColumn[ SH_RES_TYPE ].dwFlags = SR_TYPE_STRING | SR_HIDECOLUMN;
  1127. }
  1128. StringCopy( tColumn[ SH_RES_TYPE ].szColumn, RES_ADAPTER_TYPE, MAX_STRING_LENGTH );
  1129. //mac address
  1130. tColumn[ SH_RES_MAC ].dwWidth = MAC_ADDR_WIDTH;
  1131. tColumn[ SH_RES_MAC ].dwFlags = SR_TYPE_STRING;
  1132. StringCopy( tColumn[ SH_RES_MAC ].szColumn, RES_MAC_ADDRESS, MAX_STRING_LENGTH );
  1133. //transport name
  1134. tColumn[ SH_RES_TRAN ].dwWidth = TRANS_NAME_WIDTH;
  1135. tColumn[ SH_RES_TRAN ].dwFlags = SR_ARRAY | SR_TYPE_STRING | SR_NO_TRUNCATION ;
  1136. StringCopy( tColumn[ SH_RES_TRAN ].szColumn, RES_TRANS_NAME, MAX_STRING_LENGTH );
  1137. //get the display results formatter string
  1138. if( NULL == lpFormat )
  1139. {
  1140. dwFormat = SR_FORMAT_TABLE;
  1141. }
  1142. else if( 0 == StringCompare( GetResString( FR_LIST ), lpFormat, TRUE, 0 ) )
  1143. {
  1144. dwFormat = SR_FORMAT_LIST;
  1145. }
  1146. else if ( 0 == StringCompare( GetResString( FR_CSV ), lpFormat, TRUE, 0 ) )
  1147. {
  1148. dwFormat = SR_FORMAT_CSV;
  1149. }
  1150. else if( 0 == StringCompare( GetResString( FR_TABLE ), lpFormat, TRUE, 0 ) )
  1151. {
  1152. dwFormat = SR_FORMAT_TABLE;
  1153. }
  1154. else
  1155. {
  1156. dwFormat = SR_FORMAT_TABLE;
  1157. }
  1158. if( SR_FORMAT_CSV != dwFormat )
  1159. {
  1160. ShowMessage( stdout, NEW_LINE );
  1161. }
  1162. //look for header and display accordingly
  1163. if( TRUE == bHeader )
  1164. {
  1165. ShowResults( 5, tColumn, dwFormat | SR_NOHEADER, arrMacData );
  1166. }
  1167. else
  1168. {
  1169. ShowResults( 5, tColumn, dwFormat, arrMacData );
  1170. }
  1171. return( TRUE );
  1172. }
  1173. BOOL
  1174. GetTransName(
  1175. IN IWbemServices *pIWbemServiceDef,
  1176. IN IWbemServices *pIWbemServices,
  1177. IN TARRAY arrNetProtocol,
  1178. OUT TARRAY arrTransName,
  1179. IN COAUTHIDENTITY *pAuthIdentity,
  1180. IN LPCTSTR lpDeviceId
  1181. )
  1182. /*++
  1183. Routine Description:
  1184. This function gets transport name of the network adapter.
  1185. Arguments:
  1186. [IN] pIWbemServiceDef - interface to default name space.
  1187. [IN] pIWbemServices - interface to CIMV2 name space.
  1188. [IN] pNetProtocol - interface to win32_networkprotocol.
  1189. [OUT] arrTransName - Holds the transport name.
  1190. [IN] pAuthIdentity - pointer to authentication structure.
  1191. [IN] lpDeviceId - Holds the device id.
  1192. Return Value:
  1193. TRUE if successful.
  1194. FALSE if failed to get transport name.
  1195. --*/
  1196. {
  1197. BOOL bFlag = FALSE;
  1198. DWORD dwCount = 0;
  1199. DWORD i = 0;
  1200. DWORD dwOnce = 0;
  1201. HRESULT hRes = 0;
  1202. DWORD dwReturned = 1;
  1203. IWbemClassObject *pSetting = NULL;
  1204. IWbemClassObject *pClass = NULL;
  1205. IWbemClassObject *pOutInst = NULL;
  1206. IWbemClassObject *pInClass = NULL;
  1207. IWbemClassObject *pInInst = NULL;
  1208. IEnumWbemClassObject *pAdapterSetting = NULL;
  1209. VARIANT varTemp;
  1210. LPTSTR lpKeyPath = NULL;
  1211. SAFEARRAY *safeArray = NULL;
  1212. LONG lLBound = 0;
  1213. LONG lUBound = 0;
  1214. LONG lIndex = 0;
  1215. TCHAR szTemp[ MAX_STRING_LENGTH ] = _T( "\0" );
  1216. //validate input parameters
  1217. if( ( NULL == pIWbemServiceDef ) || ( NULL == pIWbemServices ) ||
  1218. ( NULL == arrNetProtocol ) || ( NULL == arrTransName ) ||
  1219. ( NULL == lpDeviceId ) )
  1220. {
  1221. SetLastError( ERROR_INVALID_PARAMETER );
  1222. SaveLastError();
  1223. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  1224. return FALSE;
  1225. }
  1226. VariantInit( &varTemp );
  1227. try
  1228. {
  1229. CHString strAType = L"\0";
  1230. CHString strSetId = L"\0";
  1231. StringCopy( szTemp, ASSOCIATOR_QUERY, SIZE_OF_ARRAY( szTemp ) );
  1232. StringConcat( szTemp, lpDeviceId, SIZE_OF_ARRAY( szTemp ) );
  1233. StringConcat( szTemp, ASSOCIATOR_QUERY1, SIZE_OF_ARRAY( szTemp ) );
  1234. hRes = pIWbemServices->ExecQuery( _bstr_t( QUERY_LANGUAGE ), _bstr_t(szTemp),
  1235. WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pAdapterSetting );
  1236. ONFAILTHROWERROR( hRes );
  1237. hRes = SetInterfaceSecurity( pAdapterSetting, pAuthIdentity );
  1238. //if failed to set security throw error
  1239. ONFAILTHROWERROR( hRes );
  1240. //get setting id
  1241. while ( 1 == dwReturned )
  1242. {
  1243. // Enumerate through the resultset.
  1244. hRes = pAdapterSetting->Next( WBEM_INFINITE,
  1245. 1,
  1246. &pSetting,
  1247. &dwReturned );
  1248. ONFAILTHROWERROR( hRes );
  1249. if( 0 == dwReturned )
  1250. {
  1251. break;
  1252. }
  1253. hRes = pSetting->Get( SETTING_ID, 0 , &varTemp, 0, NULL );
  1254. ONFAILTHROWERROR( hRes );
  1255. if( VT_NULL != varTemp.vt && VT_EMPTY != varTemp.vt )
  1256. {
  1257. strSetId = ( LPCWSTR ) _bstr_t( varTemp );
  1258. break;
  1259. }
  1260. }//while
  1261. dwCount = DynArrayGetCount( arrNetProtocol );
  1262. lpKeyPath = ( LPTSTR ) AllocateMemory( MAX_RES_STRING );
  1263. if( NULL == lpKeyPath )
  1264. {
  1265. SetLastError( ERROR_NOT_ENOUGH_MEMORY );
  1266. SaveLastError();
  1267. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  1268. SAFERELEASE( pSetting );
  1269. SAFERELEASE( pAdapterSetting );
  1270. FreeMemory( (LPVOID*)&lpKeyPath );
  1271. return( FALSE );
  1272. }
  1273. for( i = 0; i < dwCount; i++ )
  1274. {
  1275. if( 0 == StringCompare( ( DynArrayItemAsString( arrNetProtocol, i ) ),
  1276. NETBIOS, TRUE, 0 ) )
  1277. {
  1278. continue;
  1279. }
  1280. hRes = pIWbemServiceDef->GetObject( _bstr_t( WMI_REGISTRY ), 0, NULL,
  1281. &pClass, NULL );
  1282. ONFAILTHROWERROR( hRes );
  1283. hRes = pClass->GetMethod( WMI_REGISTRY_M_MSTRINGVALUE, 0,
  1284. &pInClass, NULL );
  1285. ONFAILTHROWERROR( hRes );
  1286. hRes = pInClass->SpawnInstance(0, &pInInst);
  1287. ONFAILTHROWERROR( hRes );
  1288. varTemp.vt = VT_I4;
  1289. varTemp.lVal = WMI_HKEY_LOCAL_MACHINE;
  1290. hRes = pInInst->Put( WMI_REGISTRY_IN_HDEFKEY, 0, &varTemp, 0 );
  1291. VariantClear( &varTemp );
  1292. ONFAILTHROWERROR( hRes );
  1293. StringCopy( lpKeyPath, TRANSPORT_KEYPATH,
  1294. ( GetBufferSize( lpKeyPath )/ sizeof( WCHAR ) ) );
  1295. StringConcat( lpKeyPath, DynArrayItemAsString( arrNetProtocol, i ),
  1296. ( GetBufferSize( lpKeyPath )/ sizeof( WCHAR ) ) );
  1297. StringConcat( lpKeyPath, LINKAGE,
  1298. ( GetBufferSize( lpKeyPath )/ sizeof( WCHAR ) ) );
  1299. varTemp.vt = VT_BSTR;
  1300. varTemp.bstrVal = SysAllocString( lpKeyPath );
  1301. if( NULL == varTemp.bstrVal )
  1302. {
  1303. hRes = ERROR_NOT_ENOUGH_MEMORY;
  1304. ONFAILTHROWERROR( hRes );
  1305. }
  1306. hRes = pInInst->Put( WMI_REGISTRY_IN_SUBKEY, 0, &varTemp, 0 );
  1307. VariantClear( &varTemp );
  1308. ONFAILTHROWERROR( hRes );
  1309. varTemp.vt = VT_BSTR;
  1310. varTemp.bstrVal = SysAllocString( ROUTE );
  1311. if( NULL == varTemp.bstrVal )
  1312. {
  1313. hRes = ERROR_NOT_ENOUGH_MEMORY;
  1314. ONFAILTHROWERROR( hRes );
  1315. }
  1316. hRes = pInInst->Put( WMI_REGISTRY_IN_VALUENAME, 0, &varTemp, 0 );
  1317. VariantClear( &varTemp );
  1318. ONFAILTHROWERROR( hRes );
  1319. // Call the method.
  1320. hRes = pIWbemServiceDef->ExecMethod( _bstr_t( WMI_REGISTRY ),
  1321. _bstr_t( WMI_REGISTRY_M_MSTRINGVALUE ), 0, NULL, pInInst,
  1322. &pOutInst, NULL );
  1323. ONFAILTHROWERROR( hRes );
  1324. varTemp.vt = VT_I4;
  1325. hRes = pOutInst->Get( WMI_REGISTRY_OUT_RETURNVALUE, 0, &varTemp,
  1326. 0, 0 );
  1327. ONFAILTHROWERROR( hRes );
  1328. if( 0 == varTemp.lVal )
  1329. {
  1330. VariantClear( &varTemp );
  1331. varTemp.vt = VT_BSTR;
  1332. hRes = pOutInst->Get( WMI_REGISTRY_OUT_VALUE, 0, &varTemp,
  1333. 0, 0);
  1334. ONFAILTHROWERROR( hRes );
  1335. if( VT_NULL != varTemp.vt && VT_EMPTY != varTemp.vt )
  1336. {
  1337. safeArray = (SAFEARRAY *)varTemp.parray;
  1338. //get the number of elements (subkeys)
  1339. if( NULL != safeArray )
  1340. {
  1341. hRes = SafeArrayGetLBound( safeArray, 1, &lLBound );
  1342. ONFAILTHROWERROR( hRes );
  1343. hRes = SafeArrayGetUBound( safeArray, 1, &lUBound );
  1344. ONFAILTHROWERROR( hRes );
  1345. bFlag = FALSE;
  1346. for( lIndex = lLBound; lIndex <= lUBound; lIndex++ )
  1347. {
  1348. hRes = SafeArrayGetElement( safeArray, &lIndex,
  1349. &V_UI1( &varTemp ) );
  1350. ONFAILTHROWERROR( hRes );
  1351. strAType = V_BSTR( &varTemp );
  1352. LPTSTR lpSubStr = _tcsstr( strAType, strSetId );
  1353. if( NULL != lpSubStr )
  1354. {
  1355. bFlag = TRUE;
  1356. break;
  1357. }
  1358. }
  1359. }
  1360. }
  1361. }
  1362. if( TRUE == bFlag )
  1363. {
  1364. varTemp.vt = VT_BSTR;
  1365. varTemp.bstrVal = SysAllocString( EXPORT );
  1366. hRes = pInInst->Put( WMI_REGISTRY_IN_VALUENAME, 0, &varTemp, 0 );
  1367. VariantClear( &varTemp );
  1368. ONFAILTHROWERROR( hRes );
  1369. // Call the method.
  1370. hRes = pIWbemServiceDef->ExecMethod( _bstr_t( WMI_REGISTRY ),
  1371. _bstr_t( WMI_REGISTRY_M_MSTRINGVALUE ), 0, NULL, pInInst,
  1372. &pOutInst, NULL );
  1373. ONFAILTHROWERROR( hRes );
  1374. varTemp.vt = VT_I4;
  1375. hRes = pOutInst->Get( WMI_REGISTRY_OUT_RETURNVALUE, 0,
  1376. &varTemp, 0, 0 );
  1377. ONFAILTHROWERROR( hRes );
  1378. if( 0 == varTemp.lVal )
  1379. {
  1380. VariantClear( &varTemp );
  1381. varTemp.vt = VT_BSTR;
  1382. hRes = pOutInst->Get( WMI_REGISTRY_OUT_VALUE, 0,
  1383. &varTemp, 0, 0);
  1384. ONFAILTHROWERROR( hRes );
  1385. if( VT_NULL != varTemp.vt && VT_EMPTY != varTemp.vt )
  1386. {
  1387. safeArray = varTemp.parray;
  1388. //get the number of elements (subkeys)
  1389. if( NULL != safeArray )
  1390. {
  1391. hRes = SafeArrayGetLBound( safeArray, 1, &lLBound );
  1392. ONFAILTHROWERROR( hRes );
  1393. hRes = SafeArrayGetUBound( safeArray, 1, &lUBound );
  1394. ONFAILTHROWERROR( hRes );
  1395. dwOnce = 0;
  1396. for( lIndex = lLBound; lIndex <= lUBound; lIndex++ )
  1397. {
  1398. hRes = SafeArrayGetElement( safeArray, &lIndex,
  1399. &V_UI1( &varTemp ) );
  1400. ONFAILTHROWERROR( hRes );
  1401. strAType = V_BSTR( &varTemp );
  1402. LPTSTR lpSubStr = _tcsstr( strAType, strSetId );
  1403. if( NULL != lpSubStr )
  1404. {
  1405. dwOnce = 1;
  1406. DynArrayAppendString( arrTransName, strAType, 0 );
  1407. break;
  1408. }
  1409. }
  1410. if( 0 == dwOnce )
  1411. {
  1412. hRes = SafeArrayGetLBound( safeArray, 1, &lLBound );
  1413. for( lIndex = lLBound; lIndex <= lUBound; lIndex++ )
  1414. {
  1415. hRes = SafeArrayGetElement( safeArray,
  1416. &lIndex, &V_UI1( &varTemp ) );
  1417. ONFAILTHROWERROR( hRes );
  1418. strAType = V_BSTR( &varTemp );
  1419. DynArrayAppendString( arrTransName, strAType, 0 );
  1420. }
  1421. }
  1422. }
  1423. }
  1424. }
  1425. }
  1426. }//for
  1427. }//try
  1428. catch( _com_error& e )
  1429. {
  1430. VariantClear( &varTemp );
  1431. WMISaveError( e.Error() );
  1432. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  1433. FreeMemory( (LPVOID*)&lpKeyPath );
  1434. SAFERELEASE( pSetting );
  1435. SAFERELEASE( pClass );
  1436. SAFERELEASE( pOutInst );
  1437. SAFERELEASE( pInClass );
  1438. SAFERELEASE( pInInst );
  1439. SAFERELEASE( pAdapterSetting );
  1440. return( FALSE );
  1441. }
  1442. catch( CHeap_Exception)
  1443. {
  1444. SetLastError( ERROR_NOT_ENOUGH_MEMORY );
  1445. SaveLastError();
  1446. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  1447. VariantClear( &varTemp );
  1448. FreeMemory( (LPVOID*)&lpKeyPath );
  1449. SAFERELEASE( pSetting );
  1450. SAFERELEASE( pClass );
  1451. SAFERELEASE( pOutInst );
  1452. SAFERELEASE( pInClass );
  1453. SAFERELEASE( pInInst );
  1454. SAFERELEASE( pAdapterSetting );
  1455. return( FALSE );
  1456. }
  1457. if( DynArrayGetCount( arrTransName ) <= 0 )
  1458. {
  1459. DynArrayAppendString( arrTransName, NOT_AVAILABLE, 0 );
  1460. }
  1461. FreeMemory( (LPVOID*)&lpKeyPath );
  1462. SAFERELEASE( pSetting );
  1463. SAFERELEASE( pClass );
  1464. SAFERELEASE( pOutInst );
  1465. SAFERELEASE( pInClass );
  1466. SAFERELEASE( pInInst );
  1467. SAFERELEASE( pAdapterSetting );
  1468. return( TRUE );
  1469. }
  1470. BOOL
  1471. FormatHWAddr(
  1472. IN LPTSTR lpRawAddr,
  1473. OUT LPTSTR lpFormattedAddr,
  1474. IN LPCTSTR lpFormatter
  1475. )
  1476. /*++
  1477. Routine Description:
  1478. Format a 12 Byte Ethernet Address and return it as 6 2-byte
  1479. sets separated by hyphen.
  1480. Arguments:
  1481. [IN] lpRawAddr - a pointer to a buffer containing the
  1482. unformatted hardware address.
  1483. [OUT] LpFormattedAddr - a pointer to a buffer in which to place
  1484. the formatted output.
  1485. [IN] lpFormatter - The Formatter string.
  1486. Return Value:
  1487. TRUE if mac address is successfully formatted else
  1488. FALSE.
  1489. --*/
  1490. {
  1491. //local variables
  1492. DWORD dwLength =0;
  1493. DWORD i=0;
  1494. DWORD j=0;
  1495. TCHAR szTemp[MAX_STRING] = _T( "\0" );
  1496. if( ( NULL == lpRawAddr ) || ( NULL == lpFormattedAddr ) ||
  1497. ( NULL == lpFormatter ) )
  1498. {
  1499. SetLastError( ERROR_INVALID_PARAMETER );
  1500. SaveLastError();
  1501. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  1502. return( FALSE );
  1503. }
  1504. //initialize memory
  1505. SecureZeroMemory( szTemp, MAX_STRING * sizeof( TCHAR ) );
  1506. //get the length of the string to be formatted
  1507. dwLength = StringLength( lpRawAddr, 0 );
  1508. //loop through the address string and insert formatter string
  1509. //after every two characters
  1510. while( i <= dwLength )
  1511. {
  1512. szTemp[j++] = lpRawAddr[i++];
  1513. szTemp[j++] = lpRawAddr[i++];
  1514. if( i >= dwLength )
  1515. {
  1516. break;
  1517. }
  1518. szTemp[j++] = lpFormatter[0];
  1519. }
  1520. //insert null character at the end
  1521. szTemp[j] = L'\0';
  1522. //copy the formatted string from temporary variable into the
  1523. //output string
  1524. StringCopy( lpFormattedAddr, szTemp, MAX_STRING_LENGTH );
  1525. return( TRUE );
  1526. }
  1527. BOOL
  1528. CallWin32Api(
  1529. OUT LPBYTE *lpBufptr,
  1530. IN LPCTSTR lpMachineName,
  1531. OUT DWORD *pdwNumofEntriesRead
  1532. )
  1533. /*++
  1534. Routine Description:
  1535. This function gets data into a predefined structure from win32 api.
  1536. Arguments:
  1537. [OUT] lpBufptr - To hold MAc and Transport names of all
  1538. network adapters.
  1539. [IN] lpMachineName - remote Machine name.
  1540. [OUT] pdwNumofEntriesRead - Contains number of network adpaters api
  1541. has enumerated.
  1542. Return Value:
  1543. TRUE if Win32Api function is successful.
  1544. FALSE if win32api failed.
  1545. --*/
  1546. {
  1547. //local variables
  1548. NET_API_STATUS err = NERR_Success;
  1549. DWORD dwNumofTotalEntries = 0;
  1550. DWORD dwResumehandle = 0;
  1551. LPWSTR lpwMName = NULL;
  1552. //validate input parameters
  1553. if( NULL == lpMachineName )
  1554. {
  1555. SetLastError( ERROR_INVALID_PARAMETER );
  1556. SaveLastError();
  1557. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  1558. return( FALSE );
  1559. }
  1560. //allocate memory
  1561. lpwMName = ( LPWSTR )AllocateMemory( MAX_STRING_LENGTH );
  1562. if( NULL == lpwMName )
  1563. {
  1564. SetLastError( ERROR_NOT_ENOUGH_MEMORY );
  1565. SaveLastError();
  1566. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  1567. return( FALSE );
  1568. }
  1569. if( 0 != StringLength( lpMachineName, 0 ) )
  1570. {
  1571. //get the machine name as wide character string
  1572. StringCopyW( lpwMName, lpMachineName, MAX_STRING_LENGTH );
  1573. //Call to an API function which enumerates the network adapters on the
  1574. //machine specified
  1575. err = NetWkstaTransportEnum ( lpwMName,
  1576. 0L,
  1577. lpBufptr,
  1578. (DWORD) -1,
  1579. pdwNumofEntriesRead,
  1580. &dwNumofTotalEntries,
  1581. &dwResumehandle );
  1582. }
  1583. else
  1584. {
  1585. //Call to an API function which enumerates the network adapters on the
  1586. //machine specified
  1587. err = NetWkstaTransportEnum ( NULL,
  1588. 0L,
  1589. lpBufptr,
  1590. (DWORD) -1,
  1591. pdwNumofEntriesRead,
  1592. &dwNumofTotalEntries,
  1593. &dwResumehandle );
  1594. }
  1595. FreeMemory( (LPVOID*)&lpwMName );
  1596. //if error has been returned by the API then display the error message
  1597. //just ignore the transport name and display the available data
  1598. if ( NERR_Success != err )
  1599. {
  1600. switch( GetLastError() )
  1601. {
  1602. case ERROR_IO_PENDING :
  1603. ShowMessage( stdout, NO_NETWORK_ADAPTERS );
  1604. return( FALSE );
  1605. case ERROR_NOT_SUPPORTED :
  1606. ShowMessage( stderr, ERROR_NOT_RESPONDING );
  1607. return( FALSE );
  1608. case ERROR_BAD_NETPATH :
  1609. ShowMessage( stderr, ERROR_NO_MACHINE );
  1610. return( FALSE );
  1611. case ERROR_INVALID_NAME :
  1612. ShowMessage( stderr, ERROR_INVALID_MACHINE );
  1613. return( FALSE );
  1614. case RPC_S_UNKNOWN_IF :
  1615. ShowMessage( stderr, ERROR_WKST_NOT_FOUND );
  1616. return( FALSE );
  1617. case ERROR_ACCESS_DENIED :
  1618. default :
  1619. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_SYSTEM );
  1620. return( FALSE );
  1621. }
  1622. }
  1623. return( TRUE );
  1624. }
  1625. BOOL
  1626. GetW2kMacData(
  1627. OUT TARRAY arrMacData,
  1628. IN LPCTSTR lpMachineName,
  1629. IN IEnumWbemClassObject *pAdapterConfig,
  1630. IN IEnumWbemClassObject *pAdapterSetting,
  1631. IN IWbemServices *pIWbemServiceDef,
  1632. IN IWbemServices *pIWbemServices,
  1633. IN COAUTHIDENTITY *pAuthIdentity,
  1634. IN TARRAY arrNetProtocol
  1635. )
  1636. /*++
  1637. Routine Description:
  1638. This function gets the media access control address of
  1639. the network adapters which are there on win2k or below.
  1640. Arguments:
  1641. [OUT] arrMacData - contains the MAC and other data of the network
  1642. adapter.
  1643. [IN] lpMachineName - holds machine name.
  1644. [IN] pAdapterConfig - interface to win32_networkadapter class.
  1645. [IN] pAdapterSetting - interface to win32_networkadapterconfiguration.
  1646. [IN] pIWbemServiceDef - interface to default name space.
  1647. [IN] pIWbemServices - interface to cimv2 name space.
  1648. [IN] pAuthIdentity - pointer to authentication structure.
  1649. [IN] arrNetProtocol - interface to win32_networkprotocol class
  1650. Return Value:
  1651. TRUE if getmacdata is successful.
  1652. FALSE if getmacdata failed.
  1653. --*/
  1654. {
  1655. //local variables
  1656. HRESULT hRes = S_OK;
  1657. IWbemClassObject *pAdapConfig = NULL;
  1658. VARIANT varTemp;
  1659. DWORD dwReturned = 1;
  1660. DWORD i = 0;
  1661. DWORD dwIndex = 0;
  1662. BOOL bFlag = FALSE;
  1663. DWORD dwNumofEntriesRead = 0;
  1664. LPWKSTA_TRANSPORT_INFO_0 lpAdapterData=NULL;
  1665. LPBYTE lpBufptr = NULL;
  1666. TARRAY arrTransName = NULL;
  1667. LPTSTR lpRawAddr = NULL;
  1668. LPTSTR lpFormAddr = NULL;
  1669. //validate input parametrs
  1670. if( ( NULL == arrMacData ) || ( NULL == lpMachineName ) ||
  1671. ( NULL == pAdapterConfig ) || ( NULL == pIWbemServiceDef ) ||
  1672. ( NULL == pIWbemServices ) || ( NULL == arrNetProtocol ) ||
  1673. ( NULL == pAdapterSetting ) )
  1674. {
  1675. SetLastError( ERROR_INVALID_PARAMETER );
  1676. SaveLastError();
  1677. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  1678. return FALSE;
  1679. }
  1680. try
  1681. {
  1682. CHString strAType = L"\0";
  1683. //callwin32api to get the mac address
  1684. bFlag = CallWin32Api( &lpBufptr, lpMachineName, &dwNumofEntriesRead );
  1685. if( FALSE == bFlag )
  1686. {
  1687. return( FALSE );
  1688. }
  1689. else
  1690. {
  1691. lpAdapterData = ( LPWKSTA_TRANSPORT_INFO_0 ) lpBufptr;
  1692. lpRawAddr = ( LPTSTR )AllocateMemory( MAX_STRING );
  1693. lpFormAddr = ( LPTSTR )AllocateMemory( MAX_STRING );
  1694. if( NULL == lpRawAddr )
  1695. {
  1696. FreeMemory( (LPVOID*)&lpRawAddr );
  1697. if( NULL != lpBufptr )
  1698. {
  1699. NetApiBufferFree( lpBufptr );
  1700. }
  1701. SetLastError( ERROR_NOT_ENOUGH_MEMORY );
  1702. SaveLastError();
  1703. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  1704. return( FALSE );
  1705. }
  1706. if ( NULL == lpFormAddr )
  1707. {
  1708. FreeMemory( (LPVOID*)&lpRawAddr );
  1709. FreeMemory( (LPVOID*)&lpFormAddr );
  1710. if( NULL != lpBufptr )
  1711. {
  1712. NetApiBufferFree( lpBufptr );
  1713. }
  1714. SetLastError( ERROR_NOT_ENOUGH_MEMORY );
  1715. SaveLastError();
  1716. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  1717. return( FALSE );
  1718. }
  1719. for ( i = 0; i < dwNumofEntriesRead; i++ )
  1720. {
  1721. //get the mac address
  1722. StringCopyW( lpRawAddr,
  1723. lpAdapterData[i].wkti0_transport_address, MAX_STRING );
  1724. if( 0 == StringCompare( lpRawAddr, DEFAULT_ADDRESS, TRUE, 0 ) )
  1725. {
  1726. continue;
  1727. }
  1728. bFlag = FormatHWAddr ( lpRawAddr, lpFormAddr, COLON_STRING );
  1729. if( FALSE == bFlag )
  1730. {
  1731. FreeMemory( (LPVOID*)&lpRawAddr );
  1732. FreeMemory( (LPVOID*)&lpFormAddr );
  1733. if( NULL != lpBufptr )
  1734. {
  1735. NetApiBufferFree( lpBufptr );
  1736. }
  1737. return( FALSE );
  1738. }
  1739. //get the network adapter type and other details
  1740. VariantInit( &varTemp );
  1741. bFlag = FALSE;
  1742. hRes = pAdapterConfig->Reset();
  1743. ONFAILTHROWERROR( hRes );
  1744. while ( ( 1 == dwReturned ) && ( FALSE == bFlag ) )
  1745. {
  1746. // Enumerate through the resultset.
  1747. hRes = pAdapterConfig->Next( WBEM_INFINITE,
  1748. 1,
  1749. &pAdapConfig,
  1750. &dwReturned );
  1751. ONFAILTHROWERROR( hRes );
  1752. if( 0 == dwReturned )
  1753. {
  1754. break;
  1755. }
  1756. hRes = pAdapConfig->Get( ADAPTER_MACADDR, 0 , &varTemp,
  1757. 0, NULL );
  1758. ONFAILTHROWERROR( hRes );
  1759. if( VT_NULL != varTemp.vt && VT_EMPTY != varTemp.vt )
  1760. {
  1761. strAType = varTemp.bstrVal;
  1762. VariantClear( &varTemp );
  1763. if( 0 == StringCompare( lpFormAddr, strAType, TRUE, 0 ) )
  1764. {
  1765. bFlag = TRUE;
  1766. break;
  1767. }
  1768. else
  1769. {
  1770. continue;
  1771. }
  1772. }
  1773. else
  1774. {
  1775. continue;
  1776. }
  1777. } //while
  1778. if ( TRUE == bFlag )
  1779. {
  1780. FormatHWAddr ( lpRawAddr, lpFormAddr, HYPHEN_STRING );
  1781. LONG dwCount = DynArrayFindStringEx( arrMacData, 3,
  1782. lpFormAddr, TRUE, 0 );
  1783. if( 0 == dwCount )
  1784. {
  1785. hRes = pAdapterConfig->Reset();
  1786. ONFAILTHROWERROR( hRes );
  1787. continue;
  1788. }
  1789. DynArrayAppendRow( arrMacData, 0 );
  1790. //get host name
  1791. hRes = pAdapConfig->Get( HOST_NAME, 0 , &varTemp, 0, NULL );
  1792. ONFAILTHROWERROR( hRes );
  1793. if( VT_NULL != varTemp.vt && VT_EMPTY != varTemp.vt )
  1794. {
  1795. strAType = varTemp.bstrVal;
  1796. }
  1797. else
  1798. {
  1799. strAType = NOT_AVAILABLE;
  1800. }
  1801. VariantClear( &varTemp );
  1802. DynArrayAppendString2( arrMacData, dwIndex, strAType, 0 );
  1803. FormatHWAddr ( lpRawAddr, lpFormAddr, COLON_STRING );
  1804. //get connection name
  1805. bFlag = GetConnectionName( arrMacData, dwIndex, lpFormAddr,
  1806. pAdapterSetting, pIWbemServiceDef );
  1807. if( FALSE == bFlag )
  1808. {
  1809. FreeMemory( (LPVOID*)&lpRawAddr );
  1810. FreeMemory( (LPVOID*)&lpFormAddr );
  1811. SAFERELEASE( pAdapConfig );
  1812. if( NULL != lpBufptr )
  1813. {
  1814. NetApiBufferFree( lpBufptr );
  1815. }
  1816. return( FALSE );
  1817. }
  1818. //get adapter type
  1819. hRes = pAdapConfig->Get( NAME, 0 , &varTemp, 0, NULL );
  1820. ONFAILTHROWERROR( hRes );
  1821. if( VT_NULL != varTemp.vt && VT_EMPTY != varTemp.vt )
  1822. {
  1823. strAType = varTemp.bstrVal;
  1824. }
  1825. else
  1826. {
  1827. strAType = NOT_AVAILABLE;
  1828. }
  1829. VariantClear( &varTemp );
  1830. DynArrayAppendString2( arrMacData, dwIndex, strAType, 0 );
  1831. //get MAC address
  1832. hRes = pAdapConfig->Get( ADAPTER_MACADDR, 0 , &varTemp,
  1833. 0, NULL );
  1834. ONFAILTHROWERROR( hRes );
  1835. strAType = varTemp.bstrVal;
  1836. for( int j = 2; j < strAType.GetLength();j += 3 )
  1837. {
  1838. strAType.SetAt( j, HYPHEN_CHAR );
  1839. }
  1840. VariantClear( &varTemp );
  1841. DynArrayAppendString2( arrMacData, dwIndex, strAType, 0 );
  1842. //get transport name
  1843. arrTransName = CreateDynamicArray();
  1844. if( NULL == arrTransName )
  1845. {
  1846. SetLastError( ERROR_NOT_ENOUGH_MEMORY );
  1847. SaveLastError();
  1848. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  1849. FreeMemory( (LPVOID*)&lpRawAddr );
  1850. FreeMemory( (LPVOID*)&lpFormAddr );
  1851. SAFERELEASE( pAdapConfig );
  1852. if( NULL != lpBufptr )
  1853. {
  1854. NetApiBufferFree( lpBufptr );
  1855. }
  1856. return( FALSE );
  1857. }
  1858. //get Device id
  1859. hRes = pAdapConfig->Get( DEVICE_ID, 0 , &varTemp, 0, NULL );
  1860. ONFAILTHROWERROR( hRes );
  1861. if( VT_NULL != varTemp.vt && VT_EMPTY != varTemp.vt )
  1862. {
  1863. strAType = ( LPCWSTR ) _bstr_t( varTemp );
  1864. }
  1865. bFlag = GetTransName( pIWbemServiceDef, pIWbemServices,
  1866. arrNetProtocol, arrTransName, pAuthIdentity,
  1867. strAType );
  1868. if( FALSE == bFlag )
  1869. {
  1870. FreeMemory( (LPVOID*)&lpRawAddr );
  1871. FreeMemory( (LPVOID*)&lpFormAddr );
  1872. SAFERELEASE( pAdapConfig );
  1873. if( NULL != lpBufptr )
  1874. {
  1875. NetApiBufferFree( lpBufptr );
  1876. }
  1877. DestroyDynamicArray( &arrTransName );
  1878. return( FALSE );
  1879. }
  1880. //insert transport name array into results array
  1881. DynArrayAppendEx2( arrMacData, dwIndex, arrTransName );
  1882. dwIndex++;
  1883. } //wmi data found
  1884. }//for each entry in api
  1885. }//if call api succeeded
  1886. }
  1887. catch( _com_error& e )
  1888. {
  1889. FreeMemory( (LPVOID*)&lpRawAddr );
  1890. FreeMemory( (LPVOID*)&lpFormAddr );
  1891. WMISaveError( e.Error() );
  1892. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  1893. SAFERELEASE( pAdapConfig );
  1894. if( NULL != lpBufptr )
  1895. {
  1896. NetApiBufferFree( lpBufptr );
  1897. }
  1898. return( FALSE );
  1899. }
  1900. catch( CHeap_Exception)
  1901. {
  1902. SetLastError( ERROR_NOT_ENOUGH_MEMORY );
  1903. SaveLastError();
  1904. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  1905. FreeMemory( (LPVOID*)&lpRawAddr );
  1906. FreeMemory( (LPVOID*)&lpFormAddr );
  1907. SAFERELEASE( pAdapConfig );
  1908. if( NULL != lpBufptr )
  1909. {
  1910. NetApiBufferFree( lpBufptr );
  1911. }
  1912. return( FALSE );
  1913. }
  1914. if( NULL != lpBufptr )
  1915. {
  1916. NetApiBufferFree( lpBufptr );
  1917. }
  1918. //if arrmacdata and not arrtransname then set transname to N/A
  1919. if( DynArrayGetCount( arrMacData ) > 0 &&
  1920. DynArrayGetCount( arrTransName ) <= 0 )
  1921. {
  1922. DynArrayAppendString( arrTransName, NOT_AVAILABLE, 0 );
  1923. DynArrayAppendEx2( arrMacData, dwIndex, arrTransName );
  1924. }
  1925. FreeMemory( (LPVOID*)&lpRawAddr );
  1926. FreeMemory( (LPVOID*)&lpFormAddr );
  1927. SAFERELEASE( pAdapConfig );
  1928. return( TRUE );
  1929. }
  1930. BOOL
  1931. GetConnectionName(
  1932. OUT TARRAY arrMacData,
  1933. IN DWORD dwIndex,
  1934. IN LPCTSTR lpFormAddr,
  1935. IN IEnumWbemClassObject *pAdapterSetting,
  1936. IN IWbemServices *pIWbemServiceDef
  1937. )
  1938. /*++
  1939. Routine Description:
  1940. This function gets connection name of the network adapter.
  1941. Arguments:
  1942. [OUT] arrMacData - contains the MAC and other data of the network
  1943. adapter.
  1944. [IN] dwIndex - index for array.
  1945. [IN] lpFormAddr - Mac address for network adapter.
  1946. [IN] pAdapterSetting - interface to win32_networkadapterconfiguration.
  1947. [IN] pIWbemServiceDef - interface to default name space.
  1948. Return Value:
  1949. TRUE if GetConnectionName is successful.
  1950. FALSE if GetConnectionName failed.
  1951. --*/
  1952. {
  1953. DWORD dwReturned = 1;
  1954. HRESULT hRes = 0;
  1955. IWbemClassObject *pAdapSetting = NULL;
  1956. VARIANT varTemp;
  1957. BOOL bFlag = FALSE;
  1958. IWbemClassObject *pClass = NULL;
  1959. IWbemClassObject *pOutInst = NULL;
  1960. IWbemClassObject *pInClass = NULL;
  1961. IWbemClassObject *pInInst = NULL;
  1962. LPTSTR lpKeyPath = NULL;
  1963. //validate input parameters
  1964. if( ( NULL == arrMacData ) || ( NULL == lpFormAddr ) ||
  1965. ( NULL == pAdapterSetting ) || ( NULL == pIWbemServiceDef ) )
  1966. {
  1967. SetLastError( ERROR_INVALID_PARAMETER );
  1968. SaveLastError();
  1969. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  1970. return FALSE;
  1971. }
  1972. VariantInit( &varTemp );
  1973. try
  1974. {
  1975. CHString strAType = L"\0";
  1976. hRes = pAdapterSetting->Reset();
  1977. ONFAILTHROWERROR( hRes );
  1978. while ( ( 1 == dwReturned ) && ( FALSE == bFlag ) )
  1979. {
  1980. // Enumerate through the resultset.
  1981. hRes = pAdapterSetting->Next( WBEM_INFINITE,
  1982. 1,
  1983. &pAdapSetting,
  1984. &dwReturned );
  1985. ONFAILTHROWERROR( hRes );
  1986. if( 0 == dwReturned )
  1987. {
  1988. break;
  1989. }
  1990. hRes = pAdapSetting->Get( ADAPTER_MACADDR, 0 , &varTemp, 0, NULL );
  1991. ONFAILTHROWERROR( hRes );
  1992. if( VT_NULL != varTemp.vt && VT_EMPTY != varTemp.vt )
  1993. {
  1994. strAType = varTemp.bstrVal;
  1995. VariantClear( &varTemp );
  1996. if( 0 == StringCompare( lpFormAddr, strAType, TRUE, 0 ) )
  1997. {
  1998. bFlag = TRUE;
  1999. break;
  2000. }
  2001. }
  2002. }//while
  2003. if( TRUE == bFlag )
  2004. {
  2005. hRes = pAdapSetting->Get( SETTING_ID, 0 , &varTemp, 0, NULL );
  2006. ONFAILTHROWERROR( hRes );
  2007. if( VT_NULL != varTemp.vt && VT_EMPTY != varTemp.vt )
  2008. {
  2009. strAType = varTemp.bstrVal;
  2010. VariantClear( &varTemp );
  2011. lpKeyPath = ( LPTSTR )AllocateMemory( 4 * MAX_RES_STRING );
  2012. if( NULL == lpKeyPath )
  2013. {
  2014. SetLastError( ERROR_NOT_ENOUGH_MEMORY );
  2015. SaveLastError();
  2016. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  2017. SAFERELEASE( pAdapSetting );
  2018. return( FALSE );
  2019. }
  2020. hRes = pIWbemServiceDef->GetObject( _bstr_t( WMI_REGISTRY ), 0, NULL,
  2021. &pClass, NULL );
  2022. ONFAILTHROWERROR( hRes );
  2023. hRes = pClass->GetMethod( WMI_REGISTRY_M_STRINGVALUE, 0,
  2024. &pInClass, NULL );
  2025. ONFAILTHROWERROR( hRes );
  2026. hRes = pInClass->SpawnInstance(0, &pInInst);
  2027. ONFAILTHROWERROR( hRes );
  2028. varTemp.vt = VT_I4;
  2029. varTemp.lVal = WMI_HKEY_LOCAL_MACHINE;
  2030. hRes = pInInst->Put( WMI_REGISTRY_IN_HDEFKEY, 0, &varTemp, 0 );
  2031. VariantClear( &varTemp );
  2032. ONFAILTHROWERROR( hRes );
  2033. StringCopy( lpKeyPath, CONNECTION_KEYPATH,
  2034. ( GetBufferSize( lpKeyPath )/ sizeof( WCHAR ) ) );
  2035. StringConcat( lpKeyPath, strAType,
  2036. ( GetBufferSize( lpKeyPath )/ sizeof( WCHAR ) ) );
  2037. StringConcat( lpKeyPath, CONNECTION_STRING,
  2038. ( GetBufferSize( lpKeyPath )/ sizeof( WCHAR ) ) );
  2039. varTemp.vt = VT_BSTR;
  2040. varTemp.bstrVal = SysAllocString( lpKeyPath );
  2041. hRes = pInInst->Put( WMI_REGISTRY_IN_SUBKEY, 0, &varTemp, 0 );
  2042. VariantClear( &varTemp );
  2043. ONFAILTHROWERROR( hRes );
  2044. varTemp.vt = VT_BSTR;
  2045. varTemp.bstrVal = SysAllocString( REG_NAME );
  2046. hRes = pInInst->Put( WMI_REGISTRY_IN_VALUENAME, 0,
  2047. &varTemp, 0 );
  2048. VariantClear( &varTemp );
  2049. ONFAILTHROWERROR( hRes );
  2050. // Call the method.
  2051. hRes = pIWbemServiceDef->ExecMethod( _bstr_t( WMI_REGISTRY ),
  2052. _bstr_t( WMI_REGISTRY_M_STRINGVALUE ), 0, NULL, pInInst,
  2053. &pOutInst, NULL );
  2054. ONFAILTHROWERROR( hRes );
  2055. varTemp.vt = VT_I4;
  2056. hRes = pOutInst->Get( WMI_REGISTRY_OUT_RETURNVALUE, 0,
  2057. &varTemp, 0, 0 );
  2058. ONFAILTHROWERROR( hRes );
  2059. if( 0 == varTemp.lVal )
  2060. {
  2061. VariantClear( &varTemp );
  2062. varTemp.vt = VT_BSTR;
  2063. hRes = pOutInst->Get( WMI_REGISTRY_OUT_VALUE, 0,
  2064. &varTemp, 0, 0);
  2065. ONFAILTHROWERROR( hRes );
  2066. if( VT_NULL != varTemp.vt && VT_EMPTY != varTemp.vt )
  2067. {
  2068. strAType = varTemp.bstrVal;
  2069. DynArrayAppendString2( arrMacData, dwIndex, strAType, 0 );
  2070. }
  2071. }
  2072. else
  2073. {
  2074. DynArrayAppendString2( arrMacData, dwIndex, NOT_AVAILABLE, 0 );
  2075. }
  2076. }//setting id not null
  2077. else
  2078. {
  2079. DynArrayAppendString2( arrMacData, dwIndex, NOT_AVAILABLE, 0 );
  2080. }
  2081. }//got match
  2082. else
  2083. {
  2084. DynArrayAppendString2( arrMacData, dwIndex, NOT_AVAILABLE, 0 );
  2085. }
  2086. }//try
  2087. catch( _com_error& e )
  2088. {
  2089. VariantClear( &varTemp );
  2090. WMISaveError( e.Error() );
  2091. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  2092. FreeMemory( (LPVOID*)&lpKeyPath );
  2093. SAFERELEASE( pAdapSetting );
  2094. SAFERELEASE( pClass );
  2095. SAFERELEASE( pOutInst );
  2096. SAFERELEASE( pInClass );
  2097. SAFERELEASE( pInInst );
  2098. return( FALSE );
  2099. }
  2100. catch( CHeap_Exception)
  2101. {
  2102. SetLastError( ERROR_NOT_ENOUGH_MEMORY );
  2103. SaveLastError();
  2104. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  2105. VariantClear( &varTemp );
  2106. FreeMemory( (LPVOID*)&lpKeyPath );
  2107. SAFERELEASE( pAdapSetting );
  2108. SAFERELEASE( pClass );
  2109. SAFERELEASE( pOutInst );
  2110. SAFERELEASE( pInClass );
  2111. SAFERELEASE( pInInst );
  2112. return( FALSE );
  2113. }
  2114. VariantClear( &varTemp );
  2115. FreeMemory( (LPVOID*)&lpKeyPath );
  2116. SAFERELEASE( pAdapSetting );
  2117. SAFERELEASE( pClass );
  2118. SAFERELEASE( pOutInst );
  2119. SAFERELEASE( pInClass );
  2120. SAFERELEASE( pInInst );
  2121. return( TRUE );
  2122. }
  2123. BOOL
  2124. GetNwkProtocol(
  2125. OUT TARRAY arrNetProtocol,
  2126. IN IEnumWbemClassObject *pNetProtocol
  2127. )
  2128. /*++
  2129. Routine Description:
  2130. This function enumerates all the network protocols.
  2131. Arguments:
  2132. [OUT] arrNetProtocol - contains all the network protocols.
  2133. [IN] pNetProtocol - interface to win32_networkprotocol.
  2134. Return Value:
  2135. TRUE if GetNwkProtocol is successful.
  2136. FALSE if GetNwkProtocol failed.
  2137. --*/
  2138. {
  2139. HRESULT hRes = 0;
  2140. DWORD dwReturned = 1;
  2141. IWbemClassObject *pProto = NULL;
  2142. VARIANT varTemp;
  2143. VariantInit( &varTemp );
  2144. try
  2145. {
  2146. CHString strAType = L"\0";
  2147. //get transport protocols
  2148. while ( 1 == dwReturned )
  2149. {
  2150. // Enumerate through the resultset.
  2151. hRes = pNetProtocol->Next( WBEM_INFINITE,
  2152. 1,
  2153. &pProto,
  2154. &dwReturned );
  2155. ONFAILTHROWERROR( hRes );
  2156. if( 0 == dwReturned )
  2157. {
  2158. break;
  2159. }
  2160. hRes = pProto->Get( CAPTION, 0 , &varTemp, 0, NULL );
  2161. ONFAILTHROWERROR( hRes );
  2162. if( VT_NULL != varTemp.vt && VT_EMPTY != varTemp.vt )
  2163. {
  2164. strAType = varTemp.bstrVal;
  2165. VariantClear( &varTemp );
  2166. if( 0 == DynArrayGetCount( arrNetProtocol ) )
  2167. {
  2168. DynArrayAppendString( arrNetProtocol, strAType, 0 );
  2169. }
  2170. else
  2171. {
  2172. LONG lFound = DynArrayFindString( arrNetProtocol,
  2173. strAType, TRUE, 0 );
  2174. if( -1 == lFound )
  2175. {
  2176. DynArrayAppendString( arrNetProtocol, strAType, 0 );
  2177. }
  2178. }
  2179. }
  2180. }//while
  2181. }
  2182. catch( _com_error& e )
  2183. {
  2184. VariantClear( &varTemp );
  2185. WMISaveError( e.Error() );
  2186. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  2187. SAFERELEASE( pProto );
  2188. return( FALSE );
  2189. }
  2190. catch( CHeap_Exception)
  2191. {
  2192. SetLastError( ERROR_NOT_ENOUGH_MEMORY );
  2193. SaveLastError();
  2194. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  2195. VariantClear( &varTemp );
  2196. SAFERELEASE( pProto );
  2197. return( FALSE );
  2198. }
  2199. VariantClear( &varTemp );
  2200. SAFERELEASE( pProto );
  2201. return( TRUE );
  2202. }
  2203. BOOL
  2204. CheckVersion(
  2205. IN BOOL bLocalSystem,
  2206. IN COAUTHIDENTITY *pAuthIdentity,
  2207. IN IWbemServices *pIWbemServices
  2208. )
  2209. /*++
  2210. Routine Description:
  2211. This function checks whether tha target system is win2k or above.
  2212. Arguments:
  2213. [IN] bLocalSystem - Hold whether local system or not.
  2214. [IN] pAuthIdentity - pointer to authentication structure.
  2215. [IN] pIWbemServices - pointer to IWbemServices.
  2216. Return Value:
  2217. TRUE if target system is win2k.
  2218. FALSE if target system is not win2k.
  2219. --*/
  2220. {
  2221. if ( FALSE == bLocalSystem )
  2222. {
  2223. // check the version compatibility
  2224. DWORD dwVersion = 0;
  2225. dwVersion = GetTargetVersionEx( pIWbemServices, pAuthIdentity );
  2226. if ( dwVersion <= 5000 )
  2227. {
  2228. return( TRUE );
  2229. }
  2230. }
  2231. return( FALSE );
  2232. }