Advertisement
npocmaka

localareaConnection

Jan 21st, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.53 KB | None | 0 0
  1. //=====================================================================================================
  2. //Summary: Demonstrates how to programmatically disable the IPV6 in Vista using INetCfg Interface.
  3. //THIS CODE HAS NOT BEEN TESTED RIGOROUSLY.USING THIS CODE IN PRODUCTION ENVIRONMENT IS STRICTLY NOT //RECOMMENDED.THIS SAMPLE IS PURELY FOR DEMONSTRATION PURPOSES ONLY.THIS CODE AND INFORMATION IS PROVIDED "AS IS" //WITHOUT WARRANTY OF ANY KIND.
  4. //======================================================================================================
  5.  
  6. #include <stdio.h>
  7. #include <Netcfgx.h>
  8. #include <devguid.h>
  9. #include <TCHAR.H>
  10.  
  11.  
  12. #define APP_NAME            L"DisableIPV6"
  13. #define LOCK_TIME_OUT     5000
  14.  
  15. //Modify this flag to Enable/Disable IPV6.
  16. #define ENABLE FALSE
  17.  
  18. const GUID     *pguidNetClass = &GUID_DEVCLASS_NETTRANS;
  19.  
  20. int main()
  21. {
  22.       INetCfg      *pnc = NULL;
  23.       HRESULT      hr = S_OK;
  24.  
  25.       //Initialize COM
  26.       hr = CoInitialize(NULL);
  27.  
  28.       //Create the INetCfg Interface Pointer
  29.       hr = CoCreateInstance( CLSID_CNetCfg,
  30.                                NULL, CLSCTX_INPROC_SERVER,
  31.                                IID_INetCfg,
  32.                                (void**)&pnc );
  33.  
  34.        //Get the lock before we Initialize the INetCfg object,
  35.        LPWSTR       lpszApp;
  36.        INetCfgLock  *pncLock = NULL;
  37.        hr = pnc->QueryInterface( IID_INetCfgLock,(LPVOID *)&pncLock );
  38.        if ( hr == S_OK )
  39.        {
  40.               hr = pncLock->AcquireWriteLock(
  41.                                       LOCK_TIME_OUT,APP_NAME,&lpszApp
  42.                                       );
  43.        }              
  44.  
  45.       //Initialize the INetCfg Object.
  46.       hr = pnc->Initialize( NULL );
  47.  
  48.       //Get the enumerator interface
  49.       IEnumNetCfgComponent *pencc = NULL;
  50.       INetCfgClass  *pncclass;
  51.       hr = pnc->QueryNetCfgClass(
  52.                                   pguidNetClass,
  53.                                   IID_INetCfgClass,
  54.                                   (PVOID *)&pncclass
  55.                                  );
  56.  
  57.       if ( hr == S_OK )
  58.       {
  59.         hr = pncclass->EnumComponents( &pencc );
  60.       }
  61.  
  62.       INetCfgComponent     *pncc = NULL;
  63.  
  64.       if(hr == S_OK)
  65.       {
  66.           ULONG    ulCount;
  67.           pencc->Reset();
  68.  
  69.             hr = pencc->Next( 1,&pncc,&ulCount );
  70.  
  71.             while(hr == S_OK)
  72.             {
  73.                   LPWSTR           lpszItemName;
  74.                  
  75.                   pncc->GetId (&lpszItemName);
  76.  
  77.                   //If this is Internet Protocol Version 6 then goahead to
  78.                   //enumerate the bindings.
  79.                   if( wcscmp(lpszItemName,TEXT("ms_tcpip6")) == 0)
  80.                   {
  81.                         wprintf(L"Component Name = %s\n",lpszItemName);
  82.  
  83.                         //Enumerate the Bindings and Disable them.
  84.                         IEnumNetCfgBindingPath      *pencbp = NULL;
  85.                         INetCfgComponentBindings *pnccb = NULL;
  86.                         INetCfgBindingPath          *pncbp = NULL;
  87.                        
  88.                         hr = pncc->QueryInterface(  
  89.                                                 IID_INetCfgComponentBindings,
  90.                                                 (PVOID *)&pnccb
  91.                                                   );
  92.  
  93.                          if ( hr == S_OK )
  94.                          {
  95.                           // Get binding path enumerator reference.
  96.                           hr = pnccb->EnumBindingPaths( EBP_BELOW,&pencbp );
  97.                           pnccb->Release ();
  98.  
  99.                           ULONG   ulCount;
  100.                           pencbp->Reset();
  101.  
  102.                           //Enumerate the Bindings.
  103.                           hr = pencbp->Next( 1,&pncbp,&ulCount );
  104.  
  105.                           //Loop until all the bindings are enumerated                  
  106.                           //and disabled.
  107.                           while( hr == S_OK )
  108.                           {
  109.                                //Disable this binding.
  110.                                hr = pncbp->Enable (ENABLE);
  111.  
  112.                                //Save the changes,
  113.                                pnc->Apply ();
  114.  
  115.                                //Release the interface and set it to
  116.                                //NULL.
  117.                                pncbp->Release ();
  118.                                pncbp = NULL;
  119.                                      
  120.                                //Enumerate Next Binding if any.
  121.                                hr = pencbp->Next( 1,&pncbp,&ulCount );
  122.                           }
  123.  
  124.                           //We have enumerated all the bindings and
  125.                           //disabled them.Release the interfaces.
  126.                                pencbp->Release();
  127.                          }
  128.                    }
  129.                
  130.                   pncc->Release ();
  131.                   pncc = NULL;
  132.  
  133.                   //Enumerate the next component.
  134.                   hr =  pencc->Next( 1,
  135.                       &pncc,
  136.                       &ulCount );
  137.             }
  138.  
  139.             //Release the enumerator interface.
  140.             pencc->Release();
  141.       }
  142.  
  143.       //Uninitialize the Inetcfg interface.
  144.       pnc->Uninitialize ();
  145.  
  146.       //Release the write lock.
  147.       pncLock->ReleaseWriteLock ();
  148.  
  149.       //Release the lock interface
  150.       pncLock->Release();
  151.  
  152.       //Release the pnc interface.
  153.       pnc->Release ();
  154.  
  155.       //Uninitialize the COM.
  156.       CoUninitialize();
  157.  
  158.       return 0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement