Renaming Network Connection programmatically fails

0

I have written some C++ code to automatically rename the Network Connection in ncpa.cpl. It works fine on Win7 and Win8 but fails in Win10. The function I used is INetConnection::Rename, its return value is 0x80071a90, which means:

HRESULT_FROM_WIN32(ERROR_TRANSACTIONAL_CONFLICT) : The function attempted to use a name that is reserved for use by another transaction.

But the new connection name I used is something like "Npcap Loopback Adapter", which doesn't seem to be a "reserved" name for Windows.

Someone told me that the built-in netsh.exe tool also uses this way to rename an interface, I have tried the command as "netsh.exe interface set interface name="Ethernet 5" newname="Npcap Loopback Adapter"" and the interface "Ethernet 5" is successfully renamed to "Npcap Loopback Adapter". So I think this way should work out, there must be something wrong in my code.

I want to implement this in C/C++, so don't tell me to wrap the netsh.exe command, I wonder what's wrong with my function call code? Thanks.

My code is:

/*++

Copyright (c) Nmap.org.  All rights reserved.

Module Name:

    LoopbackRename.cpp

Abstract:

    This is used for enumerating our "Npcap Loopback Adapter" using NetCfg API, if found, we changed its name from "Ethernet X" to "Npcap Loopback Adapter".
    Also, we need to make a flag in registry to let the Npcap driver know that "this adapter is ours", so send the loopback traffic to it.

This code is modified based on example: https://msdn.microsoft.com/en-us/library/windows/desktop/aa364686.aspx
--*/

#pragma warning(disable: 4311 4312)

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <objbase.h>
#include <netcon.h>
#include <stdio.h>

#include "LoopbackRename.h"

#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "oleaut32.lib")

#define         NPCAP_LOOPBACK_INTERFACE_NAME           NPF_DRIVER_NAME_NORMAL_WIDECHAR L" Loopback Adapter11"
#define         BUF_SIZE                                255

BOOL DoTheWork(INetSharingManager *pNSM, wchar_t strDeviceName[])
{   // add a port mapping to every firewalled or shared connection 
    BOOL bFound = FALSE;
    INetSharingEveryConnectionCollection * pNSECC = NULL;
    HRESULT hr = pNSM->get_EnumEveryConnection (&pNSECC);
    if (!pNSECC)
        wprintf (L"failed to get EveryConnectionCollection!\r\n");
    else {

        // enumerate connections
        IEnumVARIANT * pEV = NULL;
        IUnknown * pUnk = NULL;
        hr = pNSECC->get__NewEnum (&pUnk);
        if (pUnk) {
            hr = pUnk->QueryInterface (__uuidof(IEnumVARIANT),
                (void**)&pEV);
            pUnk->Release();
        }
        if (pEV) {
            VARIANT v;
            VariantInit (&v);

            while ((S_OK == pEV->Next (1, &v, NULL)) && (bFound == FALSE)) {
                if (V_VT (&v) == VT_UNKNOWN) {
                    INetConnection * pNC = NULL;
                    V_UNKNOWN (&v)->QueryInterface (__uuidof(INetConnection),
                        (void**)&pNC);
                    if (pNC) {
                        NETCON_PROPERTIES *pNETCON_PROPERTIES;
                        pNC->GetProperties(&pNETCON_PROPERTIES);

                        wchar_t currentGUID[BUF_SIZE];
                        GUID guid = pNETCON_PROPERTIES->guidId;
                        wsprintf(currentGUID, L"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}", 
                            guid.Data1, guid.Data2, guid.Data3, 
                            guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
                            guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);

                        if (wcscmp(currentGUID, strDeviceName) == 0)
                        {
                            hr = pNC->Rename(NPCAP_LOOPBACK_INTERFACE_NAME);
                            bFound = TRUE;
                            if (hr != S_OK)
                            {
                                wprintf(L"failed to create rename NPCAP_LOOPBACK_INTERFACE_NAME\r\n");
                            }
                        }

                        pNC->Release();
                    }
                }
                VariantClear(&v);
            }
            pEV->Release();
        }
        pNSECC->Release();
    }

    return bFound;
}

BOOL RenameLoopbackNetwork(wchar_t strDeviceName[])
{
    BOOL bResult = FALSE;
/*  CoInitialize (NULL);*/

    // init security to enum RAS connections
    CoInitializeSecurity (NULL, -1, NULL, NULL, 
        RPC_C_AUTHN_LEVEL_PKT, 
        RPC_C_IMP_LEVEL_IMPERSONATE,
        NULL, EOAC_NONE, NULL);

    INetSharingManager * pNSM = NULL;    
    HRESULT hr = ::CoCreateInstance (__uuidof(NetSharingManager),
        NULL,
        CLSCTX_ALL,
        __uuidof(INetSharingManager),
        (void**)&pNSM);
    if (!pNSM)
    {
        wprintf (L"failed to create NetSharingManager object\r\n");
        return bResult;
    }
    else {

        // add a port mapping to every shared or firewalled connection.
        bResult = DoTheWork(pNSM, strDeviceName);

        pNSM->Release();
    }

/*  CoUninitialize ();*/

    return bResult;
}

Update:

I have tried this function call 100 times in a loop, but all failed.

if (wcscmp(currentGUID, strDeviceName) == 0)
{
    int iTime = 0;
aaa:
    hr = pNC->Rename(NPCAP_LOOPBACK_INTERFACE_NAME);
    bFound = TRUE;
    if (hr == HRESULT_FROM_WIN32(ERROR_TRANSACTIONAL_CONFLICT) && iTime < 100)
    {
        iTime ++;
        goto aaa;
    }
    else if (hr != S_OK)
    {
        wprintf(L"failed to create rename NPCAP_LOOPBACK_INTERFACE_NAME\r\n");
    }
}

pNC->Release();

The code is open-source in github:

https://github.com/nmap/npcap/tree/master/packetWin7/NPFInstall, it's a Visual Studio 2005 project, but actually you can build it in other Visual Studio versions. After compiling out NPFInstall.exe, use command "NPFInstall.exe -il" to install a new loopback adapter and rename it to "Npcap Loopback Adapter", "NPFInstall.exe -ul" to uninstall. the source to rename the adapter is: LoopbackRename.cpp. I hope this can help solve the problem.

c
networking
windows-10
npcap
asked on Stack Overflow Jul 17, 2015 by Yang Luo • edited Nov 5, 2020 by Matt Davis

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0