获取网络连接名称“本地连接”的两种方法



方法一:通过WMI得到连接属性NETCON_PROPERTIES,这个结构体定义如下:

typedef struct tagNETCON_PROPERTIES
    {
    GUID guidId;
    LPWSTR pszwName;
    LPWSTR pszwDeviceName;
    NETCON_STATUS Status;
    NETCON_MEDIATYPE MediaType;
    DWORD dwCharacter;
    CLSID clsidThisObject;
    CLSID clsidUiObject;
    } NETCON_PROPERTIES;
pszwName就是“本地连接”的网络连接名称。
给出控制台程序源码:

#include "stdafx.h"
#include
#pragma comment(lib,"Iphlpapi.lib")
#include

#pragma comment(lib,"Rpcrt4.lib")//GUID
#include //启用、禁用网卡

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

int main(int argc, char* argv[])
{
INetConnectionManager *pManager;
INetConnection *pConnection;
IEnumNetConnection *pEnum;
ULONG           celtFetched;
CoInitialize(NULL);
CoCreateInstance(CLSID_ConnectionManager,   NULL,   CLSCTX_SERVER,   IID_INetConnectionManager,   (void**)&pManager);
pManager->EnumConnections(NCME_DEFAULT,   &pEnum);
pManager->Release();   
while(   pEnum->Next(1,   &pConnection,   &celtFetched)   ==   S_OK   )
{
NETCON_PROPERTIES*   properties;
pConnection->GetProperties(&properties);
DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,properties->pszwName,-1,NULL,0,NULL,FALSE);
char *psText;
psText = new char[dwNum];
if(!psText)
{
delete []psText;
}
WideCharToMultiByte (CP_OEMCP,NULL,properties->pszwName,-1,psText,dwNum,NULL,FALSE);
printf(psText);
printf("\r\n");
delete []psText;
}
return 0;
}

方法二:通过注册表读取,路径如下:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}
在这个键下面每一个网卡对应有一个子键Connection,再下一级就有一个Name的键值就是网络连接名称了。

你可能感兴趣的:(Windows)