Ir al contenido


Foto

Backup de conexiones wifi


  • Por favor identifícate para responder
2 respuestas en este tema

#1 escafandra

escafandra

    Advanced Member

  • Administrador
  • 4.107 mensajes
  • LocationMadrid - España

Escrito 30 septiembre 2015 - 11:45

Este código surge de una pregunta hecha en el foro hermano Club Builder. La cuestión era como desconectar un PC de su conexión wifi y volver poder dejar las conexiones como estaban.
 
Dado que un PC puede tener varios interfaces Wifi y cada uno conectado a una red diferente, se trata de guardar el estado y conexión de cada para poder volver a realizar la conexión de cada interface con su red más tarde.
 
Que quede claro que este código no sirve para conectarse a una red a la que previamente no estabamos conectados, para ello os redirijo al tema Jugando con las conexiones Wifi
 
Aunque la pregunta se centraba en C++ Builder, me pareció interesante implementarlo también para delphi y compartir con vosotros este truco:
 
Este es el código para C/C++

cpp
  1. // Desconecta o conecta las redes Wifi guardandolas al desconectar
  2. // Con hasta 10 adaptadores wifi en el PC
  3. #define MAX_Wifi_Interfaces 10
  4. BOOL Wifi_Connect_DisConnect2(BOOL connect)
  5. {
  6. struct WLANConnected{
  7. WCHAR strProfileName[256];
  8. GUID InterfaceGuid;
  9. };
  10.  
  11. static WLANConnected Interfaces[MAX_Wifi_Interfaces] = {0};
  12.  
  13. HANDLE hClient;
  14. DWORD dwVersion;
  15. PWLAN_INTERFACE_INFO_LIST pInterfaceInfoList;
  16. PGUID pInterfaceGuid;
  17. PWLAN_CONNECTION_PARAMETERS pWLCP;
  18. PWLAN_AVAILABLE_NETWORK_LIST pNetworkList;
  19. BOOL Result = false;
  20.  
  21. if(ERROR_SUCCESS == WlanOpenHandle(1, NULL, &dwVersion, &hClient)){
  22. if(ERROR_SUCCESS == WlanEnumInterfaces(hClient, NULL, &pInterfaceInfoList)){
  23. for(int i=0; i < pInterfaceInfoList->dwNumberOfItems && i < MAX_Wifi_Interfaces; i++){
  24. pInterfaceGuid = &pInterfaceInfoList->InterfaceInfo[pInterfaceInfoList->dwIndex].InterfaceGuid;
  25. if(connect){
  26. //if(pInterfaceInfoList->InterfaceInfo[pInterfaceInfoList->dwIndex].isState == wlan_interface_state_disconnected)
  27. for(int j=0; j<MAX_Wifi_Interfaces; j++){
  28. if(Interfaces[j].InterfaceGuid == *pInterfaceGuid){
  29. pWLCP = (PWLAN_CONNECTION_PARAMETERS)WlanAllocateMemory(sizeof(WLAN_CONNECTION_PARAMETERS));
  30. pWLCP->strProfile = Interfaces[j].strProfileName;
  31. pWLCP->wlanConnectionMode = 0; // wlan_connection_mode_profile
  32. pWLCP->pDot11Ssid = NULL; //pDot11_DDSI;
  33. pWLCP->pDesiredBssidList = NULL;
  34. pWLCP->dot11BssType = 1; // dot11_BSS_type_infrastructure;
  35. pWLCP->dwFlags = 0;//0xF;
  36. Result |= (ERROR_SUCCESS == WlanConnect(hClient, pInterfaceGuid, pWLCP, NULL));
  37. WlanFreeMemory(pWLCP);
  38. }
  39. }
  40. }
  41. else if(ERROR_SUCCESS == WlanGetAvailableNetworkList(hClient, pInterfaceGuid, 1, NULL, &pNetworkList)){
  42. for(int j=0; j < pNetworkList->dwNumberOfItems; j++){
  43. if(pNetworkList->Network[j].dwFlags & WLAN_AVAILABLE_NETWORK_CONNECTED){
  44. lstrcpynW(Interfaces[i].strProfileName, pNetworkList->Network[j].strProfileName, 256);
  45. Interfaces[i].InterfaceGuid = *pInterfaceGuid;
  46. Result |= (ERROR_SUCCESS == WlanDisconnect(hClient, pInterfaceGuid, NULL));
  47. }
  48. }
  49. }
  50. }
  51. WlanFreeMemory(pInterfaceInfoList);
  52. }
  53. WlanCloseHandle(hClient, NULL);
  54. }
  55. return Result;
  56. }


Y esta sería la versión para delphi:

delphi
  1. // Desconecta o conecta las redes Wifi guardandolas al desconectar
  2. // Con hasta 10 adaptadores wifi en el PC
  3.  
  4. type
  5. TWLANConnected = record
  6. strProfileName: array [0..255] of WCHAR;
  7. InterfaceGuid: TGUID;
  8. end;
  9. const
  10. MAX_Wifi_Interfaces = 10;
  11. var
  12. Interfaces: array [0..MAX_Wifi_Interfaces] of TWLANConnected;
  13.  
  14. function Wifi_Connect_DisConnect2(connect: BOOL): BOOL;
  15. var
  16. hClient: THandle;
  17. dwVersion: DWORD;
  18. pInterfaceInfoList: PWLAN_INTERFACE_INFO_LIST;
  19. pInterfaceGuid: PGUID;
  20. pWLCP: PWLAN_CONNECTION_PARAMETERS;
  21. pNetworkList: PWLAN_AVAILABLE_NETWORK_LIST;
  22. i, j: integer;
  23. begin
  24. Result:= false;
  25. if ERROR_SUCCESS = WlanOpenHandle(1, nil, @dwVersion, @hClient) then
  26. begin
  27. if ERROR_SUCCESS = WlanEnumInterfaces(hClient, nil, @pInterfaceInfoList) then
  28. begin
  29. for i:= 0 to pInterfaceInfoList^.dwNumberOfItems - 1 do
  30. begin
  31. if i >= MAX_Wifi_Interfaces then break;
  32. pInterfaceGuid:= @pInterfaceInfoList^.InterfaceInfo[pInterfaceInfoList^.dwIndex].InterfaceGuid;
  33. if connect then
  34. begin
  35. for j:=0 to MAX_Wifi_Interfaces-1 do
  36. begin
  37. if IsEqualGUID(Interfaces[j].InterfaceGuid, pInterfaceGuid^) then
  38. begin
  39. pWLCP:= WlanAllocateMemory(sizeof(TWLAN_CONNECTION_PARAMETERS));
  40. pWLCP.strProfile:= Interfaces[j].strProfileName;
  41. pWLCP.wlanConnectionMode:= 0; // wlan_connection_mode_profile
  42. pWLCP.pDot11Ssid:= nil; //pDot11_DDSI;
  43. pWLCP.pDesiredBssidList:= nil;
  44. pWLCP.dot11BssType:= 1; // dot11_BSS_type_infrastructure;
  45. pWLCP.dwFlags:= 0;
  46. Result:= Result or (ERROR_SUCCESS = WlanConnect(hClient, pInterfaceGuid, pWLCP, nil));
  47. WlanFreeMemory(pWLCP);
  48. end;
  49. end;
  50. end
  51. else if ERROR_SUCCESS = WlanGetAvailableNetworkList(hClient, pInterfaceGuid, 1, nil, pNetworkList) then
  52. begin
  53. for j:=0 to pNetworkList^.dwNumberOfItems-1 do
  54. begin
  55. if (pNetworkList^.Network[j].dwFlags and WLAN_AVAILABLE_NETWORK_CONNECTED) <> 0 then
  56. begin
  57. lstrcpynW(Interfaces[i].strProfileName, pNetworkList^.Network[j].strProfileName, 256);
  58. Interfaces[i].InterfaceGuid:= pInterfaceGuid^;
  59. Result:= Result or (ERROR_SUCCESS = WlanDisconnect(hClient, pInterfaceGuid, nil));
  60. end;
  61. end;
  62. end;
  63. end;
  64. WlanFreeMemory(pInterfaceInfoList);
  65. end;
  66. WlanCloseHandle(hClient, nil);
  67. end;
  68. end;


El uso es bien simple:

delphi
  1. Wifi_Connect_DisConnect2(true); // Para conectar
  2. Wifi_Connect_DisConnect2(false); // Para desconectar


Subo un proyecto para delphi con las definiciones estrictamente necesarias, en este proyecto, para usar Wlanapi.
 
 
Saludos.

Archivos adjuntos


  • 4

#2 Agustin Ortu

Agustin Ortu

    Advanced Member

  • Moderadores
  • PipPipPip
  • 831 mensajes
  • LocationArgentina

Escrito 30 septiembre 2015 - 03:00

Muy bueno como siempre

Soy tan pendejo que puse al boton de "no me gusta" aunque claramente no era mi intencion
  • 0

#3 escafandra

escafandra

    Advanced Member

  • Administrador
  • 4.107 mensajes
  • LocationMadrid - España

Escrito 30 septiembre 2015 - 03:24

Soy tan pendejo que puse al boton de "no me gusta" aunque claramente no era mi intencion

 

Pues este agravio sólo se soluciona con unas cervezas. 15.gif

 

 

Saludos.


  • 0




IP.Board spam blocked by CleanTalk.