home *** CD-ROM | disk | FTP | other *** search
/ PCNET 2006 September - Disc 1 / PCNET_CD_2006_09.iso / surpriz / MSRMesh-VirtualWIFI.MSI / virtual.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-06-24  |  13.7 KB  |  494 lines

  1. /*
  2.  * Author   : Ranveer Chandra
  3.  * Directory: VirtualWiFi_Root\notifyob
  4.  * File Name: virtual.cpp
  5.  * Purpose  : Virtual miniport class definition.
  6.  */
  7.  
  8.  
  9. #include "virtual.h"
  10. #include "common.h"
  11.  
  12. //+---------------------------------------------------------------------------
  13. //
  14. // Function:  CMuxVirtualMiniport::CMuxVirtualMiniport
  15. //
  16. // Purpose:   Constructor for class CMuxVirtualMiniport
  17. //
  18. // Arguments: None
  19. //
  20. // Returns:
  21. //
  22. // Notes:
  23. //
  24.  
  25. CMuxVirtualMiniport::CMuxVirtualMiniport(INetCfg *pnc,
  26.                                          GUID    *pguidMiniport,
  27.                                          GUID    *pguidAdapter)
  28. {
  29.     TraceMsg( L"-->CMuxVirtualMiniport::CMuxVirtualMiniport(Constructor).\n" );
  30.  
  31.     m_pnc = pnc;
  32.     m_pnc->AddRef();
  33.  
  34.     CopyMemory( &m_guidAdapter,
  35.                 pguidAdapter,
  36.                 sizeof(GUID) );
  37.  
  38.     if ( pguidMiniport ) {
  39.  
  40.         CopyMemory( &m_guidMiniport,
  41.                     pguidMiniport,
  42.                     sizeof(GUID) );
  43.  
  44.     }
  45.     else {
  46.  
  47.         ZeroMemory( &m_guidMiniport,
  48.                     sizeof(GUID) );
  49.     }
  50.  
  51.     TraceMsg( L"<--CMuxVirtualMiniport::CMuxVirtualMiniport(Constructor).\n" );
  52. }
  53.  
  54. //+---------------------------------------------------------------------------
  55. //
  56. // Function:  CMuxVirtualMiniport::~CMuxVirtualMiniport
  57. //
  58. // Purpose:   Destructor for class CMuxVirtualMiniport
  59. //
  60. // Arguments: None
  61. //
  62. // Returns:
  63. //
  64. // Notes:
  65. //
  66.  
  67. CMuxVirtualMiniport::~CMuxVirtualMiniport(VOID)
  68. {
  69.     TraceMsg( L"-->CMuxVirtualMiniport::~CMuxVirtualMiniport(Destructor).\n" );
  70.  
  71.     ReleaseObj( m_pnc );
  72.  
  73.     TraceMsg( L"<--CMuxVirtualMiniport::~CMuxVirtualMiniport(Destructor).\n" );
  74.  
  75. }
  76.  
  77. //+---------------------------------------------------------------------------
  78. //
  79. // Function:  CMuxVirtualMiniport::LoadConfiguration
  80. //
  81. // Purpose:   Load miniport configuration from the registry.
  82. //
  83. // Arguments: None
  84. //
  85. // Returns: S_OK on success, otherwise an error code.
  86. //
  87. // Notes:
  88. //
  89.  
  90. HRESULT CMuxVirtualMiniport::LoadConfiguration(VOID)
  91. {
  92.     TraceMsg( L"-->CMuxVirtualMiniport::LoadConfiguration.\n" );
  93.  
  94.     TraceMsg( L"<--CMuxVirtualMiniport::LoadConfiguration(HRESULT = %x).\n",
  95.             S_OK );
  96.  
  97.     return S_OK;
  98. }
  99.  
  100. //+---------------------------------------------------------------------------
  101. //
  102. // Function:  CMuxVirtualMiniport::GetAdapterGUID
  103. //
  104. // Purpose:   Returns the adapter GUID.
  105. //
  106. // Arguments:
  107. //          OUT pguidAdapter: GUID of the adapter returned.
  108. //
  109. // Returns: None.
  110. //
  111. // Notes:
  112. //
  113.  
  114. VOID CMuxVirtualMiniport::GetAdapterGUID (GUID *pguidAdapter)
  115. {
  116.     TraceMsg( L"-->CMuxVirtualMiniport::GetAdapterGUID.\n" );
  117.  
  118.     CopyMemory( pguidAdapter,
  119.                 &m_guidAdapter,
  120.                 sizeof(GUID) );
  121.  
  122.     TraceMsg( L"<--CMuxVirtualMiniport::GetAdapterGUID.\n" );
  123. }
  124.  
  125. //+---------------------------------------------------------------------------
  126. //
  127. // Function:  CMuxVirtualMiniport::GetMiniportGUID
  128. //
  129. // Purpose:   Returns the miniport GUID.
  130. //
  131. // Arguments:
  132. //          OUT pguidMiniport: GUID of the miniport returned.
  133. //
  134. // Returns: None.
  135. //
  136. // Notes:
  137. //
  138.  
  139. VOID CMuxVirtualMiniport::GetMiniportGUID (GUID *pguidMiniport)
  140. {
  141.     TraceMsg( L"-->CMuxVirtualMiniport::GetMiniportGUID.\n" );
  142.  
  143.     CopyMemory( pguidMiniport,
  144.               &m_guidMiniport,
  145.               sizeof(GUID) );
  146.  
  147.     TraceMsg( L"<--CMuxVirtualMiniport::GetMiniportGUID.\n" );
  148. }
  149.  
  150. //+---------------------------------------------------------------------------
  151. //
  152. // Function:  CMuxVirtualMiniport::Install
  153. //
  154. // Purpose:   Installs a virtual miniport.
  155. //
  156. // Arguments: None
  157. //
  158. // Returns: S_OK on success, otherwise an error code.
  159. //
  160. // Notes:
  161. //
  162.  
  163. HRESULT CMuxVirtualMiniport::Install (VOID)
  164. {
  165.     INetCfgClass       *pncClass;
  166.     INetCfgClassSetup  *pncClassSetup;
  167.     INetCfgComponent   *pnccMiniport;
  168.     HRESULT            hr;
  169.  
  170.     TraceMsg( L"-->CMuxVirtualMiniport::Install.\n" );
  171.  
  172.     hr = m_pnc->QueryNetCfgClass( &GUID_DEVCLASS_NET,
  173.                                   IID_INetCfgClass,
  174.                                   (void **)&pncClass );
  175.     if ( hr == S_OK ) {
  176.  
  177.         hr = pncClass->QueryInterface( IID_INetCfgClassSetup,
  178.                                        (void **)&pncClassSetup );
  179.         if ( hr == S_OK ) {
  180.  
  181.             hr = pncClassSetup->Install( c_szMuxMiniport,
  182.                                          NULL,
  183.                                          0,
  184.                                          0,
  185.                                          NULL,
  186.                                          NULL,
  187.                                          &pnccMiniport );
  188.             if ( hr == S_OK ) {
  189.  
  190.                 hr = pnccMiniport->GetInstanceGuid( &m_guidMiniport );
  191.  
  192.                 if ( hr != S_OK ) {
  193.  
  194.                     TraceMsg( L"   Failed to get the instance guid, uninstalling "
  195.                               L" the miniport.\n" );
  196.  
  197.                     pncClassSetup->DeInstall( pnccMiniport,
  198.                                               NULL,
  199.                                               NULL );
  200.                 }
  201.  
  202.                 ReleaseObj( pnccMiniport );
  203.             }
  204.             else {
  205.  
  206.                 TraceMsg( L"   Failed to install the miniport.\n" );
  207.             }
  208.  
  209.             ReleaseObj( pncClassSetup );
  210.         }
  211.         else {
  212.  
  213.             TraceMsg( L"   QueryInterface failed.\n" );
  214.         }
  215.  
  216.         ReleaseObj( pncClass );
  217.     }
  218.     else {
  219.  
  220.      TraceMsg( L"   QueryNetCfgClass failed.\n" );
  221.     }
  222.  
  223.     TraceMsg( L"<--CMuxVirtualMiniport::Install(HRESULT = %x).\n",
  224.             hr );
  225.  
  226.     return hr;
  227. }
  228.  
  229. //+---------------------------------------------------------------------------
  230. //
  231. // Function:  CMuxVirtualMiniport::DeInstall
  232. //
  233. // Purpose:   Uninstalls the virtual miniport.
  234. //
  235. // Arguments: None
  236. //
  237. // Returns: S_OK on success, otherwise an error code.
  238. //
  239. // Notes:
  240. //
  241.  
  242. HRESULT CMuxVirtualMiniport::DeInstall (VOID)
  243. {
  244.     INetCfgClass       *pncClass;
  245.     INetCfgClassSetup  *pncClassSetup;
  246.     INetCfgComponent   *pnccMiniport;
  247.     HRESULT            hr;
  248.  
  249.     TraceMsg( L"-->CMuxVirtualMiniport::DeInstall.\n" );
  250.  
  251.     hr = m_pnc->QueryNetCfgClass( &GUID_DEVCLASS_NET,
  252.                                   IID_INetCfgClass,
  253.                                   (void **)&pncClass );
  254.     if ( hr == S_OK ) {
  255.  
  256.         hr = pncClass->QueryInterface( IID_INetCfgClassSetup,
  257.                                        (void **)&pncClassSetup );
  258.         if ( hr == S_OK ) {
  259.  
  260.             hr = HrFindInstance( m_pnc,
  261.                                  m_guidMiniport,
  262.                                  &pnccMiniport );
  263.  
  264.             if ( hr == S_OK ) {
  265.  
  266.                 TraceMsg( L"   Found the miniport instance to uninstall.\n" );
  267.  
  268.                 hr = pncClassSetup->DeInstall( pnccMiniport,
  269.                                                NULL,
  270.                                                NULL );
  271.                 ReleaseObj( pnccMiniport );
  272.             }
  273.             else {
  274.                 TraceMsg( L"   Didn't find the miniport instance to uninstall.\n" );
  275.             }
  276.  
  277.             ReleaseObj( pncClassSetup );
  278.         }
  279.         else {
  280.  
  281.             TraceMsg( L"   QueryInterface failed.\n" );
  282.         }
  283.  
  284.         ReleaseObj( pncClass );
  285.     }
  286.     else {
  287.  
  288.         TraceMsg( L"   QueryNetCfgClass failed.\n" );
  289.     }
  290.  
  291.     TraceMsg( L"<--CMuxVirtualMiniport::DeInstall(HRESULT = %x).\n",
  292.               hr );
  293.  
  294.     return hr;
  295. }
  296.  
  297. //+---------------------------------------------------------------------------
  298. //
  299. // Function:  CMuxVirtualMiniport::ApplyRegistryChanges
  300. //
  301. // Purpose:   Store the changes in the registry.
  302. //
  303. // Arguments:
  304. //            IN eApplyAction: Action performed.
  305. //
  306. // Returns: S_OK on success, otherwise an error code.
  307. //
  308. // Notes:
  309. //
  310.  
  311. HRESULT CMuxVirtualMiniport::ApplyRegistryChanges(ConfigAction eApplyAction)
  312. {
  313.     HKEY                    hkeyAdapterGuid;
  314.     WCHAR                   szAdapterGuid[MAX_PATH+1];
  315.     WCHAR                   szAdapterGuidKey[MAX_PATH+1];
  316.     WCHAR                   szMiniportGuid[MAX_PATH+1];
  317.     LPWSTR                  lpDevice;
  318.     LONG                    lResult = 0;
  319.  
  320.     TraceMsg( L"-->CMuxVirtualMiniport::ApplyRegistryChanges.\n" );
  321.  
  322.     switch( eApplyAction ) {
  323.  
  324.         case eActAdd:         // Virtual miniport added.
  325.  
  326.             StringFromGUID2( m_guidAdapter,
  327.                              szAdapterGuid,
  328.                              MAX_PATH+1 );
  329.  
  330.             swprintf( szAdapterGuidKey,
  331.                       L"%s\\%s",
  332.                       c_szAdapterList,
  333.                       szAdapterGuid );
  334.  
  335.             lResult = RegCreateKeyExW( HKEY_LOCAL_MACHINE,
  336.                                        szAdapterGuidKey,
  337.                                        0,
  338.                                        NULL,
  339.                                        REG_OPTION_NON_VOLATILE,
  340.                                        KEY_ALL_ACCESS,
  341.                                        NULL,
  342.                                        &hkeyAdapterGuid,
  343.                                        NULL);
  344.  
  345.  
  346.             if ( lResult == ERROR_SUCCESS ) {
  347.  
  348.                 StringFromGUID2( m_guidMiniport,
  349.                                  szMiniportGuid,
  350.                                  MAX_PATH+1 );
  351.  
  352.                 lpDevice = AddDevicePrefix( szMiniportGuid );
  353.  
  354.                 if ( lpDevice ) {
  355.  
  356. #ifndef PASSTHRU_NOTIFY
  357.  
  358.                     lResult = AddToMultiSzValue( hkeyAdapterGuid,
  359.                                                  lpDevice );
  360. #else
  361.  
  362.                     lResult = RegSetValueExW( hkeyAdapterGuid,
  363.                                               c_szUpperBindings,
  364.                                               0,
  365.                                               REG_SZ,
  366.                                               (LPBYTE)lpDevice,
  367.                                               (wcslen(lpDevice) + 1) *
  368.                                               sizeof(WCHAR) );
  369.  
  370.  
  371. #endif
  372.  
  373.                     if ( lResult != ERROR_SUCCESS ) {
  374.  
  375.                         TraceMsg( L"   Failed to save %s at %s\\%s.\n",
  376.                                   lpDevice,
  377.                                   szAdapterGuidKey,
  378.                                   c_szUpperBindings );
  379.  
  380.                     }
  381.  
  382.                     free( lpDevice );
  383.                 }
  384.                 else {
  385.                     lResult = ERROR_NOT_ENOUGH_MEMORY;
  386.                 }
  387.  
  388.                 RegCloseKey( hkeyAdapterGuid );
  389.             }
  390.             else {
  391.                 TraceMsg( L"   Failed to open the registry key: %s.\n",
  392.                           szAdapterGuidKey );
  393.             }
  394.             break;
  395.  
  396.         case eActRemove:                  // Virtual miniport removed.
  397.  
  398.             StringFromGUID2( m_guidAdapter,
  399.                              szAdapterGuid,
  400.                              MAX_PATH+1 );
  401.  
  402.             swprintf( szAdapterGuidKey,
  403.                       L"%s\\%s",
  404.                       c_szAdapterList,
  405.                       szAdapterGuid );
  406.  
  407.             lResult = RegCreateKeyExW( HKEY_LOCAL_MACHINE,
  408.                                         szAdapterGuidKey,
  409.                                         0,
  410.                                         NULL,
  411.                                         REG_OPTION_NON_VOLATILE,
  412.                                         KEY_ALL_ACCESS,
  413.                                         NULL,
  414.                                         &hkeyAdapterGuid,
  415.                                         NULL);
  416.  
  417.  
  418.             if ( lResult == ERROR_SUCCESS ) {
  419.  
  420.                 StringFromGUID2( m_guidMiniport,
  421.                                  szMiniportGuid,
  422.                                  MAX_PATH+1 );
  423.  
  424.                 lpDevice = AddDevicePrefix( szMiniportGuid );
  425.                 TraceMsg( L"   Deleting %s at %s.\n",
  426.                           lpDevice,
  427.                           szAdapterGuidKey );
  428.  
  429.                 if ( lpDevice ) {
  430.  
  431. #ifndef PASSTHRU_NOTIFY
  432.  
  433.                     lResult = DeleteFromMultiSzValue( hkeyAdapterGuid,
  434.                                                       lpDevice );
  435. #else
  436.  
  437.                     lResult = RegDeleteValueW( hkeyAdapterGuid,
  438.                                                c_szUpperBindings );
  439. #endif
  440.  
  441.                     if ( lResult != ERROR_SUCCESS ) {
  442.  
  443.                         TraceMsg( L"   Failed to delete %s at %s\\%s.\n",
  444.                                   lpDevice,
  445.                                   szAdapterGuidKey,
  446.                                   c_szUpperBindings );
  447.  
  448.                     }
  449.  
  450.                     free( lpDevice );
  451.                 }
  452.  
  453.                 RegCloseKey( hkeyAdapterGuid );
  454.             }
  455.             else {
  456.                 TraceMsg( L"   Failed to open the registry key: %s.\n",
  457.                           szAdapterGuidKey );
  458.             }
  459.     }
  460.  
  461.     TraceMsg( L"<--CMuxVirtualMiniport::ApplyRegistryChanges(HRESULT = %x).\n",
  462.               HRESULT_FROM_WIN32(lResult) );
  463.  
  464.     return HRESULT_FROM_WIN32(lResult);
  465. }
  466.  
  467. //+---------------------------------------------------------------------------
  468. //
  469. // Function:  CMuxVirtualMiniport::ApplyPnpChanges
  470. //
  471. // Purpose:   
  472. //
  473. // Arguments:
  474. //            IN eApplyAction: Action performed.
  475. //
  476. // Returns: S_OK on success, otherwise an error code.
  477. //
  478. // Notes:
  479. //
  480.  
  481. HRESULT CMuxVirtualMiniport::ApplyPnpChanges
  482.                                  (INetCfgPnpReconfigCallback *pfCallback,
  483.                                   ConfigAction eApplyAction)
  484. {
  485.     TraceMsg( L"-->CMuxVirtualMiniport::ApplyPnpChanges.\n" );
  486.  
  487.     TraceMsg( L"<--CMuxVirtualMiniport::ApplyPnpChanges(HRESULT = %x).\n",
  488.             S_OK );
  489.  
  490.     return S_OK;
  491. }
  492.  
  493.  
  494.