Here i am demonstrating on how to disconnect or how to close all network connections in a system by programming uing Win32 C++. There are hardly certain situations where we need to disconnect some, or all of the network connections and close the adapters. Anyway this can be done directly through a few API calls in Windows. Nothing is big deal about doing this task, and its quite easy.
How to do.
There is a Windows API function named GetInterfaceInfo() in the IP Helper functions library which allows us to enumerate all available Network Adapters installed in a system. Well, so we got all available adapters list filled by the above function. Next, our task is to release the adapter/adapters.. For that, we can use another IP helper API named IpReleaseAddress(). This function releases an IP address previously obtained through Dynamic Host Configuration Protocol (DHCP). We can set a loop, so all adapters can be Released.
Code.
Now here's the code outline...
#include "IpExport.h"
#include "Iphlpapi.h"
#pragma comment( lib, "iphlpapi" )
...
// Call GetInterfaceInfo() with empty buffer to get the required allocation size.
ULONG ulInfoLength = 0;
GetInterfaceInfo( 0, &ulInfoLength );
// Allocate buffer for PIP_INTERFACE_INFO
LPBYTE pInfoBuffer = new BYTE[ ulInfoLength ];
PIP_INTERFACE_INFO pInfo = ( PIP_INTERFACE_INFO )pInfoBuffer;
// Call GetInterfaceInfo() with valid buffer to get all Network Adapters
GetInterfaceInfo( pInfo, &ulInfoLength );
// Release all adapters one by one
for( LONG i = 0; iNumAdapters; ++i )
IpReleaseAddress( &pInfo->Adapter[ i ] );
// Delete the IP_INTERFACE_INFO buffer
delete []pInfoBuffer;
Thats it... now dont forget to leave me Feedbacks :)
i was stuck for two days. Thanks for your help :)
ReplyDelete