CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2008
    Posts
    3

    Turning on and off Internet connection under WinXP

    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

  2. #2
    Join Date
    Feb 2008
    Posts
    3

    Re: Turning on and off Internet connection under WinXP

    i've used google but i didn't found anything usefull. This must be really difficult problem...

  3. #3
    Join Date
    May 2002
    Posts
    1,435

    Re: Turning on and off Internet connection under WinXP

    Are you looking for something like the command line ipconfig /release /renew function? If so, then this is a basic implementation:
    Code:
    #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.

  4. #4
    Join Date
    Jan 2008
    Posts
    178

    Re: Turning on and off Internet connection under WinXP

    Quote Originally Posted by Marcin Wronsky
    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

  5. #5
    Join Date
    Feb 2008
    Posts
    3

    Re: Turning on and off Internet connection under WinXP

    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.

  6. #6
    Join Date
    May 2002
    Posts
    1,435

    Re: Turning on and off Internet connection under WinXP

    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/lib...8VS.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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured