Saber 酱的抱枕

Fly me to the moon

09/28
2014
软件

承载网络及其辅助工具

很久没发东西了……
嘛最近那什么感觉还不错……
算了不扯远

承载网络是win7下引入的共享网络的方法。事实上网上教程多如牛毛,相关软件也不少
下面是我的教程(没错,就是很久以前写的那个)及其辅助工具。
注意,由于只是辅助工具,有些步骤难以代劳,所以不对这个方法有个全面的了解直接用工具只会摸不着头脑

工具可以直接查看代码……对密码位数处理的代码来自网络

http://pan.baidu.com/s/1sjv0WIL

net share.exe
大小 :240,128字节
MD5:7bbf66beef3af7c3e98d40054bb083f1
SHA1:5f76a27775a32076deae8434a6ddb7017b9f52ca
CRC32:46193bad

bat大更新,增强了代码健壮性,允许ssid和密码输入空格等字符事实上字符串处理一塌糊涂………………

c++版本正在路上。基础功能已经完成。 完成。
部分功能直接使用api调用,并且c++下字符串处理再怎么着也比批处理方便
有些仍然保留netsh,是因为不知道api/api没有公开/使用api会导致程序结束共享即关闭
API参考:https://msdn.microsoft.com/en-us/library/ms706274(v=vs.85).aspx
然而代码及其粗糙,完全不适合debug。反正也就自己随便用用……好孩子不要学!!!缩进已经爆炸了我不管了

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <wlanapi.h>
#pragma comment (lib, "wlanapi.lib")
#include <VersionHelpers.h>
#include <iostream>
#include <string>
using namespace std;
#undef max//因为下面有标示符max而windows.h里定义了一个宏max…………
int checkpassword(string pass);//检查密码长度是否合法
bool checkstring(string str);//检查字符串是否仅由ascII组成
string getssid();//输入ssid并检查长度
string getpassword();//输入密码,且调用checkpassword
DWORD getpeer();//输入最大连接数
string getname();//输入"无线网络连接"名
int main()
{
	if (!IsWindows7OrGreater())
	{
		cout << "需要Windows 7或更高的操作系统才支持承载网络" << endl;
		system("pause");
		return 0;
	}
	cout << "本程序不具备任何除错功能,当开启承载网络失败时请自行通过netsh命令开启以查找原因" << endl;
	system("pause");
	system("cls");
	DWORD dwClientVersion = 2;
	PVOID pReserved = NULL;
	DWORD dwNegotiatedVersion;
	HANDLE hClientHandle;
	PWLAN_HOSTED_NETWORK_REASON pFailReason = NULL;
	WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS wlan_settings;//API需求变量
	WlanOpenHandle(
		dwClientVersion,
		pReserved,
		&dwNegotiatedVersion,
		&hClientHandle
		); //句柄API
	int num, i;
	DWORD peer;
	string ssid, pass, name, commandtemp;
	const string	COMMAND1_1 = { "netsh wlan set autoconfig enabled=no interface=" },
		COMMAND1_2 = { "netsh wlan set autoconfig enabled=yes interface=" },
		COMMAND2_1 = { "netsh int set int name=" },
		COMMAND2_2_1 = { " admin=disabled" },
		COMMAND2_2_2 = { " admin=enable" };//需要用到的命令行的组成部分
	UCHAR upass[64];	char command[1000];
	bool temp;
	while (true)//循环以回到原界面,其他函数同理
	{
		num = -1;
		cout << "请输入操作对应的编号" << endl;<code>
		cout << "0.查看当前虚拟网卡状态" << endl << "1.查看是否支持虚拟网卡" << endl << "2.允许启用虚拟网卡功能并设定网络名称(ssid)、密码及最大连接数" << endl << "3.仅允许启用虚拟网卡功能"
			<< endl << "4.打开虚拟网卡" << endl << "5.修改网络名称(ssid)和最大连接数" << endl << "6.修改密码" << endl << "7.关闭虚拟网卡" << endl << "8.禁止启用虚拟网卡功能"
			<< endl << "9.增强网络共享的稳定性(慎用)" << endl << "10.关闭增强网络共享的稳定性功能(每次开启增强稳定性功能后必须要关闭)" << endl << "11.尝试修复无法正常启动或禁用的虚拟网卡(现有无线网络连接会断开)"
			<< endl << "12.退出" << endl;
		cin >> num;
		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(), '\n');
		switch (num)
		{
		case 0:
			{
				system("netsh wlan show hostednetwork ");
			}
			break;
		case 1:
			{
				system("netsh wlan show drivers");
			}
			break;
		case 2:
			{
				ssid = getssid();
				pass = getpassword();
				peer = getpeer();
				wlan_settings.hostedNetworkSSID.uSSIDLength = ssid.length();
				for (i = 0;i<int(ssid.length()); i++)
				{
					wlan_settings.hostedNetworkSSID.ucSSID[i] = ssid[i];
				}//将string转换为uchar,作为api参数
				wlan_settings.dwMaxNumberOfPeers = peer;
				WlanHostedNetworkSetProperty(
					hClientHandle,
					wlan_hosted_network_opcode_connection_settings,
					sizeof(WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS),
					&wlan_settings,
					pFailReason,
					NULL
					);//SSID/最大连接数API
				for (i = 0; i<int(pass.length()); i++)
				{
					upass[i] = pass[i];
				}
				upass[i] = '\0';//将string转换为uchar字符串,作为api参数
				WlanHostedNetworkSetSecondaryKey(
					hClientHandle,
					(i+1),
					upass,
					true,
					true,
					pFailReason,
					NULL
					);//密码API
				WlanHostedNetworkInitSettings(
					hClientHandle,
					pFailReason,
					NULL
					);//初始化API
				temp = true;
				WlanHostedNetworkSetProperty(
					hClientHandle,
					wlan_hosted_network_opcode_enable,
					sizeof(BOOL),
					&temp,
					pFailReason,
					NULL
					);//设置为允许API
				cout << "设置完成" << endl;
			}
			break;
		case 3:
			{
				WlanHostedNetworkInitSettings(
					hClientHandle,
					pFailReason,
					NULL
					);
				temp = true;
				WlanHostedNetworkSetProperty(
					hClientHandle,
					wlan_hosted_network_opcode_enable,
					sizeof(BOOL),
					&temp,
					pFailReason,
					NULL
					);
				cout << "设置完成" << endl;
			}
			break;
		case 4:
			{
				WlanHostedNetworkInitSettings(
					hClientHandle,
					pFailReason,
					NULL
					);
				system("netsh wlan start hostednetwork");
			}
			break;
		case 5:
			{
				system("netsh wlan stop hostednetwork");
				ssid = getssid();
				peer = getpeer();
				wlan_settings.hostedNetworkSSID.uSSIDLength = ssid.length();
				for (i = 0; i<int(ssid.length()); i++)
				{
					wlan_settings.hostedNetworkSSID.ucSSID[i] = ssid[i];
				}//将string转换为uchar,作为api参数
				wlan_settings.dwMaxNumberOfPeers = peer;
				WlanHostedNetworkSetProperty(
					hClientHandle,
					wlan_hosted_network_opcode_connection_settings,
					sizeof(WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS),
					&wlan_settings,
					pFailReason,
					NULL
					);
				WlanHostedNetworkInitSettings(
					hClientHandle,
					pFailReason,
					NULL
					);
				cout << "设置完成" << endl;
			}
			break;
		case 6:
			{
				system("netsh wlan stop hostednetwork");
				pass = getpassword();
				for (i = 0; i<int(pass.length()); i++)
				{
					upass[i] = pass[i];
				}
				upass[i] = '\0';//将string转换为uchar字符串,作为api参数
				WlanHostedNetworkSetSecondaryKey(
					hClientHandle,
					(i+1),
					upass,
					true,
					true,
					pFailReason,
					NULL
					);
				WlanHostedNetworkInitSettings(
					hClientHandle,
					pFailReason,
					NULL
					);
				cout << "设置完成" << endl;
			}
			break;
		case 7:
			{
				system("netsh wlan stop hostednetwork");
			}
			break;
		case 8:
			{
				system("netsh wlan stop hostednetwork");
				temp = false;
				WlanHostedNetworkSetProperty(
					hClientHandle,
					wlan_hosted_network_opcode_enable,
					sizeof(BOOL),
					&temp,
					pFailReason,
					NULL
					);
				cout << "设置完成" << endl;
			}
			break;
		case 9://基本上理解为关闭网卡的搜索功能让出资源给承载网络
			{
				name = getname();
				commandtemp = COMMAND1_1 + name;
				for (i = 0; i < int(commandtemp.length()); i++)
				{
					command[i] = commandtemp[i];
				}
				command[i] = '\0';//将string转换为char字符串,作为命令行命令
				system(command);
				cout << "网络共享停止后请一定记得关闭该功能(功能编号10)" << endl << "否则无法连接无线网络!" << endl;
			}
			break;
		case 10:
			{
				name = getname();
				commandtemp = COMMAND1_2 + name;
				for (i = 0; i < int(commandtemp.length()); i++)
				{
					command[i] = commandtemp[i];
				}
				command[i] = '\0';//将string转换为char字符串,作为命令行命令
				system(command);
				cout << "设置完成" << endl;
			}
			break;
		case 11://其实就是把网卡禁用再启用
			{
				system("netsh wlan stop hostednetwork");
				name = getname();
				commandtemp = COMMAND2_1 + name + COMMAND2_2_1;
				for (i = 0; i < int(commandtemp.length()); i++)
				{
					command[i] = commandtemp[i];
				}
				command[i] = '\0';//将string转换为char字符串,作为命令行命令
				system(command);
				commandtemp = COMMAND2_1 + name + COMMAND2_2_2;
				for (i = 0; i < int(commandtemp.length()); i++)
				{
					command[i] = commandtemp[i];
				}
				command[i] = '\0';//将string转换为char字符串,作为命令行命令
				system(command);
				cout << "请尝试再次启动虚拟网卡" << endl;
			}
			break;
		case 12:
			{
				WlanCloseHandle(
					hClientHandle,
					NULL
					);//关闭句柄API
				return 0;
			}
		break;
		default:
			{
				cout << "输入错误!请重新输入" << endl;
			}
		}
		system("pause");
		system("cls");
	}
}
int checkpassword(string pass)
{
	int len;
	len = pass.length();
	if (len < 8)
		return 1;
	else
		if (len > 63)
			return 2;
	return 0;
}
bool checkstring(string str)
{
	int len = str.length(), i, j;
	for (i = 0; i < len; i++)
	{
		j = int(str[i]);
		if (j <= 0 || j >= 128)
			return true;
	}
		return false;
}
string getssid()//DOT11_SSID_MAX_LENGTH是API定义的SSID最大长度
{
	string ssid;
	while (true)
	{
		cout << "请输入ssid(网络名称," << DOT11_SSID_MAX_LENGTH << "位以内)。请仅使用数字、字母及符号" << endl;
		getline(cin, ssid);
		//cin.clear();
		//cin.ignore(numeric_limits<streamsize>::max(), '\n');
		if (checkstring(ssid))
		{
			cout << "输入错误!请重新输入" << endl;
			system("pause");
			continue;
		}
		if (ssid.length()>DOT11_SSID_MAX_LENGTH)
		{
			cout << "SSID过长,请重新输入" << endl;
			system("pause");
			continue;
		}
		return ssid;
	}
}
string getpassword()
{
	string pass;
	while (true)
	{
		cout << "请输入密码(8~63位)。请仅使用数字、字母及符号" << endl;
		getline(cin, pass);
		//cin.clear();
		//cin.ignore(numeric_limits<streamsize>::max(), '\n');
		if (checkstring(pass))
		{
			cout << "输入错误!" << endl;
			system("pause");
			continue;
		}
		switch (checkpassword(pass))
		{
			case 1:
			{
				cout << "密码过短,请重新输入" << endl;
				system("pause");
				continue;
			}
			break;
			case 2:
			{
				cout << "密码过长,请重新输入" << endl;
				system("pause");
				continue;
			}
			break;
		}
		cout << "安全起见,接下来会进行清屏以消除密码" << endl;
		system("pause");
		system("cls");
		return pass;
	}
}
DWORD getpeer()
{
	DWORD peer;
	while (true)
	{
		peer = 0;
		cout << "请输入最大连接数。不要设得过小" << endl;
		cin >> peer;
		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(), '\n');
		if (peer <= 0)
		{
			cout << "输入错误!请重新输入" << endl;
			system("pause");
			continue;
		}
		return peer;
	}
}
string getname()//由于必然涉及中文,所以并没有checkstring
{
	string name;
	while (name.length() == 0)
	{
		cout << "请输入实际无线网卡对应的“无线网络连接”的名字(如无线网络连接 2),不带引号" << endl;
		getline(cin,name);
		//cin.clear();
		//cin.ignore(numeric_limits<streamsize>::max(), '\n');
	}
	name = "\"" + name + "\"";//给无线网络连接名加上引号,以防空格(用于命令行命令)
	return name;
}

承载网络及其辅助工具