Click to See Complete Forum and Search --> : Turning on and off Internet connection under WinXP
Marcin Wronsky
February 23rd, 2008, 01:34 AM
I have such problem: i want to write a simple program in C++ running under Windows XP that will in some situations turn off internet connection (just like we can do by clicking "disconnect" button) and in some situations will turn it on. Is this possible to do under XP? Can i use the command line for this? Is there some windows command to do that or i need to use some WinApi function to achive my goal or is there any other way?
Regards,
Marcin Wronsky
Marcin Wronsky
February 23rd, 2008, 07:49 AM
i've used google but i didn't found anything usefull. This must be really difficult problem...
0xC0000005
February 23rd, 2008, 08:59 AM
Are you looking for something like the command line ipconfig /release /renew function? If so, then this is a basic implementation:
#include "Iphlpapi.h"
#pragma comment(lib,"Iphlpapi")
enum enumINet { INET_RELEASE, INET_RENEW };
void INetConnection(enumINet e)
{
// Release/Renew may take a few seconds, so...
CWaitCursor wait;
// Get interface info
DWORD dwSize = 0;
::GetInterfaceInfo(0,&dwSize);
BYTE *pBytes = new BYTE[dwSize];
const PIP_INTERFACE_INFO pInfo = (PIP_INTERFACE_INFO)pBytes;
::GetInterfaceInfo(pInfo,&dwSize);
// Release/renew each adapter
for( int i=0; i<pInfo->NumAdapters; ++i ){
if(INET_RELEASE==e)
::IpReleaseAddress(&pInfo->Adapter[i]);
else if(INET_RENEW==e)
::IpRenewAddress(&pInfo->Adapter[i]);
}
// Delete interface info buffer
delete [] pBytes;
}
The code uses IP helper library functions so you will need to include "Iphlpapi.h" and link to the Iphlpapi library as shown in the first two lines above (although these lines should be in stdafx.h).
The IP helper library is not included with VisualStudio 6 (don't know about the .NET versions) so you may have to download the SDK - or at least obtain iphlpapi.h and iphlpapi.lib from somewhere.
The code iterates through and releases/renews each adapter in the case of multiple adapters so you may want to modify it to release/renew specific adpaters only (check the 'Name' member of pInfo->Adapter[i]).
Most computers will probably have only one adapter but not always. Mine has two: an IDE bus card LAN connections and a PCMCIA card for wireless.
fred100
February 23rd, 2008, 10:14 AM
I have such problem: i want to write a simple program in C++ running under Windows XP that will in some situations turn off internet connection (just like we can do by clicking "disconnect" button)
which "disconnect" button ?
you can easily enable/disable local area connections with apis, but there is no "disconnect" button
Marcin Wronsky
February 23rd, 2008, 09:20 PM
0xC0000005 - Thanks for replay!
Your solutions works well and i already have whole program i wanted. The only problem is that it is "ipconfig \release" and "ipconfig \renew" implementation. I have 5 connections defined with 4 adapters (usually 2 are on) and one of them is ADSL modem connected by USB port and ipconfig seems not working for such adapters.
I managed to find out that there is "rasdial" command that handle such connections but i wonder if there is implementation of rasdial command in IP helper library or any other library?
For now i connect my ADSL modem by running .bat script with rasdial command but it is not optimal solution as console window is apearing. I still can hide it but i bet there is much simpler way to do this
fred 100- in my WinXP (well polish edition not english) if i right click on connection icon i get "disconnect" button when it is on and "connect" when it is off.
0xC0000005
February 24th, 2008, 02:05 PM
I know nothing about RASDIAL functions and so I'm not sure if either of these links will help, but here is info about the RADSIAL CLIENT API:
http://msdn2.microsoft.com/en-us/library/aa446739%28VS.85%29.aspx
And a note about obtaining an IP address from a RASDIAL client:
How To Obtain the IP Address Assigned to a RAS Client (http://support.microsoft.com/kb/160622)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.