How to Enable/Disable IPV6 Programmatically in Windows Vista?

 

 

Why do you want to disable IPV6?

                Before you want to disable IPV6, you need to carefully think and evaluate what kind of problem you are facing and how that is related to IPV6. We highly recommend that you isolate the component that is misbehaving [Might be a device or some component in your application] and fix that, rather than disabling IPV6. To me, disabling IPV6 is wrong and not recommended.

 

Disabling IPV6 Programmatically:

               There are no straight forward SDK API’s to disable IPV6. You need to use the Device Driver Kit [DDK] interface INetCfg to enumerate all the bindings for the IPV6 component and disable them. When you disable the appropriate bindings, the NIC properties panel also gets updated .

 

Code Sample:

==========

Note: You need to download and install the Windows Driver Kit and configure Visual Studio to use the appropriate libraries and header files for the below program

               to successfully Compile & Run. You need to run this program under an “Elevated Admin context”.

            

                If you would like to explore the INetCfg family of interfaces to its extent, I will highly recommend you to go through the “BindView” sample that ships with the Windows Driver Kit.

          

//=====================================================================================================

//Summary: Demonstrates how to programmatically disable the IPV6 in Vista using INetCfg Interface.

//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.

//======================================================================================================

#include <stdio.h>

#include <Netcfgx.h>

#include <devguid.h>

#include <TCHAR.H>

#define APP_NAME L"DisableIPV6"

#define LOCK_TIME_OUT 5000

//Modify this flag to Enable/Disable IPV6.

#define ENABLE FALSE

const GUID *pguidNetClass = &GUID_DEVCLASS_NETTRANS;

int main()

{

      INetCfg *pnc = NULL;

      HRESULT hr = S_OK;

      //Initialize COM

      hr = CoInitialize(NULL);

      //Create the INetCfg Interface Pointer

      hr = CoCreateInstance( CLSID_CNetCfg,

                               NULL, CLSCTX_INPROC_SERVER,

                               IID_INetCfg,

                               (void**)&pnc );

      //Get the lock before we Initialize the INetCfg object,

      LPWSTR lpszApp;

      INetCfgLock *pncLock = NULL;

      hr = pnc->QueryInterface( IID_INetCfgLock,(LPVOID *)&pncLock );

      if ( hr == S_OK )

      {

              hr = pncLock->AcquireWriteLock(

                                      LOCK_TIME_OUT,APP_NAME,&lpszApp

                                      );

      }

      //Initialize the INetCfg Object.

      hr = pnc->Initialize( NULL );

      //Get the enumerator interface

      IEnumNetCfgComponent *pencc = NULL;

      INetCfgClass *pncclass;

      hr = pnc->QueryNetCfgClass(

                                 pguidNetClass,

                                  IID_INetCfgClass,

                                  (PVOID *)&pncclass

                                );

      if ( hr == S_OK )

      {

        hr = pncclass->EnumComponents( &pencc );

      }

  INetCfgComponent *pncc = NULL;

      if(hr == S_OK)

      {

   ULONG ulCount;

   pencc->Reset();

            hr = pencc->Next( 1,&pncc,&ulCount );

            while(hr == S_OK)

            {

                  LPWSTR lpszItemName;

                 

                  pncc->GetId (&lpszItemName);

                  //If this is Internet Protocol Version 6 then goahead to

                  //enumerate the bindings.

                  if( wcscmp(lpszItemName,TEXT("ms_tcpip6")) == 0)

                  {

                        wprintf(L"Component Name = %s\n",lpszItemName);

                        //Enumerate the Bindings and Disable them.

                        IEnumNetCfgBindingPath *pencbp = NULL;

                        INetCfgComponentBindings *pnccb = NULL;

                        INetCfgBindingPath *pncbp = NULL;

                       

                        hr = pncc->QueryInterface(

                                                IID_INetCfgComponentBindings,

                 (PVOID *)&pnccb

                                                  );

                        if ( hr == S_OK )

                        {

                  // Get binding path enumerator reference.

                  hr = pnccb->EnumBindingPaths( EBP_BELOW,&pencbp );

   pnccb->Release ();

                        ULONG ulCount;

                        pencbp->Reset();

                        //Enumerate the Bindings.

                        hr = pencbp->Next( 1,&pncbp,&ulCount );

                        //Loop until all the bindings are enumerated

                          //and disabled.

                        while( hr == S_OK )

                        {

                              //Disable this binding.

                        hr = pncbp->Enable (ENABLE);

                              //Save the changes,

                              pnc->Apply ();

                              //Release the interface and set it to

                               //NULL.

                        pncbp->Release ();

                              pncbp = NULL;

                                   

                              //Enumerate Next Binding if any.

                              hr = pencbp->Next( 1,&pncbp,&ulCount );

                        }

                        //We have enumerated all the bindings and

                         //disabled them.Release the interfaces.

                              pencbp->Release();

                        }

                  }

           

                  pncc->Release ();

                  pncc = NULL;

                  //Enumerate the next component.

                  hr = pencc->Next( 1,

                      &pncc,

                      &ulCount );

            }

            //Release the enumerator interface.

            pencc->Release();

      }

      //Uninitialize the Inetcfg interface.

      pnc->Uninitialize ();

      //Release the write lock.

      pncLock->ReleaseWriteLock ();

      //Release the lock interface

    pncLock->Release();

      //Release the pnc interface.

      pnc->Release ();

      //Uninitialize the COM.

      CoUninitialize();

      return 0;

}

 

Balajee

Windows SDK