Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //=====================================================================================================
- //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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement