2023-02-22

wlan compiled code returns error and i need to specify the password for the ssid

I have this code right here I am using mingw to compile and I am running windows XP. When I run the output executable it gives me error prompt about the application..

Do I set everything correctly ? How do I set the SSID password for it to connect ? Also I want to know if I am using the correct network interface to connect (I have an external wifi adapter)..

I got the code from this link any help is very much appreciated..

//#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;
}

This is the error I get:

enter image description here

Edit: I have made it this far to the code below.. But still I can not assign the value to PDOT11_SSID struct.. It seems the struct has UCHAR array yet I can't assign.. Here is the 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..

Edit: I ran with -Wall -Wextra -pedantic -Werror Like David mentioned and with the code segment below:

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

I got this error

enter image description here

yet if I initialized with

PDOT11_SSID pSsid = { 0 };

or

PDOT11_SSID pSsid = NULL;

It compiled but I got the crash image above I used to get.. Important to note that memset(pSsid->ucSSID, '\0', 32); causes the crash as well. I can know this by commenting out each line and compiling/running again.

This is a burden since I am yet to figure out how to add SSID password with wlanapi.h so help for these will be much appreciated for me to move on forward.

Fyi: I use basic gcc to compile gcc.exe wlan.c -lwlanapi. If there is any working solution code to connect to an ssid with the ssid and password using wlanapi, a link to that is also appreciated.

Wlan connect from msdn is this link.. I can't see any security parameters, how do I set the password of the SSID I want to connect to ?



No comments:

Post a Comment