C++实现wifi共享(本地网卡共享虚拟网卡)(1)

//当然看这篇文章之前你要实现虚拟网卡的启动功能,这个功能的实现有很多,我就不贴这个代码了
//下面就来实现怎么把本地网卡共享给虚拟网卡,让虚拟网卡发出热点。让手机也能连接上网、
//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();//记得添加
//待续

 

虚拟网卡驱动源代码(原版): /* * snull.c -- the Simple Network Utility * * Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet * Copyright (C) 2001 O'Reilly & Associates * * The source code in this file can be freely used, adapted, * and redistributed in source or binary form, so long as an * acknowledgment appears in derived source files. The citation * should list that the code comes from the book "Linux Device * Drivers" by Alessandro Rubini and Jonathan Corbet, published * by O'Reilly & Associates. No warranty is attached; * we cannot take responsibility for errors or fitness for use. * * $Id: snull.c,v 1.21 2004/11/05 02:36:03 rubini Exp $ */ #include #include #include #include #include #include /* printk() */ #include /* kmalloc() */ #include /* error codes */ #include /* size_t */ #include /* mark_bh */ #include #include /* struct device, and other headers */ #include /* eth_type_trans */ #include /* struct iphdr */ #include /* struct tcphdr */ #include #include "snull.h" #include #include MODULE_AUTHOR("Alessandro Rubini, Jonathan Corbet"); MODULE_LICENSE("Dual BSD/GPL"); /* * Transmitter lockup simulation, normally disabled. */ static int lockup = 0; module_param(lockup, int, 0); static int timeout = SNULL_TIMEOUT; module_param(timeout, int, 0); /* * Do we run in NAPI mode? */ static int use_napi = 0; module_param(use_napi, int, 0); /* * A structure representing an in-flight packet. */ struct snull_packet { struct snull_packet *next; struct net_device *dev; int datalen; u8 data[ETH_DATA_LEN]; }; int pool_size = 8; module_param(pool_size, int, 0); /* * This structure is private to each device. It is used to
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喜欢吃一口烤肉的啵啵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值