当前位置:   article > 正文

C++ Windows下连接WIFI_c++ 重连wifi

c++ 重连wifi

  1. #pragma once
  2. #include <windows.h>
  3. #include <wlanapi.h>
  4. #pragma comment(lib, "wlanapi.lib")
  5. //连接方式
  6. #define MODE_AUTO "auto"
  7. #define MODE_MANUAL "manual"
  8. //认证方式
  9. #define AUTH_WPAPSK "WPAPSK"
  10. #define AUTH_WPA2PSK "WPA2PSK"
  11. //加密方式
  12. #define ENCR_AES "AES"
  13. #define ENCR_TKIP "TKIP"
  14. /*WIFI信息*/
  15. struct WifiInfo
  16. {
  17. char account[64];
  18. char password[64];
  19. char mode[32];
  20. char auth[32];
  21. char encrypt[32];
  22. };
  23. class WifiMgr
  24. {
  25. public:
  26. WifiMgr();
  27. ~WifiMgr();
  28. bool connect(const char* account, const char* password, const char* mode = MODE_MANUAL,
  29. const char* auth = AUTH_WPAPSK, const char* encrypt = ENCR_AES);
  30. bool connect(const WifiInfo& info);
  31. bool reconnect();
  32. bool disconnect(bool removeProFile = true);
  33. bool connected() const;
  34. const char* getLastError() const;
  35. protected:
  36. static void _stdcall wlanNotification(PWLAN_NOTIFICATION_DATA data, void* args);
  37. private:
  38. bool m_connected = false;
  39. bool m_reconnect = false;
  40. char m_lastError[256] = { "未知错误" };
  41. HANDLE m_handle = INVALID_HANDLE_VALUE;
  42. PWLAN_INTERFACE_INFO_LIST m_infoList = nullptr;
  43. WifiInfo m_wifiInfo = { 0 };
  44. };
  1. #include "WifiMgr.h"
  2. #include <stdio.h>
  3. #include <string>
  4. WifiMgr::WifiMgr()
  5. {
  6. }
  7. WifiMgr::~WifiMgr()
  8. {
  9. if (m_connected)
  10. {
  11. disconnect();
  12. }
  13. }
  14. bool WifiMgr::connect(const char* account, const char* password, const char* mode,
  15. const char* auth, const char* encrypt)
  16. {
  17. bool result = false;
  18. do
  19. {
  20. if (!m_reconnect)
  21. {
  22. strcpy(m_wifiInfo.account, account);
  23. strcpy(m_wifiInfo.password, password);
  24. strcpy(m_wifiInfo.mode, mode);
  25. strcpy(m_wifiInfo.auth, auth);
  26. strcpy(m_wifiInfo.encrypt, encrypt);
  27. }
  28. unsigned long version = 0, value = 0;
  29. value = WlanOpenHandle(WLAN_API_VERSION, nullptr, &version, &m_handle);
  30. if (value != ERROR_SUCCESS)
  31. {
  32. sprintf(m_lastError, "打开WIFI句柄失败,错误代码:%lu", value);
  33. break;
  34. }
  35. value = WlanEnumInterfaces(m_handle, nullptr, &m_infoList);
  36. if (value != ERROR_SUCCESS)
  37. {
  38. disconnect();
  39. sprintf(m_lastError, "枚举WIFI接口失败,错误代码:%lu", value);
  40. break;
  41. }
  42. std::string character = std::string(password);
  43. for (std::string::size_type pos = 0; pos != std::string::npos; pos += strlen("&amp;"))
  44. {
  45. pos = character.find("&", pos);
  46. if (pos != std::string::npos)
  47. character.replace(pos, strlen("&"), "&amp;");
  48. else
  49. break;
  50. }
  51. char xmlBuffer[4096] = { 0 };
  52. sprintf(xmlBuffer, "<?xml version =\"1.0\"?>\
  53. <WLANProfile xmlns =\"http://www.microsoft.com/networking/WLAN/profile/v1\">\
  54. <name>%s</name>\
  55. <SSIDConfig>\
  56. <SSID>\
  57. <name>%s</name>\
  58. </SSID>\
  59. </SSIDConfig>\
  60. <connectionType>ESS</connectionType>\
  61. <connectionMode>%s</connectionMode>\
  62. <autoSwitch>false</autoSwitch>\
  63. <MSM>\
  64. <security>\
  65. <authEncryption>\
  66. <authentication>%s</authentication>\
  67. <encryption>%s</encryption>\
  68. <useOneX>false</useOneX>\
  69. </authEncryption>\
  70. <sharedKey>\
  71. <keyType>passPhrase</keyType>\
  72. <protected>false</protected>\
  73. <keyMaterial>%s</keyMaterial>\
  74. </sharedKey>\
  75. </security>\
  76. </MSM>\
  77. </WLANProfile>",
  78. account, account, mode, auth, encrypt, character.c_str());
  79. wchar_t fileData[4096] = { 0 };
  80. int length = strlen(xmlBuffer);
  81. for (int j = 0; j < length; j++)
  82. {
  83. mbtowc(&fileData[j], &xmlBuffer[j], 1);
  84. }
  85. fileData[length] = '\0';
  86. WLAN_REASON_CODE reasonCode = 0;
  87. value = WlanSetProfile(m_handle, &m_infoList->InterfaceInfo[0].InterfaceGuid,
  88. 0, fileData, nullptr, TRUE, nullptr, &reasonCode);
  89. if (value != ERROR_SUCCESS)
  90. {
  91. disconnect();
  92. sprintf(m_lastError, "设置WIFI配置文件失败,错误代码:%lu", value);
  93. break;
  94. }
  95. wchar_t wideSsid[512] = { 0 };
  96. if (!MultiByteToWideChar(CP_ACP, 0, account, strlen(account) + 1,
  97. wideSsid, sizeof(wideSsid) / sizeof(wideSsid[0])))
  98. {
  99. disconnect();
  100. sprintf(m_lastError, "多字符转宽字符失败,错误代码:%lu", GetLastError());
  101. break;
  102. }
  103. DOT11_SSID dot11Ssid = { 0 };
  104. memcpy(dot11Ssid.ucSSID, account, strlen(account) + 1);
  105. dot11Ssid.uSSIDLength = strlen(account);
  106. WLAN_CONNECTION_PARAMETERS connectionParameters;
  107. memset(&connectionParameters, 0, sizeof(connectionParameters));
  108. connectionParameters.pDot11Ssid = &dot11Ssid;
  109. connectionParameters.wlanConnectionMode = wlan_connection_mode_profile;
  110. connectionParameters.strProfile = wideSsid; /* clear 可以填nullptr */
  111. connectionParameters.dot11BssType = dot11_BSS_type_any;//dot11_BSS_type_independent; /* msdn说可以dot11_BSS_type_any,实测不行 */
  112. connectionParameters.pDesiredBssidList = nullptr;
  113. connectionParameters.dwFlags = 0; /* WLAN_CONNECTION_ADHOC_JOIN_ONLY */
  114. Sleep(300);
  115. value = WlanConnect(m_handle, &m_infoList->InterfaceInfo[0].InterfaceGuid,
  116. &connectionParameters, nullptr);
  117. if (value != ERROR_SUCCESS)
  118. {
  119. disconnect();
  120. sprintf(m_lastError, "连接WIFI失败,错误代码:%lu", value);
  121. break;
  122. }
  123. //value = WlanRegisterNotification(m_handle, WLAN_NOTIFICATION_SOURCE_ALL, false,
  124. // wlanNotification, this, nullptr, nullptr);
  125. //if (value != ERROR_SUCCESS)
  126. //{
  127. // sprintf(m_lastError, "注册通知失败,错误代码:%lu", value);
  128. // break;
  129. //}
  130. m_connected = true;
  131. result = true;
  132. } while (false);
  133. return result;
  134. }
  135. bool WifiMgr::connect(const WifiInfo& info)
  136. {
  137. return connect(info.account, info.password, info.mode, info.auth, info.encrypt);
  138. }
  139. bool WifiMgr::reconnect()
  140. {
  141. bool result = false;
  142. m_reconnect = true;
  143. result = disconnect() && connect(m_wifiInfo);
  144. m_reconnect = false;
  145. return result;
  146. }
  147. bool WifiMgr::disconnect(bool removeProFile)
  148. {
  149. bool result = true;
  150. do
  151. {
  152. unsigned long value = 0;
  153. if (removeProFile)
  154. {
  155. wchar_t profile[256] = { 0 };
  156. if (MultiByteToWideChar(CP_ACP, 0, m_wifiInfo.account, strlen(m_wifiInfo.account) + 1,
  157. profile, sizeof(profile) / sizeof(profile[0])))
  158. {
  159. if (m_handle != INVALID_HANDLE_VALUE && m_infoList)
  160. {
  161. WlanDeleteProfile(m_handle, &(m_infoList->InterfaceInfo->InterfaceGuid), profile, nullptr);
  162. }
  163. }
  164. }
  165. if (m_handle != INVALID_HANDLE_VALUE && m_infoList)
  166. {
  167. WlanDisconnect(m_handle, &(m_infoList->InterfaceInfo->InterfaceGuid), nullptr);
  168. }
  169. if (m_handle != INVALID_HANDLE_VALUE)
  170. {
  171. WlanCloseHandle(m_handle, nullptr);
  172. m_handle = INVALID_HANDLE_VALUE;
  173. }
  174. if (m_infoList)
  175. {
  176. WlanFreeMemory(m_infoList);
  177. m_infoList = nullptr;
  178. }
  179. m_connected = false;
  180. } while (false);
  181. return result;
  182. }
  183. bool WifiMgr::connected() const
  184. {
  185. return m_connected;
  186. }
  187. const char* WifiMgr::getLastError() const
  188. {
  189. return m_lastError;
  190. }
  191. void WifiMgr::wlanNotification(PWLAN_NOTIFICATION_DATA data, void* args)
  192. {
  193. WifiMgr* wifi = static_cast<WifiMgr*>(args);
  194. if (!wifi)
  195. return;
  196. unsigned long source = data->NotificationSource;
  197. unsigned long code = data->NotificationCode;
  198. unsigned long size = data->dwDataSize;
  199. const char* action = "unknown";
  200. if (source == WLAN_NOTIFICATION_SOURCE_ACM)
  201. {
  202. switch (source)
  203. {
  204. case wlan_notification_acm_start:
  205. action = "acm_start";
  206. break;
  207. case wlan_notification_acm_autoconf_enabled:
  208. action = "acm_autoconf_enabled";
  209. break;
  210. case wlan_notification_acm_autoconf_disabled:
  211. action = "acm_autoconf_disabled";
  212. break;
  213. case wlan_notification_acm_background_scan_enabled:
  214. action = "acm_background_scan_enabled";
  215. break;
  216. case wlan_notification_acm_background_scan_disabled:
  217. action = "acm_background_scan_disabled";
  218. break;
  219. case wlan_notification_acm_bss_type_change:
  220. action = "acm_bss_type_change";
  221. break;
  222. case wlan_notification_acm_power_setting_change:
  223. action = "acm_power_setting_change";
  224. break;
  225. case wlan_notification_acm_scan_complete:
  226. action = "acm_scan_complete";
  227. break;
  228. case wlan_notification_acm_scan_fail:
  229. action = "acm_scan_fail";
  230. break;
  231. case wlan_notification_acm_connection_start:
  232. action = "acm_connection_start";
  233. break;
  234. case wlan_notification_acm_connection_complete:
  235. action = "acm_connection_complete";
  236. break;
  237. case wlan_notification_acm_connection_attempt_fail:
  238. action = "acm_connection_attempt_fail";
  239. break;
  240. case wlan_notification_acm_filter_list_change:
  241. action = "acm_filter_list_change";
  242. break;
  243. case wlan_notification_acm_interface_arrival:
  244. action = "acm_interface_removal";
  245. break;
  246. case wlan_notification_acm_interface_removal:
  247. action = "acm_interface_removal";
  248. break;
  249. case wlan_notification_acm_profile_change:
  250. action = "acm_profile_change";
  251. break;
  252. case wlan_notification_acm_profile_name_change:
  253. action = "acm_profile_name_change";
  254. break;
  255. case wlan_notification_acm_profiles_exhausted:
  256. action = "acm_profiles_exhausted";
  257. break;
  258. case wlan_notification_acm_network_not_available:
  259. action = "acm_network_not_available";
  260. break;
  261. case wlan_notification_acm_network_available:
  262. action = "acm_network_available";
  263. break;
  264. case wlan_notification_acm_disconnecting:
  265. action = "acm_disconnecting";
  266. break;
  267. case wlan_notification_acm_disconnected:
  268. action = "acm_disconnected";
  269. break;
  270. case wlan_notification_acm_adhoc_network_state_change:
  271. action = "acm_adhoc_network_state_change";
  272. break;
  273. case wlan_notification_acm_end:
  274. action = "acm_end";
  275. break;
  276. default:break;
  277. }
  278. }
  279. else if (source == WLAN_NOTIFICATION_SOURCE_MSM)
  280. {
  281. switch (code)
  282. {
  283. case wlan_notification_msm_start:
  284. action = "msm_start";
  285. break;
  286. case wlan_notification_msm_associating:
  287. {
  288. auto ptr = ((PWLAN_MSM_NOTIFICATION_DATA)data->pData);
  289. }
  290. action = "msm_associating";
  291. break;
  292. case wlan_notification_msm_associated:
  293. {
  294. auto ptr = ((PWLAN_MSM_NOTIFICATION_DATA)data->pData);
  295. }
  296. action = "msm_associated";
  297. break;
  298. case wlan_notification_msm_authenticating:
  299. {
  300. auto ptr = ((PWLAN_MSM_NOTIFICATION_DATA)data->pData);
  301. }
  302. action = "msm_authenticating";
  303. break;
  304. case wlan_notification_msm_connected:
  305. action = "msm_connected";
  306. break;
  307. case wlan_notification_msm_roaming_start:
  308. action = "msm_roaming_start";
  309. break;
  310. case wlan_notification_msm_roaming_end:
  311. action = "msm_roaming_end";
  312. break;
  313. case wlan_notification_msm_radio_state_change:
  314. action = "msm_radio_state_change";
  315. break;
  316. case wlan_notification_msm_signal_quality_change:
  317. action = "msm_signal_quality_change";
  318. printf("信号质量:%lu\n", *(unsigned long*)data->pData);
  319. break;
  320. case wlan_notification_msm_disassociating:
  321. action = "msm_disassociating";
  322. break;
  323. case wlan_notification_msm_disconnected:
  324. {
  325. auto ptr = ((PWLAN_MSM_NOTIFICATION_DATA)data->pData);
  326. }
  327. action = "msm_disconnected";
  328. break;
  329. case wlan_notification_msm_peer_join:
  330. action = "msm_peer_join";
  331. break;
  332. case wlan_notification_msm_peer_leave:
  333. action = "msm_peer_leave";
  334. break;
  335. case wlan_notification_msm_adapter_removal:
  336. action = "msm_adapter_removal";
  337. break;
  338. case wlan_notification_msm_adapter_operation_mode_change:
  339. action = "msm_adapter_operation_mode_change";
  340. break;
  341. case wlan_notification_msm_end:
  342. action = "msm_end";
  343. break;
  344. default:break;
  345. }
  346. }
  347. printf("source %d,code %d,size %d,action %s\n", source, code, size, action);
  348. return;
  349. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/252322
推荐阅读
相关标签
  

闽ICP备14008679号