当前位置:   article > 正文

C++连接Wlan/Wifi网络设备_c++网络连接

c++网络连接

本单元只介绍如何链接wifi设备,对信号强度不做涉及,另外链接指定wifi之前应该先判断wifi是否已经链接,如果已经链接就没必要再链接了,代码中采用的是没做链接判断,先直接断开然后重新进行链接,代码如下:

  1. #include <wlanapi.h>
  2. #pragma comment(lib, "wlanapi.lib")
  3. BOOL connectWlan(CString Name, CString SSID, CString Password)
  4. {
  5. BOOL bRet = FALSE;
  6. DWORD dwError = ERROR_SUCCESS;
  7. HANDLE hClient = NULL;
  8. DWORD dwServiceVersion;
  9. PWLAN_INTERFACE_INFO_LIST pIntfList = NULL;
  10. // 打开WLAN服务
  11. if ((dwError = WlanOpenHandle(
  12. WLAN_API_VERSION,
  13. NULL,
  14. &dwServiceVersion,
  15. &hClient
  16. )) == ERROR_SUCCESS) {
  17. //枚举处当前系统安装的所有无线网卡的接口信息
  18. if ((dwError = WlanEnumInterfaces(
  19. hClient,
  20. NULL,
  21. &pIntfList
  22. )) == ERROR_SUCCESS) {
  23. for (int i = 0; i < pIntfList->dwNumberOfItems; i++)
  24. {
  25. /*获取网卡信息*/
  26. PWLAN_INTERFACE_INFO pInterface = (PWLAN_INTERFACE_INFO)&pIntfList->InterfaceInfo[i];
  27. /*获得网络信息列表*/
  28. PWLAN_AVAILABLE_NETWORK_LIST pNetList = NULL;
  29. DWORD dwResult = WlanGetAvailableNetworkList(hClient
  30. , &pInterface->InterfaceGuidGUID
  31. , 0x00
  32. , NULL
  33. , &pNetList
  34. );
  35. if (ERROR_SUCCESS != dwResult)
  36. {
  37. if (pNetList != NULL)
  38. {
  39. WlanFreeMemory(pNetList);
  40. pNetList = NULL;
  41. }
  42. break;
  43. }
  44. /*连接网络*/
  45. for (int j = 0; j < pNetList->dwNumberOfItems; j++)
  46. {
  47. /*获取网络信息*/
  48. PWLAN_AVAILABLE_NETWORK pNet = (PWLAN_AVAILABLE_NETWORK)&pNetList->Network[j];
  49. if (strcmp(SSID, (char*)pNet->dot11Ssid.ucSSID))
  50. continue;
  51. if (pInterface->isState == wlan_interface_state_connected) {
  52. // 判断wifi是否所需
  53. PWLAN_CONNECTION_ATTRIBUTES pConnectInfo = NULL;
  54. DWORD dwSize = sizeof(WLAN_CONNECTION_ATTRIBUTES);
  55. WLAN_O
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/252323
推荐阅读
相关标签
  

闽ICP备14008679号