C语言 wlan编译代码返回错误并且我需要指定ssid的口令

k2arahey  于 2023-02-21  发布在  其他
关注(0)|答案(1)|浏览(169)

我有这个代码就在这里,我正在使用mingw编译,我正在运行windows XP。当我运行输出可执行文件时,它会给我关于应用程序的错误提示。
我是否所有的设置都正确?我如何设置SSID密码让它连接?我还想知道我是否使用正确的网络接口连接(我有一个外部WiFi适配器)。
我从this link得到的代码,任何帮助都非常感谢。

//#define _WIN32_DCOM
#define _WIN32_WINNT 0x0600
#include <stdio.h>
#include <windows.h>
#include <conio.h>
#include <objbase.h>
#include <rpcsal.h>
#include <objbase.h>
#include <wlanapi.h>

int main(){
    
    PDOT11_SSID pSsid;
    strcpy(pSsid->ucSSID, "SUPERONLINE-WiFi_24811");
    pSsid->uSSIDLength = 25;
     
    HANDLE wlanHandle;
    unsigned long nv;
    WlanOpenHandle(1, NULL, &nv, &wlanHandle);

    DWORD dwResult = 0;
    WLAN_CONNECTION_PARAMETERS cp;
    memset(&cp, 0, sizeof(WLAN_CONNECTION_PARAMETERS));

    cp.wlanConnectionMode = wlan_connection_mode_profile;
    cp.strProfile = NULL;
    cp.dwFlags = 0;
    cp.pDot11Ssid = pSsid;
    cp.pDesiredBssidList = 0;
    cp.dot11BssType = dot11_BSS_type_any;

    PWLAN_INTERFACE_INFO_LIST pIfList = NULL;
    PWLAN_INTERFACE_INFO pIfInfo = NULL;

    // only use the first wifi interface
    dwResult = WlanEnumInterfaces(wlanHandle, NULL, &pIfList);
    pIfInfo = (WLAN_INTERFACE_INFO *)&pIfList->InterfaceInfo[1];

    if (dwResult == ERROR_SUCCESS)
    {
       dwResult = WlanConnect(wlanHandle, &(pIfInfo->InterfaceGuid), &cp, NULL);

       if (dwResult == ERROR_SUCCESS)
       {
           printf("Connected..\n");
          //connected = true;
       }
    }
    
    return 0;
}

这是我得到的错误:

编辑:我已经完成了下面的代码..但是我仍然不能给PDOT11_SSID结构体赋值..看起来这个结构体有UCHAR数组,但是我不能赋值..下面是link

PDOT11_SSID pSsid;
UCHAR arr[22] = { 0 };
//memset(pSsid->ucSSID, '\0', 32); //this causes the same error too

memcpy(
arr,
(unsigned char[]){ 'S','U','P','E','R','O','N','L','I','N','E','-','W','i','F','i','_','2','4','8','1','1' },
sizeof(arr)); //memcpy to arr struct works..

ULONG len = 22;
pSsid->uSSIDLength = len;
memcpy(pSsid->ucSSID, arr, sizeof(arr)); // this line fails still..

编辑:我运行了大卫提到的-Wall -Wextra -pedantic -Werror和下面的代码段:

PDOT11_SSID pSsid;
memset(pSsid->ucSSID, '\0', 32); //fails

我得到这个错误

但如果我用

PDOT11_SSID pSsid = { 0 };

PDOT11_SSID pSsid = NULL;

它编译了,但是我得到了上面我曾经得到的崩溃图像。。重要的是要注意,memset(pSsid->ucSSID, '\0', 32);也会导致崩溃。我可以通过注解掉每一行并再次编译/运行来知道这一点。
这是一个负担,因为我还没有弄清楚如何添加SSID密码与wlanapi.h,所以帮助这些将非常感谢我继续前进。
友情提示:我使用basic gcc来编译gcc.exe wlan.c -lwlanapi,如果有任何解决方案代码可以使用wlanapi连接到ssidpassword的ssid,也希望有链接。
从msdn连接的WLAN是this link..我看不到任何安全参数,如何设置要连接的SSID的密码?

p8h8hvxi

p8h8hvxi1#

这是你一直在寻找的:

#include <winsock2.h>
#include <Wlanapi.h>

#include <iphlpapi.h>

#include <stdio.h>

#pragma warning(disable:4996) /*fuss sprintf*/

#define SSID_MAX_LEN      (32)



int ConnectToTargetWifiSSID(char *pSSIDName, char *pPassword)
{
 DWORD dwVersion; 
#if(0)
 DWORD dwMajorVersion;
 dwVersion = 0 = dwMajorVersion;

    dwVersion = GetVersion();
 dwMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));

 DWORD dwClientVersion;
 dwClientVersion = ( dwMajorVersion >= 6) ?  2 : 1 ;  /*vista or latter*/

#endif 

 DWORD result;
 HANDLE iWifiHandle;

 PWLAN_INTERFACE_INFO_LIST iAvailableInterfaces;
 PWLAN_AVAILABLE_NETWORK_LIST availableNetworkList;

 GUID iInterfaceGuid;

 int isHavingProfile;
 
 char authentication[64];
 char encryption[64];
 int isOpenedAP;
 unsigned int i;
 unsigned int iii;

 WLAN_CONNECTION_PARAMETERS connParam;

 iWifiHandle = NULL;
 iAvailableInterfaces = NULL;
 availableNetworkList = NULL;

 
 /*part zero : one a wlan handle*/
 result = WlanOpenHandle(1,NULL,&dwVersion, &iWifiHandle);

 if(NULL != iWifiHandle)
 {   
   /*get wireless network card*/
   result = WlanEnumInterfaces(iWifiHandle, NULL, &iAvailableInterfaces);

   if(ERROR_SUCCESS == result && iAvailableInterfaces->dwNumberOfItems > 0)
   {
    /*chose the zeroth one*/ 
    printf("InterFaceName: %ls",iAvailableInterfaces->InterfaceInfo[0].strInterfaceDescription);
    iInterfaceGuid = iAvailableInterfaces->InterfaceInfo[0].InterfaceGuid;
   }
   else
   {
    /*no wireless card*/ 
    result = -2;
    goto Exit_ConnectToTargetSSID;
   }
   
 }
 else
 {
  result = -1;
  goto Exit_ConnectToTargetSSID;
 }/*if NULL != iWifiHandle*/



 /*part one: scan available wifi routers(SSID)*/ 
 result= WlanGetAvailableNetworkList(iWifiHandle, &iInterfaceGuid, 
  0x00000001, NULL, &availableNetworkList);
  //WLAN_AVAILABLE_NETWORK_INCLUDE_ALL_ADHOC_PROFILES, NULL, &availableNetworkList);

 if(ERROR_SUCCESS != result)
  return -3;

 isHavingProfile = FALSE;
 isOpenedAP = FALSE;
 iii = -1;

 memset(&authentication[0], 0, 64);
 memset(&encryption[0], 0, 64);

 if( 0 == availableNetworkList->dwNumberOfItems)
 {
  /*on wifi router has been found*/
  result = -4;
  goto Exit_ConnectToTargetSSID;
 }/*if 0 < wifiList->dwNumberOfItems*/ 

 for(i = 0; i < availableNetworkList->dwNumberOfItems; i++)
 {
         DWORD flag;

   printf("SSID:\t\t\t%s\nSIGNAL:\t\t\t%d\n",
                availableNetworkList->Network[i].dot11Ssid.ucSSID,
                availableNetworkList->Network[i].wlanSignalQuality);
   
  

        printf("SECURITY:\t\t");
        switch(availableNetworkList->Network[i].dot11DefaultAuthAlgorithm)
        {
                case DOT11_AUTH_ALGO_80211_OPEN:
     printf("OPEN\n");
     break;
                case DOT11_AUTH_ALGO_80211_SHARED_KEY:
                    printf("WEP\n");
                break;
                     
                case DOT11_AUTH_ALGO_WPA:
                case DOT11_AUTH_ALGO_WPA_PSK:
                case DOT11_AUTH_ALGO_WPA_NONE:
                    printf("WPA\n");
                break;

                case DOT11_AUTH_ALGO_RSNA:
                case DOT11_AUTH_ALGO_RSNA_PSK:
                    printf("WPA2\n");
                break;

                default:
                    printf("UNKNOWN\n");
                break;
        }

  printf("encryption:\t\t");

  switch(availableNetworkList->Network[i].dot11DefaultCipherAlgorithm)
  {
       case DOT11_CIPHER_ALGO_NONE:
     printf("NONE\n");
     break;

    case DOT11_CIPHER_ALGO_WEP40:
     printf("WEP40\n");
    break;

    case DOT11_CIPHER_ALGO_TKIP:
     printf("TKIP\n");
    break;

    case DOT11_CIPHER_ALGO_WEP104:
     printf("WEP104\n");
    break;

    case DOT11_CIPHER_ALGO_CCMP:
     printf("CCMP\n");
    break;

                default:
                    printf("UNKNOWN\n");
                break;
  }/*switch*/

  
  flag = availableNetworkList->Network[i].dwFlags;

  if(flag & 0x00000001)
   printf("\t NOTE : Current connecting\n");

  if(flag & 0x00000002)
   printf("\t NOTE : WLAN_AVAILABLE_NETWORK_HAS_PROFILE\n");

  if(flag & 0x00000004)
   printf("\t NOTE : WLAN_AVAILABLE_NETWORK_CONSOLE_USER_PROFILE\n");

  /*if(flag & WLAN_AVAILABLE_NETWORK_CONNECTED)
   printf("\t NOTE : Current connecting\n");

  if(flag & WLAN_AVAILABLE_NETWORK_HAS_PROFILE)
   printf("\t NOTE : WLAN_AVAILABLE_NETWORK_HAS_PROFILE\n");

  if(flag & WLAN_AVAILABLE_NETWORK_CONSOLE_USER_PROFILE)
   printf("\t NOTE : WLAN_AVAILABLE_NETWORK_CONSOLE_USER_PROFILE\n");*/
        printf("\n");
 }/*for */

 

 /*part two: save target SSID propertis*/  
 for(i = 0; i < availableNetworkList->dwNumberOfItems; i++)
 {

  if(0 == strncmp(pSSIDName, (char*)availableNetworkList->Network[i].dot11Ssid.ucSSID , SSID_MAX_LEN))
  {
   //WLAN_AVAILABLE_NETWORK_CONNECTED
   if( 0x00000001 & availableNetworkList->Network[i].dwFlags)
   {    
    printf("%s is current connecting!!\n", pSSIDName);
    result = 1;
    goto Exit_ConnectToTargetSSID;
   }/*if*/

   iii = i;

   if(0x00000002  & availableNetworkList->Network[i].dwFlags)
   //if(WLAN_AVAILABLE_NETWORK_HAS_PROFILE  & availableNetworkList->Network[i].dwFlags)
    isHavingProfile = TRUE;

   /*list the target SSID properties*/

   switch(availableNetworkList->Network[i].dot11DefaultAuthAlgorithm)
   {
   case DOT11_AUTH_ALGO_80211_OPEN:
    sprintf(&authentication[0], "OPEN");
    break;
   case DOT11_AUTH_ALGO_80211_SHARED_KEY:
      sprintf(&authentication[0], "WEP");
   break;
                 
   case DOT11_AUTH_ALGO_WPA:
   case DOT11_AUTH_ALGO_WPA_PSK:
   case DOT11_AUTH_ALGO_WPA_NONE:
    sprintf(&authentication[0], "WPAPSK");
   break;

   case DOT11_AUTH_ALGO_RSNA:
   case DOT11_AUTH_ALGO_RSNA_PSK:
    sprintf(&authentication[0], "WPA2PSK");
   break;

   default:
    sprintf(&authentication[0], "UNKNOWN");
   break;
   }/*switch dot11DefaultAuthAlgorithm*/

   switch(availableNetworkList->Network[i].dot11DefaultCipherAlgorithm)
   {
    case DOT11_CIPHER_ALGO_NONE:
     sprintf(&encryption[0], "NOEN");
    break;

    case DOT11_CIPHER_ALGO_TKIP:
     sprintf(&encryption[0], "TKIP");
    break;

    case DOT11_CIPHER_ALGO_CCMP:
     sprintf(&encryption[0], "AES");
    break;

                default:
                    sprintf(&encryption[0], "WEP");
                break;
   }/*/*switch dot11DefaultCipherAlgorithm*/
   
   break; 
  }/*if 0 == strncmp(pSSIDName, (char*)availableNetworkList->Network[i].dot11Ssid.ucSSID , SSID_MAX_LEN)*/

 }/*for i*/

 if(-1 == iii)
 {
  /*target router could not found */
  result = -5;
  goto Exit_ConnectToTargetSSID;
 }/*if */
  
 
 /*part there, set XML profile*/
 if(FALSE == isHavingProfile)
 {
  /*if current computer has never connected to target router..*/

  wchar_t profileXMLUnicode[4096];
  char buff[4096];
  
  DWORD reasonCode;

  char profileTemplateXML[] = "<?xml version=\"1.0\"?>" \
 "<WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\">"\
 " <name>%s</name>" \
 "       <SSIDConfig> " \
 "                <SSID>" \
 "                        <name>%s</name>"\
 "                </SSID>"\
 "        </SSIDConfig>"\
 "        <connectionType>ESS</connectionType>"\
 "        <connectionMode>auto</connectionMode>"\
 "        <MSM>"\
 "                <security>"\
 "                        <authEncryption>"\
 "                                <authentication>%s</authentication>"\
 "                                <encryption>%s</encryption>"\
 "                                <useOneX>false</useOneX>"\
 "                        </authEncryption>"\
 "                        <sharedKey>"\
 "                                <keyType>passPhrase</keyType>"\
 "                                <protected>false</protected>"\
 "                                <keyMaterial>%s</keyMaterial>"\
 "                        </sharedKey>"\
 "                </security>" \
 "        </MSM>" \
 "</WLANProfile>" ;

  reasonCode = 0;
    
  sprintf(buff, profileTemplateXML, availableNetworkList->Network[iii].dot11Ssid.ucSSID, 
   availableNetworkList->Network[iii].dot11Ssid.ucSSID, 
   &authentication[0], &encryption[0], pPassword);

  /*Covert ansi to unicode*/ 
  MultiByteToWideChar(CP_ACP, 0, &buff[0], -1, &profileXMLUnicode[0], 4096);

  result = WlanSetProfile(iWifiHandle, &iInterfaceGuid, 0,  &profileXMLUnicode[0], 
   NULL, TRUE, NULL, &reasonCode);

  wprintf( L"%s", profileXMLUnicode); 

  if(ERROR_SUCCESS != result)
  {
   result = -6;
   goto Exit_ConnectToTargetSSID;
  }/*if */
 }
 else
 {
  /*if current computer had connected to target router, and remember who it is ...*/
  DWORD dwFlags;
  DWORD dwGrantedAccess;
  LPWSTR xml;
  result = WlanGetProfile(iWifiHandle, &iInterfaceGuid, 
   availableNetworkList->Network[iii].strProfileName, NULL, &xml ,&dwFlags,&dwGrantedAccess);
  wprintf( L"%s", xml);  
 }/*if FASLSE == isHavingProfile*/

 
 /*part four, connect to target ssid */

 connParam.pDot11Ssid= NULL;
 connParam.strProfile= availableNetworkList->Network[iii].strProfileName;

 connParam.wlanConnectionMode = wlan_connection_mode_profile;
 connParam.pDesiredBssidList=NULL;

 connParam.dot11BssType= availableNetworkList->Network[iii].dot11BssType;
 connParam.dwFlags = 0;

 //wprintf( L"%s", &iInterfaceGuid,wifiList->Network[iii].strProfileName);
 result = WlanConnect(iWifiHandle, &iInterfaceGuid, &connParam, NULL); 
 
 if(ERROR_SUCCESS != result)
  result = -7;
  
 

Exit_ConnectToTargetSSID:

 if(NULL != availableNetworkList)
  WlanFreeMemory(availableNetworkList);

 if(NULL != iAvailableInterfaces)
  WlanFreeMemory(iAvailableInterfaces);

 if(NULL != iWifiHandle)
  WlanCloseHandle(iWifiHandle, NULL);

 return result;
}/*ConnectToTargetWifiSSID*/

int main(int argc, char *argv[])
{
 ConnectToTargetWifiSSID("SUPERONLINE-WiFi_24811", "pass123");
 return 0;
}/*main*/

相关问题