powershell’s “get-netadapter” replacement using C/C++ APIs

I need to obtain the network adapter GUID given the connection name as shown by Windows network manager. Powershell’s get-netadapter does the job, but I’d like to do it in C/C++. I know that thare are two set of API function/methods:

  • iphlpapi.h
  • netlistmgr.h

Both seems to do part of the work. In particular, by IEnumNetworks and related classes I managed to list some networks/adapters. E.G., this is the output of get-netadapter:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Name InterfaceDescription ifIndex Status MacAddress LinkSpeed
---- -------------------- ------- ------ ---------- ---------
My Tunnel TAP-Windows Adapter V9 #2 40 Disconnected 00-FF-E6-0A-89-C6 1 Gbps
Ethernet 4 Intel(R) PRO/1000 MT Desktop Adapter #2 30 Up 08-00-27-95-74-A2 1 Gbps
Local Area Connection TAP-Windows Adapter V9 15 Disconnected 00-FF-CC-95-DA-46 1 Gbps
Ethernet 3 Intel(R) PRO/1000 MT Desktop Adapter 4 Up 08-00-27-AC-C1-5A 1 Gbps
</code>
<code>Name InterfaceDescription ifIndex Status MacAddress LinkSpeed ---- -------------------- ------- ------ ---------- --------- My Tunnel TAP-Windows Adapter V9 #2 40 Disconnected 00-FF-E6-0A-89-C6 1 Gbps Ethernet 4 Intel(R) PRO/1000 MT Desktop Adapter #2 30 Up 08-00-27-95-74-A2 1 Gbps Local Area Connection TAP-Windows Adapter V9 15 Disconnected 00-FF-CC-95-DA-46 1 Gbps Ethernet 3 Intel(R) PRO/1000 MT Desktop Adapter 4 Up 08-00-27-AC-C1-5A 1 Gbps </code>
Name                      InterfaceDescription                    ifIndex Status       MacAddress             LinkSpeed
----                      --------------------                    ------- ------       ----------             ---------
My Tunnel                 TAP-Windows Adapter V9 #2                    40 Disconnected 00-FF-E6-0A-89-C6         1 Gbps
Ethernet 4                Intel(R) PRO/1000 MT Desktop Adapter #2      30 Up           08-00-27-95-74-A2         1 Gbps
Local Area Connection     TAP-Windows Adapter V9                       15 Disconnected 00-FF-CC-95-DA-46         1 Gbps
Ethernet 3                Intel(R) PRO/1000 MT Desktop Adapter          4 Up           08-00-27-AC-C1-5A         1 Gbps

My goal is to get “My Tunnel” adapter object (ifIndex or GUID, at least), but when the network is not “connected”, I don’t get any adapter. Here my “quick and dirty” test program (error checks, main and includes omitted for clarity):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> CoInitializeEx(NULL, COINITBASE_MULTITHREADED);
// get a network list manager object
INetworkListManager *mgr;
CoCreateInstance(CLSID_NetworkListManager, NULL, CLSCTX_ALL, IID_INetworkListManager, (LPVOID*)&mgr);
// get the enumerator for existing networks
IEnumNetworks *enn;
mgr->GetNetworks(NLM_ENUM_NETWORK_ALL, &enn);
for (;;) { {
INetwork *net;
ULONG n = 0;
if (S_OK != enn->Next(1, &net, &n) || n < 1) {
break;
}
// print the name of current network object
BSTR str;
net->GetName(&str); printf(" Name: %wsn", str); SysFreeString(str);
// get the enumerator for network connections belonging to the current network
IEnumNetworkConnections *enc;
net->GetNetworkConnections(&enc);
INetworkConnection *nc;
for (;;) {
if (S_OK != enc->Next(1, &nc, &n)) {
break;
}
// explore the list of connections and print the adapter's GUID
nc->GetAdapterId(&g);
printf(" Adapter: {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}n",
g.Data1, g.Data2, g.Data3, g.Data4[0], g.Data4[1], g.Data4[2],
g.Data4[3], g.Data4[4], g.Data4[5], g.Data4[6], g.Data4[7]);
}
}
</code>
<code> CoInitializeEx(NULL, COINITBASE_MULTITHREADED); // get a network list manager object INetworkListManager *mgr; CoCreateInstance(CLSID_NetworkListManager, NULL, CLSCTX_ALL, IID_INetworkListManager, (LPVOID*)&mgr); // get the enumerator for existing networks IEnumNetworks *enn; mgr->GetNetworks(NLM_ENUM_NETWORK_ALL, &enn); for (;;) { { INetwork *net; ULONG n = 0; if (S_OK != enn->Next(1, &net, &n) || n < 1) { break; } // print the name of current network object BSTR str; net->GetName(&str); printf(" Name: %wsn", str); SysFreeString(str); // get the enumerator for network connections belonging to the current network IEnumNetworkConnections *enc; net->GetNetworkConnections(&enc); INetworkConnection *nc; for (;;) { if (S_OK != enc->Next(1, &nc, &n)) { break; } // explore the list of connections and print the adapter's GUID nc->GetAdapterId(&g); printf(" Adapter: {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}n", g.Data1, g.Data2, g.Data3, g.Data4[0], g.Data4[1], g.Data4[2], g.Data4[3], g.Data4[4], g.Data4[5], g.Data4[6], g.Data4[7]); } } </code>
    CoInitializeEx(NULL, COINITBASE_MULTITHREADED);

// get a network list manager object
    INetworkListManager *mgr;
    CoCreateInstance(CLSID_NetworkListManager, NULL, CLSCTX_ALL, IID_INetworkListManager, (LPVOID*)&mgr);

// get the enumerator for existing networks    
    IEnumNetworks *enn;
    mgr->GetNetworks(NLM_ENUM_NETWORK_ALL, &enn);
    for (;;) {                {
      INetwork *net;
      ULONG n = 0;
      if (S_OK != enn->Next(1, &net, &n) || n < 1) {
        break;
      }
  // print the name of current network object
      BSTR str;
      net->GetName(&str); printf(" Name: %wsn", str); SysFreeString(str);
  
  // get the enumerator for network connections belonging to the current network                          
      IEnumNetworkConnections *enc;
      net->GetNetworkConnections(&enc);
      INetworkConnection *nc;
      for (;;) {
        if (S_OK != enc->Next(1, &nc, &n)) {
          break;
        }
  // explore the list of connections and print the adapter's GUID
        nc->GetAdapterId(&g);
        printf(" Adapter: {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}n",
               g.Data1, g.Data2, g.Data3, g.Data4[0], g.Data4[1], g.Data4[2],
               g.Data4[3], g.Data4[4], g.Data4[5], g.Data4[6], g.Data4[7]);
      }
    }

Here the output I get:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> Name: Network
Name: Local Area Connection
Name: mygroup.net
Adapter: {e1b4e605-c6ee-4f03-b4c6-0d879a27669b}
Name: My Tunnel
Name: Unidentified network
Adapter: {12d9ae29-7863-414f-b195-e1230d8c697b}
</code>
<code> Name: Network Name: Local Area Connection Name: mygroup.net Adapter: {e1b4e605-c6ee-4f03-b4c6-0d879a27669b} Name: My Tunnel Name: Unidentified network Adapter: {12d9ae29-7863-414f-b195-e1230d8c697b} </code>
 Name: Network
 Name: Local Area Connection
 Name: mygroup.net
 Adapter: {e1b4e605-c6ee-4f03-b4c6-0d879a27669b}
 Name: My Tunnel
 Name: Unidentified network
 Adapter: {12d9ae29-7863-414f-b195-e1230d8c697b}

“My Tunnel” doesn’t seem to have any adapter, although powershell is able to retrieve it.
The adapter appears only after I established a connection (a VPN in this case).
How can I get the adapters in the same way powershell does?

NOTE: what I need is the ability to get the network adapter given the network name. By iphlpapi I can retreive other information (e.g. InterfaceDescription), but not the network name. It seems that the “network name” is not part of the “core networking” of Windows, but of a sort of managing layer instead. So, the only solution I have found right now is CreateProcess("powershell.exe",...)… uhm…

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật