wifi自动断连后连接 c++
住在酒店不知为撒,wifi总是断链,显示的是连接,可就是没网,需要手动断开然后重连,所以有了这篇文章。
#include <stdlib.h>
#include <Windows.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string name = "ChinaNet-1122";
int flag = 0;
void GetWiFi(string wifiname)
{
string filename = "wifi_info.txt";
string text;
ifstream f;
f.open(filename);
while (getline(f, text))
{
//cout << text << endl; look at ping sessions
if (text.find("TTL") != text.npos)
{
flag = 1;
//cout << text << endl;
return;
}
flag = 0;
}
}
void Work(string wifiname)
{
system("ping www.baidu.com -t -n 1 > wifi_info.txt");
GetWiFi(wifiname);
if (flag == 0)
{
keybd_event(VK_RETURN, 0, 0, 0);
keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);
cout << "enter" << endl;
system("netsh wlan disconnect");
cout << "disconnected... ..." << endl;
string bodys = "netsh wlan connect name=";
string Ss = bodys + wifiname;
char * ss = (char*)Ss.data();
system(ss);
cout << "connected... ..." << endl;
}
}
VOID CALLBACK TimerProcess(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime)
{
Work(name);
}
int main()
{
cout << "保持连接中......" << endl;
HWND hwndTimer;
MSG msg;
SetTimer(NULL, 1, 1000, TimerProcess);
int itemp;
while ((itemp = GetMessage(&msg, NULL, NULL, NULL)) && (itemp != 0) && (-1 != itemp))
{
if (msg.message == WM_TIMER)
{
//cout<<"test"<<endl;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return 0;
}
参考文章在下面:
referrence