Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2235 lines
65 KiB

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