我到处搜索...我找到了!我只是找不到任何关于如何在 C++ 上连接到 Wi-Fi 的示例。
我找到并尝试了 WlanGetAvailableNetworkList() 的例子和 WlanQueryInterface()在 MSDN 上。我还找到了一个 example我在 C# 上搜索的内容。任何人都可以告诉我一个用于 C++ 的方法吗?
编辑:我对 C++ 的互联网部分一无所知(服务器、Wifi API,甚至对 Win32 API 了解不多),只是语言的核心,我只想制作一个简单程序找到一个打开的连接并自动连接到它并在连接成功时播放声音。如果您可以向我提供一些信息和链接,我会进行研究并发布我能找到的任何解决方案。
最佳答案
给定的代码不是高级类型,即不是那么好,但它可以工作。我在 Windows 中使用了代码块。
#include <iostream>
#include<stdlib.h>
#include<fstream>
#include<string>
#include<conio.h>
using namespace std;
int main()
{
ofstream xmlFile;
ifstream xmlFile1;
string name="",pass="";
string ntyp="Wi-Fi",netType,fileTest=">test.txt",check,ntype,fil,xfileName,fileName="myWlan.xml";
char c='"',cho='2',cho1='1',c1;
netType=c+ntyp+c+fileTest;
xfileName=c+fileName+c;
int succ=0;
do
{
system("netsh wlan show networks");
cout<<" >-------------------- TO REFRESS PRESS :1 \n\n >-------------------- TO CHOOSE NETWORK PRESS : 2 \n\n > ";
cho=getch();
}while(cho!='2');
cout<<"\n Enter the desired network name-------: ";
cin>>name;
do
{
cout<<"\n Enter wifi Password------: ";
cin>>pass;
xmlFile.open(fileName.c_str());
//Writing a xml file .....................
xmlFile<<"<?xml version="<<c<<"1.0"<<c<<"?>\n";
xmlFile<<"<WLANProfile xmlns="<<c<<"http://www.microsoft.com/networking/WLAN/profile/v1"<<c<<">\n";
xmlFile<<"<name>";
xmlFile<<name;
xmlFile<<"</name>\n<SSIDConfig>\n<SSID>\n<hex>";
for(int i=0;i<name.length();i++)
xmlFile<<hex<<(int)name.at(i);
xmlFile<<"</hex>\n<name>";
xmlFile<<name;
xmlFile<<"</name>\n</SSID>\n</SSIDConfig>\n<connectionType>ESS</connectionType>\n<connectionMode>auto</connectionMode>\n<MSM>\n<security>\n<authEncryption>";
xmlFile<<"\n<authentication>WPA2PSK</authentication>\n<encryption>AES</encryption>\n<useOneX>false</useOneX>\n</authEncryption>\n<sharedKey>";
xmlFile<<"\n<keyType>passPhrase</keyType>\n<protected>false</protected>\n<keyMaterial>";
xmlFile<<pass;
xmlFile<<"</keyMaterial>\n</sharedKey>\n</security>\n</MSM>\n";
xmlFile<<"<MacRandomization xmlns="<<c<<"http://www.microsoft.com/networking/WLAN/profile/v3"<<c<<">\n";
xmlFile<<"<enableRandomization>false</enableRandomization>\n</MacRandomization>\n</WLANProfile>";
xmlFile.close();
//addd the xml file to system profile.............
system(("netsh wlan add profile filename="+xfileName).c_str());
//to let system realize what changes have been made...............
system("timeout /t 2");
//to check if connected...........
system(("netsh interface show interface name="+netType).c_str());
xmlFile1.open("test.txt");
while(!xmlFile1.eof())
{
xmlFile1>>c1;
if(c1==':')
{
for(int i=0;i<9;i++)
{
xmlFile1>>c1;
check=check+c1;
}
}
if(check=="Connected")
{
cout<<"...............................................You are connected!!.................................";
succ=1;break;
}
if(check!="Connected")check="";
}
xmlFile1.close();
if(succ==1)break;
}while(succ!=1);
return 0;
}
希望对您有所帮助....
关于c++ - 连接到 Wi-Fi C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18578514/