selecting network interfaces

Hello everyone,
I have been trying to find a way to activate/inactivate network cards in .NET, kinda like ifconfig up and ifconfig down on Linux. The reason is that I simply don't want Windows to automatically choose one interface over the other and use this, but to decide for myself.

Is there any good way to do this in .NET I found that IP Helper API can list all interfaces, but it does not seem to be able to manage them. Thanks a lot for any tips!

/ Roland


Answer this question

selecting network interfaces

  • Gaturam

    Well, a friend told me how to do this. If anyone else wonders, simply use

    socket.bind(EndPoint)

    (how could I miss it )

    Thanks again all.


  • FigBug

    Here is some sample c++ code that I have used in some of out testing.  It enables and disables all network adapters on the machine.  I realize that it is not exactly what you are looking for, but you can use it as a template to get you started.  You can create a COM interop assembly for the COM objects used in this code (namely, the IEnumConnectionManager and INetConnection objects) using the AddReference wizard in Visual Studio.Net.



       

    HRESULT DisableEnableConnections(BOOL bEnable)

    {

    HRESULT hr = E_FAIL;

    CoInitialize(NULL);

    INetConnectionManager *pNetConnectionManager = NULL;

    hr = CoCreateInstance(CLSID_ConnectionManager,

    NULL,

    CLSCTX_LOCAL_SERVER | CLSCTX_NO_CODE_DOWNLOAD,

    IID_INetConnectionManager,

    reinterpret_cast<LPVOID *>(&pNetConnectionManager)

    );

    if (SUCCEEDED(hr))

    {

    // Get an enumurator for the set of connections on the system

    IEnumNetConnection* pEnumNetConnection;

    pNetConnectionManager->EnumConnections(NCME_DEFAULT, &pEnumNetConnection);

    ULONG ulCount = 0;

    BOOL fFound = FALSE;

    hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);

    HRESULT hrT = S_OK;

    // Enumurate through the list of adapters on the system and look for the one we want

    // NOTE: To include per-user RAS connections in the list, you need to set the COM

    // Proxy Blanket on all the interfaces. This is not needed for All-user RAS

    // connections or LAN connections.

    do

    {

    NETCON_PROPERTIES* pProps = NULL;

    INetConnection * pConn;

    // Find the next (or first connection)

    hrT = pEnumNetConnection->Next(1, &pConn, &ulCount);

    if (SUCCEEDED(hrT) && 1 == ulCount)

    {

    hrT = pConn->GetProperties(&pProps); // Get the connection properties

    if (S_OK == hrT)

    {

    printf("* %S\n", pProps->pszwName);

    if (bEnable)

    {

    printf(" Enabling adapter: %S...\n",pProps->pszwName);

    hr = pConn->Connect();

    }

    else

    {

    printf(" Disabling adapter: %S...\n",pProps->pszwName);

    hr = pConn->Disconnect();

    }

    CoTaskMemFree (pProps->pszwName);

    CoTaskMemFree (pProps->pszwDeviceName);

    CoTaskMemFree (pProps);

    }

    pConn->Release();

    pConn = NULL;

    }

    } while (SUCCEEDED(hrT) && 1 == ulCount && !fFound);

    if (FAILED(hrT))

    {

    hr = hrT;

    }

    pEnumNetConnection->Release();

    }

    if (FAILED(hr) && hr != HRESULT_FROM_WIN32(ERROR_RETRY))

    {

    printf("Could not enable or disable connection (0x%08x)\r\n", hr);

    }

    pNetConnectionManager->Release();

    CoUninitialize();

    return hr;

    }



     


  • Jelle van der Beek30865

    Ahh, yes. We could have pointed you in that direction if you had mentioned that it was for use with a socket.  Your original question only specified that you wanted to disable network adapters.

  • BlkShdw88

    Thanks guys. Actually what I would like to do is select which one of the network interfaces on the local computer to bind a socket to. I thought I have to turn of network interfaces to do so, as it seems .NET selects one automatically. However I heard you should be able to chose which ip address (on client side) to connect the socket to. Haven't found it though, anyone heard about this
  • Steve_Black

    Thanks a lot JonCole. I don't got so much experience in this area, but I will try to understand the code. I had hoped it would be an easier way doing this, given it's so easy on Linux  :). 

      

  • adelgado

    Let me know if you have trouble doing what I suggest above.  I can give you more details if you need it.

  • Parrot

  • Anthony Kautz

    Guess there is no such easy command line utility that comes with OS. But my little search revealed that DevCon
    [
    excerpt from http://www.robvanderwoude.com/index.html

    DEVCON is a Microsoft tool that allows "device management" from the command line.

    It is available for free at http://support.microsoft.com/default.aspx scid=kb;EN-US;Q311272.

    ]
    allows you to do this.

    Also see
    http://www.novell.com/coolsolutions/tools/14784.html


  • selecting network interfaces