Win7启动承载网络的方式(代码)

下面的代码是从网上下载的,具体从哪里下载的已经找不到链接了。

源文件:

// WIFIMAIN.cpp : 实现文件
//

//#include "stdafx.h"
#include "pch.h"
#include "WifiShareTool.h"
#include "WIFIMAIN.h"


// WIFIMAIN

IMPLEMENT_DYNAMIC(WIFIMAIN, CWnd)

WIFIMAIN::WIFIMAIN()
: allow(false)
, start(false)
{
	Init();
}

WIFIMAIN::~WIFIMAIN()
{
	StopHostedNetWork();
	WlanCloseHandle(hClient, NULL);
}


BEGIN_MESSAGE_MAP(WIFIMAIN, CWnd)
END_MESSAGE_MAP()



// WIFIMAIN 消息处理程序






int WIFIMAIN::Init(void)
{
	DWORD dwCurVersion = 0;
    DWORD dwResult = 0;
    
    dwResult = WlanOpenHandle(WLAN_API_VERSION, NULL, &dwCurVersion, &hClient);
	if( ERROR_SUCCESS != dwResult )
	{
		return -1;
	}


	BOOL bIsAllow = true;
    WLAN_HOSTED_NETWORK_REASON dwFailedReason;
	dwResult = WlanHostedNetworkSetProperty(hClient,
											wlan_hosted_network_opcode_enable, 
											sizeof(BOOL),
											&bIsAllow,
											&dwFailedReason,
											NULL);
	if( ERROR_SUCCESS != dwResult )
	{
		return -2;
	}
	
	return 0;
}


int WIFIMAIN::AllowHostedNetWork(void)
{
	PWLAN_HOSTED_NETWORK_REASON pFailReason = NULL;
	
	DWORD dwResult= 0;
	dwResult=WlanHostedNetworkForceStart(hClient,pFailReason,NULL);
	if( dwResult != ERROR_SUCCESS )
	{
		return -1;
	}
	allow=true;
	return 0;
}


int WIFIMAIN::DisallowHostedNetWork(void)
{
	PWLAN_HOSTED_NETWORK_REASON pFailReason = NULL;
	DWORD dwResult= 0;
	dwResult=WlanHostedNetworkForceStop(hClient,pFailReason,NULL);
	if( dwResult != ERROR_SUCCESS )
	{
		return -1;
	}
	allow=false;
	return 0;
}


int WIFIMAIN::StartHostedNetWork(void)
{
	PWLAN_HOSTED_NETWORK_REASON pFailReason = NULL;
	DWORD dwResult= 0;
	dwResult=WlanHostedNetworkStartUsing(hClient,pFailReason,NULL);
	if( dwResult != ERROR_SUCCESS )
	{
		return -1;
	}
	start=true;
	return 0;
}


int WIFIMAIN::StopHostedNetWork(void)
{
	PWLAN_HOSTED_NETWORK_REASON pFailReason = NULL;
	DWORD dwResult= 0;
	dwResult=WlanHostedNetworkStopUsing(hClient,pFailReason,NULL);
	if( dwResult != ERROR_SUCCESS )
	{
		return -1;
	}
	start=false;
	return 0;
}



bool WIFIMAIN::isStart(void)
{
	return start;
}


bool WIFIMAIN::isAllow(void)
{
	return allow;
}


int WIFIMAIN::Resume(void)
{
	DWORD dwResult = 0;
    
	BOOL bIsAllow = false;
    WLAN_HOSTED_NETWORK_REASON dwFailedReason;
	dwResult = WlanHostedNetworkSetProperty(hClient,
											wlan_hosted_network_opcode_enable, 
											sizeof(BOOL),
											&bIsAllow,
											&dwFailedReason,
											NULL);
	if( ERROR_SUCCESS != dwResult )
	{
		return -2;
	}
	
	return 0;
}


int WIFIMAIN::getpeernumber(void)
{
	PWLAN_HOSTED_NETWORK_STATUS ppWlanHostedNetworkStatus=NULL;
	int retval = WlanHostedNetworkQueryStatus(hClient,&ppWlanHostedNetworkStatus,NULL);
	if(  retval != ERROR_SUCCESS ){
		return -1;
	}
	return ppWlanHostedNetworkStatus->dwNumberOfPeers;
}

BOOL WIFIMAIN::getNetworkStatus(void)
{
	PWLAN_HOSTED_NETWORK_STATUS ppWlanHostedNetworkStatus = NULL;
	int retval = WlanHostedNetworkQueryStatus(hClient, &ppWlanHostedNetworkStatus, NULL);
	if (retval != ERROR_SUCCESS) {
		return FALSE;
	}
	else {
		if (wlan_hosted_network_active == ppWlanHostedNetworkStatus->HostedNetworkState) {
			return TRUE;
		}
	}
	return FALSE;
}

int WIFIMAIN::SetKEY(CString key)
{
	char chkey[64];
	int index;
	for(index=0; index

头文件:

#pragma once
#include 
#pragma comment(lib,"wlanapi.lib")

// WIFIMAIN

class WIFIMAIN : public CWnd
{
	DECLARE_DYNAMIC(WIFIMAIN)

public:
	WIFIMAIN();
	virtual ~WIFIMAIN();

protected:
	DECLARE_MESSAGE_MAP()
private:
	bool allow;
	bool start;
	HANDLE hClient;
public:
	int Init(void);
	int AllowHostedNetWork(void);
	int DisallowHostedNetWork(void);
	int StartHostedNetWork(void);
	int StopHostedNetWork(void);
	bool isStart(void);
	bool isAllow(void);
	int Resume(void);
	int getpeernumber(void);
	BOOL WIFIMAIN::getNetworkStatus(void);
	int SetKEY(CString key);
	int SetSSID(CString ssidname);
};

 

你可能感兴趣的:(Windows开发)