//当然看这篇文章之前你要实现虚拟网卡的启动功能,这个功能的实现有很多,我就不贴这个代码了
//下面就来实现怎么把本地网卡共享给虚拟网卡,让虚拟网卡发出热点。让手机也能连接上网、
//1.现在相关头文件和链接(注意:不要在VC6.0版本测试,因为VC6.0不包含下面的部分头文件和链接)
//推荐使用(VS2010-vs2013版本,还有win7-win8.1系统)
//以下的代码可以直接复制到项目中,文字的地方已经被注释了,方便复制代码
//由于时间的问题,先写了寻找网卡的代码,
//等有时间待续。
#include <NetCon.h>
#include <locale>
#pragma comment(lib,"Iphlpapi.lib")
#pragma comment(lib,"Rpcrt4.lib")//GUID
//启用、禁用网卡
#pragma comment(lib,"ole32.lib")//CoCreateInstance函数需要
//2.声明相关的变量和赋初值,以下代码直接可以复制到main函数里面
INetConnectionManager *pManager=NULL;
INetConnection *pConnection=NULL;
IEnumNetConnection *pEnum=NULL;
ULONG celt;
INetSharingManager *pNetSharingManager=NULL;
INetConnectionProps *pProps=NULL;
INetSharingConfiguration *pConfiguration=NULL;
NETCON_PROPERTIES* Nproperties=NULL;
//3.
CoInitialize(NULL);//初始化COM库
//扩展知识:用来告诉 Windows以单线程的方式创建com对象,CoInitialize和CoUninitialize必须成对使用。
CoCreateInstance(CLSID_ConnectionManager, NULL, CLSCTX_SERVER, IID_INetConnectionManager, (void**)&pManager);//创建一个com对象
//CLSID_ConnectionManager是创建的Com对象的类标识符(CLSID)
//IID_INetConnectionManager是创建的Com对象的接口标识符
//最后一个参数是用来接收指向Com对象接口地址的指针变量
//4.前面的都准备后了,接下来就是实现的代码了。
//先做一下是否成功创建了com对象
if(pManager == NULL)
{
printf("产生异常,创建失败,Error:%d\n",GetLastError());
return 0;
}
//接下来先看看一个函数的解释:
/*
virtual HRESULT STDMETHODCALLTYPE EnumConnections(
[in] NETCONMGR_ENUM_FLAGS Flags,
[out] __RPC__deref_out_opt IEnumNetConnection **ppEnum)
其中: 第一个参数可以有以下的参数选择、
NCME_DEFAULT = 0,
NCME_HIDDEN = 0x1
*/
pManager->EnumConnections(NCME_DEFAULT, &pEnum);//开始枚举网卡
while( pEnum->Next(1, &pConnection, &celt) == S_OK )
{
/*
条件会返回2个值,分别是S_OK,S_FALSE
#define S_OK ((HRESULT)0L)
#define S_FALSE ((HRESULT)1L)
*/
pConnection->GetProperties(&NNproperties);
if(NNproperties->Status == NCS_CONNECTED)//查询连接状态的网卡
{
wprintf(L"%s\n",Nproperties->pszwDeviceName);//pszwDeviceName储存着网卡的驱动名字
}
if(NNproperties->Status == NCS_DISCONNECTING)//查询断开连接状态的网卡
{
wprintf(L"%s\n",Nproperties->pszwDeviceName);
}
/*
其中状态值有下面的值:
NCS_DISCONNECTED = 0,
NCS_CONNECTING = ( NCS_DISCONNECTED + 1 ) ,
NCS_CONNECTED = ( NCS_CONNECTING + 1 ) ,
NCS_DISCONNECTING = ( NCS_CONNECTED + 1 ) ,
NCS_HARDWARE_NOT_PRESENT = ( NCS_DISCONNECTING + 1 ) ,
NCS_HARDWARE_DISABLED = ( NCS_HARDWARE_NOT_PRESENT + 1 ) ,
NCS_HARDWARE_MALFUNCTION = ( NCS_HARDWARE_DISABLED + 1 ) ,
NCS_MEDIA_DISCONNECTED = ( NCS_HARDWARE_MALFUNCTION + 1 ) ,
NCS_AUTHENTICATING = ( NCS_MEDIA_DISCONNECTED + 1 ) ,
NCS_AUTHENTICATION_SUCCEEDED = ( NCS_AUTHENTICATING + 1 ) ,
NCS_AUTHENTICATION_FAILED = ( NCS_AUTHENTICATION_SUCCEEDED + 1 ) ,
NCS_INVALID_ADDRESS = ( NCS_AUTHENTICATION_FAILED + 1 ) ,
NCS_CREDENTIALS_REQUIRED = ( NCS_INVALID_ADDRESS + 1 )
NETCON_STATUS;
*/
}
//最后就是结束程序后的工作了,如果写在了一个类里面,直接可以把下面的代码放在斥构函数里面
if(pManager) pManager->Release();
if(pConnection) pConnection->Release();
if(pEnum) pEnum->Release();
if(pNetSharingManager) pNetSharingManager->Release();
if(pConfiguration) pConfiguration->Release();
CoUninitialize();//记得添加
//待续