Jump to content
Sign in to follow this  
AutID

wlan api - WlanConnect

Recommended Posts

AutID

Hello everyone,

I am trying to write some wlan api functions for a self use.

Already succeed writing some functions but WlanConnect seems to making it hard.

I can't understand the parameters of the function. Shouldn't wireless autoconnect to the profile name I give since it is already saved on my system. It is actually my Ethernet connection and I am just trying to make some wireless functions for my laptop.

This is what I made but it doesn't seem to work properly :/

Global $hDLL = DllOpen("wlanapi.dll"), $dGUID = DllStructCreate("byte[16]")
Local $hClientHandle, $aGUID, $pGUID, $avNetworks, $connect, $closehandle, $close

$hClientHandle = _GetClientHandle()
$aGUID = _GetInterfaces($hClientHandle)
$pGUID = _SelectInterface($aGUID[0])
$connect = _Connect($hClientHandle, $pGUID, "testlan", 5)
If @error Then ConsoleWrite(@error & @CRLF & @extended & @CRLF)

$closehandle = DllCall($hDLL, "dword", "WlanCloseHandle", "ptr", $hClientHandle, "ptr", 0)
$close = DllClose($hDLL)

Func _GetClientHandle()
Local $aResult = DllCall($hDLL, "dword", "WlanOpenHandle", "dword", 2, "ptr", 0, "dword*", 0, "hwnd*", 0)
If @error Or $aResult[0] Then
ConsoleWrite("WlanOpenHandle Failed" & @CRLF & "ERROR: " & @error & @CRLF & "EXTENDED: " & @extended & @CRLF)
Exit
EndIf

Return $aResult[4]
EndFunc

Func _Connect($hClientHandle, $pGUID, $SSID, $Time)
Local $strProfile, $ConnectionParameters, $Timer

$CallbackConnectCount = 0
$CallbackDisconnectCount = 0

$strProfile = DllStructCreate("wchar strProfile[256]")
DllStructSetData($strProfile, "strProfile", $SSID)

$ConnectionParameters = DllStructCreate("dword WLANCONNECTIONMODE; ptr strProfile; ptr PDOT11SSID; ptr PDOT11BSSIDLIST; dword DOT11BSSTYPE; dword dwFlags")
DllStructSetData($ConnectionParameters, "strProfile", DllStructGetPtr($strProfile))
DllStructSetData($ConnectionParameters, "DOT11BSSTYPE", 1)

$Timer = TimerInit()

$aResult = DllCall($hDLL, "dword", "WlanConnect", "hwnd", $hClientHandle, "ptr", $pGUID, "ptr", DllStructGetPtr($ConnectionParameters), "ptr", 0)
If @error Then Return SetError(4, 0, 0)
If $aResult[0] Then SetError(1, $aResult[0], @error)

While 1
Select
Case TimerDiff($Timer) > 1000 * $Time
$Time = "Timeout"
ExitLoop
Case $CallbackConnectCount == 3
ExitLoop
Case $CallbackDisconnectCount
If $CallbackConnectCount Then ExitLoop
$CallbackDisconnectCount = 0
EndSelect
Sleep(500)
WEnd

If $Time = "Timeout" Then SetError(5, 0, 0)
If $CallbackDisconnectCount Then Return False
EndFunc

Func _GetInterfaces($hClientHandle)
Local $aResult, $pInterfaceList, $tInterfaceList, $iInterfaceCount, $pInterface, $tInterface

$aResult = DllCall($hDLL, "dword", "WlanEnumInterfaces", "hwnd", $hClientHandle, "ptr", 0, "ptr*", 0)
If @error Or $aResult[0] Then
ConsoleWrite("WlanEnumInterfaces Failed" & @CRLF & "ERROR: " & @error & @CRLF & "EXTENDED: " & @extended & @CRLF)
Exit
EndIf

$pInterfaceList = $aResult[3]


$tInterfaceList = DllStructCreate("dword", $pInterfaceList)
$iInterfaceCount = DllStructGetData($tInterfaceList, 1)

If Not $iInterfaceCount Then
ConsoleWrite("No InterFaces were found. Check Troubleshooting for solutions" & @CRLF)
Exit
EndIf

Local $abGUIDs[$iInterfaceCount]

For $i = 0 To $iInterfaceCount - 1
$pInterface = Ptr(Number($pInterfaceList) + ($i * 532 + 8)) 
$tInterface = DllStructCreate("byte GUID[16]; wchar descr[256]; int State", $pInterface)
$abGUIDs[$i] = DllStructGetData($tInterface, "GUID") 
ConsoleWrite("Found Interface: " & DllStructGetData($tInterface, "descr") & @CRLF)
Next

DllCall($hDLL, "dword", "WlanFreeMemory", "ptr", $pInterfaceList)

Return $abGUIDs
EndFunc

Func _SelectInterface($bGUID)
DllStructSetData($dGUID, 1, $bGUID)
Return DllStructGetPtr($dGUID)
EndFunc
Any ideas? Do I need all WLAN_CONNECTION_PARAMETERS??? Edited by AutID

Share this post


Link to post
Share on other sites
JohnOne

perhaps this comment from msdn has something to do with it.

Note that the guids which populate the array returned by WlanEnumInterfaces are NOT the guids assigned to adapters by the IP helper api, nor do they match the guids used to identify network adapters in the registry

 


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
AutID

perhaps this comment from msdn has something to do with it.

No I think it's the WLAN_CONNECTION_PARAMETERS. I must get the profile parameters and parse them to the connection. Normally it should work only by giving the name of the profile.

On the other side, the CMD connection on a wifi needs SSID,NAME,and wireless network declarations. So I guess I have to get the connection parameters set them correctly and then call the WlanConnect

Share this post


Link to post
Share on other sites
MattyD

Oh cool,

I had some time to procrastinate last night and probably jumped the gun a little, so feel free to ignore the following!

Thanks,

Matty

Edit - Where I have referenced the SSID in the _connect() function, it technically should be the profile name...

Global $hDLL = DllOpen("wlanapi.dll"), $hClientHandle, $pGUID, $tGUID = DllStructCreate("byte GUID[16]")
OnAutoItExitRegister("_Exit")

_GetClientHandle()
Local $abGUIDs = _GetInterfaces()
_SelectInterface($abGUIDs[0])
_Connect("DI-624S")

Func _GetClientHandle()

    ;~  DWORD WINAPI WlanOpenHandle(
    ;~      _In_        DWORD dwClientVersion,
    ;~      _Reserved_  PVOID pReserved,
    ;~      _Out_       PDWORD pdwNegotiatedVersion,
    ;~      _Out_       PHANDLE phClientHandle
    ;~  );

    Local $aResult = DllCall($hDLL, "dword", "WlanOpenHandle", "dword", 2, "ptr", 0, "dword*", 0, "hwnd*", 0)
    If @error Or $aResult[0] Then
        If @error Then
            ;Realistically, this exception shouldn't occur...
            ConsoleWrite("DllCall failure - WlanOpenHandle" & @CRLF & "ERROR: " & @error & @CRLF & "EXTENDED: " & @extended & @CRLF)
        Else
            ;Errors returned by the API end up in $aResult[0].
            ;Turn error codes into plain text by calling _WinAPI_FormatMessage()
            ;Or look them up here - http://msdn.microsoft.com/en-us/library/ms681382(VS.85).aspx
            ConsoleWrite("API error - WlanOpenHandle" & @CRLF & "ERROR: " & $aResult[0] & @CRLF)
        EndIf
        Exit
    EndIf

    $hClientHandle = $aResult[4]
EndFunc   ;==>_GetClientHandle

Func _GetInterfaces()

    ;~  DWORD WINAPI WlanEnumInterfaces(
    ;~      _In_        HANDLE hClientHandle,
    ;~      _Reserved_  PVOID pReserved,
    ;~      _Out_       PWLAN_INTERFACE_INFO_LIST *ppInterfaceList
    ;~  );
    ;~  -------------------------------------------------------------
    ;~ typedef struct _WLAN_INTERFACE_INFO_LIST {
    ;~      DWORD               dwNumberOfItems;
    ;~      DWORD               dwIndex;
    ;~      WLAN_INTERFACE_INFO InterfaceInfo[];
    ;~ } WLAN_INTERFACE_INFO_LIST, *PWLAN_INTERFACE_INFO_LIST;
    ;~  -------------------------------------------------------------
    ;~ typedef struct _WLAN_INTERFACE_INFO {
    ;~      GUID                 InterfaceGuid;
    ;~      WCHAR                strInterfaceDescription[256];
    ;~      WLAN_INTERFACE_STATE isState;
    ;~ } WLAN_INTERFACE_INFO, *PWLAN_INTERFACE_INFO;

    Local $aResult, $pInterfaceList, $tInterfaceList, $iInterfaceCount, $pInterface, $tInterface
    $aResult = DllCall($hDLL, "dword", "WlanEnumInterfaces", "hwnd", $hClientHandle, "ptr", 0, "ptr*", 0)
    If @error Or $aResult[0] Then
        If @error Then
            ConsoleWrite("DllCall failure - WlanEnumInterfaces" & @CRLF & "ERROR: " & @error & @CRLF & "EXTENDED: " & @extended & @CRLF)
        Else
            ConsoleWrite("API error - WlanEnumInterfaces" & @CRLF & "ERROR: " & $aResult[0] & @CRLF)
        EndIf
        Exit
    EndIf

    ;Memory at $aResult[3] should look like this! (if, for example, two interfaces were found)
    ;number of interfaces   [dword (4 bytes)]
    ;nothing!               [dword (4 bytes)]
    ;1st interface GUID     [GUID struct (16 bytes)]
    ;1st interface descr    [256 WCHAR (512 bytes)]
    ;1st interface state    [dword (4 bytes)]
    ;2nd interface GUID     [GUID struct (16 bytes)]
    ;2nd interface descr    [256 WCHAR (512 bytes)]
    ;2nd interface state    [dword (4 bytes)]

    $pInterfaceList = $aResult[3]
    $tInterfaceList = DllStructCreate("dword", $pInterfaceList)
    $iInterfaceCount = DllStructGetData($tInterfaceList, 1)

    If Not $iInterfaceCount Then
        ConsoleWrite("No InterFaces were found." & @CRLF)
        Exit
    EndIf

    Local $abGUIDs[$iInterfaceCount]

    For $i = 0 To $iInterfaceCount - 1
        ;find the starting address of each interface structure in the list
            ;interface struct address = starting address + offset
            ;offset = interface index * interface structure size + list header size

        ;dump the GUID of each interface to an array - note: for our purposes we don't need to disect the GUID structure, just grab the raw binary data as a block

        $pInterface = Ptr(Number($pInterfaceList) + ($i * 532 + 8))
        $tInterface = DllStructCreate("byte GUID[16]; wchar descr[256]; int State", $pInterface)
        $abGUIDs[$i] = DllStructGetData($tInterface, "GUID")
        ConsoleWrite("Found Interface: " & DllStructGetData($tInterface, "descr") & @CRLF)
    Next

    ;Your executable will chew up memory every time this function is called unless you free it! - this is what "memory leak" is
    DllCall($hDLL, "dword", "WlanFreeMemory", "ptr", $pInterfaceList)

    Return $abGUIDs
EndFunc   ;==>_GetInterfaces

Func _SelectInterface($bGUID)
    ;dump the selected GUID data from enuminterfaces back into memory for fuctions to come.
    ;The struct is created in a global scope so AutoIt doesn't release memory on function exit!
    ;I'd imagine a static scope might work just as well
    DllStructSetData($tGUID, 1, $bGUID)
    $pGUID = DllStructGetPtr($tGUID)
EndFunc

Func _Connect($sSSID)
    ;This is a complicated fuction, which supports many types of connection methods.
    ;we will just cover connecting to an existing profile in the store.
    ;
    ;The API notifies with callbacks if the connection has failed or completed.
    ;These can become quite complex to handle with autoit, as it only supports a single thread.
    ;so the short story is I've left them well alone in this example.

    ;~ DWORD WINAPI WlanConnect(
    ;~      _In_        HANDLE hClientHandle,
    ;~      _In_        const GUID *pInterfaceGuid,
    ;~      _In_        const PWLAN_CONNECTION_PARAMETERS pConnectionParameters,
    ;~      _Reserved_  PVOID pReserved
    ;~ );
    ;~  -------------------------------------------------------------
    ;~ typedef struct _WLAN_CONNECTION_PARAMETERS {
    ;~      WLAN_CONNECTION_MODE wlanConnectionMode;
    ;~      LPCWSTR              strProfile;
    ;~      PDOT11_SSID          pDot11Ssid;
    ;~      PDOT11_BSSID_LIST    pDesiredBssidList;
    ;~      DOT11_BSS_TYPE       dot11BssType;
    ;~      DWORD                dwFlags;
    ;~ } WLAN_CONNECTION_PARAMETERS, *PWLAN_CONNECTION_PARAMETERS;
    ;~  -------------------------------------------------------------
    ; When wlanConnectionMode = wlan_connection_mode_profile
    ;   strProfile specifies the name of the profile used for the connection
    ;   pDot11Ssid is optional - we will ignore it.
    ;   pDesiredBssidList is optional - we will ignore it.

    ;The connection parameters struct in memory will look like
    ;Connection Mode        [dword (4 bytes)]
    ;Pointer to SSID string [Pointer to array of WCHARS (4 bytes)]
    ;Nothing!               [ptr (4 bytes)]
    ;Nothing!               [ptr (4 bytes)]
    ;BSS Type               [dword (4 bytes)]
    ;Flags                  [dword (4 bytes)]

    Local $tSSID, $tParameters
    Local Const $WLAN_CONNECTION_MODE_PROFILE = 0 ;connection mode
    Local Const $DOT11_BSS_TYPE_INFRASTRUCTURE = 1, $DOT11_BSS_TYPE_INDEPENDENT = 2 ;bss type
    Local Const $WLAN_CONNECTION_HIDDEN_NETWORK = 1, $WLAN_CONNECTION_ADHOC_JOIN_ONLY = 2 ;flags (of interest to us) a value of 3 will be a combination of flags.

    $tSSID = DllStructCreate("WCHAR[32]")
    DllStructSetData($tSSID, 1, $sSSID)
    $tParameters = DllStructCreate("dword Mode; ptr SSID; ptr; ptr; dword BSSType; dword Flags")

    DllStructSetData($tParameters, "Mode", $WLAN_CONNECTION_MODE_PROFILE)
    DllStructSetData($tParameters, "SSID", DllStructGetPtr($tSSID))
    ;Making a bit of an assuption in the next two lines, but you can expand on this later!
    DllStructSetData($tParameters, "BSSType", $DOT11_BSS_TYPE_INFRASTRUCTURE)
    DllStructSetData($tParameters, "Flags", 0)

    $aResult = DllCall($hDLL, "dword", "WlanConnect", "hwnd", $hClientHandle, "ptr", $pGUID, "ptr", DllStructGetPtr($tParameters), "ptr", 0)
    If @error Or $aResult[0] Then
        If @error Then
            ConsoleWrite("DllCall failure - WlanConnect" & @CRLF & "ERROR: " & @error & @CRLF & "EXTENDED: " & @extended & @CRLF)
        Else
            ConsoleWrite("API error - WlanConnect" & @CRLF & "ERROR: " & $aResult[0] & @CRLF)
        EndIf
    EndIf
EndFunc   ;==>_Connect

Func _Exit()
    ;Don't need to error check here - cause if they fail, what can you do!
    DllCall($hDLL, "dword", "WlanCloseHandle", "ptr", $hClientHandle, "ptr", 0)
    DllClose($hDLL)
EndFunc
Edited by MattyD

Share this post


Link to post
Share on other sites
AutID

Oh cool,

 

I had some time to procrastinate last night and probably jumped the gun a little, so feel free to ignore the following!

 

Thanks,

Matty

 

Edit - Where I have referenced the SSID in the _connect() function, it technically should be the profile name...

Global $hDLL = DllOpen("wlanapi.dll"), $hClientHandle, $pGUID, $tGUID = DllStructCreate("byte GUID[16]")
OnAutoItExitRegister("_Exit")

_GetClientHandle()
Local $abGUIDs = _GetInterfaces()
_SelectInterface($abGUIDs[0])
_Connect("DI-624S")

Func _GetClientHandle()

    ;~  DWORD WINAPI WlanOpenHandle(
    ;~      _In_        DWORD dwClientVersion,
    ;~      _Reserved_  PVOID pReserved,
    ;~      _Out_       PDWORD pdwNegotiatedVersion,
    ;~      _Out_       PHANDLE phClientHandle
    ;~  );

    Local $aResult = DllCall($hDLL, "dword", "WlanOpenHandle", "dword", 2, "ptr", 0, "dword*", 0, "hwnd*", 0)
    If @error Or $aResult[0] Then
        If @error Then
            ;Realistically, this exception shouldn't occur...
            ConsoleWrite("DllCall failure - WlanOpenHandle" & @CRLF & "ERROR: " & @error & @CRLF & "EXTENDED: " & @extended & @CRLF)
        Else
            ;Errors returned by the API end up in $aResult[0].
            ;Turn error codes into plain text by calling _WinAPI_FormatMessage()
            ;Or look them up here - http://msdn.microsoft.com/en-us/library/ms681382(VS.85).aspx
            ConsoleWrite("API error - WlanOpenHandle" & @CRLF & "ERROR: " & $aResult[0] & @CRLF)
        EndIf
        Exit
    EndIf

    $hClientHandle = $aResult[4]
EndFunc   ;==>_GetClientHandle

Func _GetInterfaces()

    ;~  DWORD WINAPI WlanEnumInterfaces(
    ;~      _In_        HANDLE hClientHandle,
    ;~      _Reserved_  PVOID pReserved,
    ;~      _Out_       PWLAN_INTERFACE_INFO_LIST *ppInterfaceList
    ;~  );
    ;~  -------------------------------------------------------------
    ;~ typedef struct _WLAN_INTERFACE_INFO_LIST {
    ;~      DWORD               dwNumberOfItems;
    ;~      DWORD               dwIndex;
    ;~      WLAN_INTERFACE_INFO InterfaceInfo[];
    ;~ } WLAN_INTERFACE_INFO_LIST, *PWLAN_INTERFACE_INFO_LIST;
    ;~  -------------------------------------------------------------
    ;~ typedef struct _WLAN_INTERFACE_INFO {
    ;~      GUID                 InterfaceGuid;
    ;~      WCHAR                strInterfaceDescription[256];
    ;~      WLAN_INTERFACE_STATE isState;
    ;~ } WLAN_INTERFACE_INFO, *PWLAN_INTERFACE_INFO;

    Local $aResult, $pInterfaceList, $tInterfaceList, $iInterfaceCount, $pInterface, $tInterface
    $aResult = DllCall($hDLL, "dword", "WlanEnumInterfaces", "hwnd", $hClientHandle, "ptr", 0, "ptr*", 0)
    If @error Or $aResult[0] Then
        If @error Then
            ConsoleWrite("DllCall failure - WlanEnumInterfaces" & @CRLF & "ERROR: " & @error & @CRLF & "EXTENDED: " & @extended & @CRLF)
        Else
            ConsoleWrite("API error - WlanEnumInterfaces" & @CRLF & "ERROR: " & $aResult[0] & @CRLF)
        EndIf
        Exit
    EndIf

    ;Memory at $aResult[3] should look like this! (if, for example, two interfaces were found)
    ;number of interfaces   [dword (4 bytes)]
    ;nothing!               [dword (4 bytes)]
    ;1st interface GUID     [GUID struct (16 bytes)]
    ;1st interface descr    [256 WCHAR (512 bytes)]
    ;1st interface state    [dword (4 bytes)]
    ;2nd interface GUID     [GUID struct (16 bytes)]
    ;2nd interface descr    [256 WCHAR (512 bytes)]
    ;2nd interface state    [dword (4 bytes)]

    $pInterfaceList = $aResult[3]
    $tInterfaceList = DllStructCreate("dword", $pInterfaceList)
    $iInterfaceCount = DllStructGetData($tInterfaceList, 1)

    If Not $iInterfaceCount Then
        ConsoleWrite("No InterFaces were found." & @CRLF)
        Exit
    EndIf

    Local $abGUIDs[$iInterfaceCount]

    For $i = 0 To $iInterfaceCount - 1
        ;find the starting address of each interface structure in the list
            ;interface struct address = starting address + offset
            ;offset = interface index * interface structure size + list header size

        ;dump the GUID of each interface to an array - note: for our purposes we don't need to disect the GUID structure, just grab the raw binary data as a block

        $pInterface = Ptr(Number($pInterfaceList) + ($i * 532 + 8))
        $tInterface = DllStructCreate("byte GUID[16]; wchar descr[256]; int State", $pInterface)
        $abGUIDs[$i] = DllStructGetData($tInterface, "GUID")
        ConsoleWrite("Found Interface: " & DllStructGetData($tInterface, "descr") & @CRLF)
    Next

    ;Your executable will chew up memory every time this function is called unless you free it! - this is what "memory leak" is
    DllCall($hDLL, "dword", "WlanFreeMemory", "ptr", $pInterfaceList)

    Return $abGUIDs
EndFunc   ;==>_GetInterfaces

Func _SelectInterface($bGUID)
    ;dump the selected GUID data from enuminterfaces back into memory for fuctions to come.
    ;The struct is created in a global scope so AutoIt doesn't release memory on function exit!
    ;I'd imagine a static scope might work just as well
    DllStructSetData($tGUID, 1, $bGUID)
    $pGUID = DllStructGetPtr($tGUID)
EndFunc

Func _Connect($sSSID)
    ;This is a complicated fuction, which supports many types of connection methods.
    ;we will just cover connecting to an existing profile in the store.
    ;
    ;The API notifies with callbacks if the connection has failed or completed.
    ;These can become quite complex to handle with autoit, as it only supports a single thread.
    ;so the short story is I've left them well alone in this example.

    ;~ DWORD WINAPI WlanConnect(
    ;~      _In_        HANDLE hClientHandle,
    ;~      _In_        const GUID *pInterfaceGuid,
    ;~      _In_        const PWLAN_CONNECTION_PARAMETERS pConnectionParameters,
    ;~      _Reserved_  PVOID pReserved
    ;~ );
    ;~  -------------------------------------------------------------
    ;~ typedef struct _WLAN_CONNECTION_PARAMETERS {
    ;~      WLAN_CONNECTION_MODE wlanConnectionMode;
    ;~      LPCWSTR              strProfile;
    ;~      PDOT11_SSID          pDot11Ssid;
    ;~      PDOT11_BSSID_LIST    pDesiredBssidList;
    ;~      DOT11_BSS_TYPE       dot11BssType;
    ;~      DWORD                dwFlags;
    ;~ } WLAN_CONNECTION_PARAMETERS, *PWLAN_CONNECTION_PARAMETERS;
    ;~  -------------------------------------------------------------
    ; When wlanConnectionMode = wlan_connection_mode_profile
    ;   strProfile specifies the name of the profile used for the connection
    ;   pDot11Ssid is optional - we will ignore it.
    ;   pDesiredBssidList is optional - we will ignore it.

    ;The connection parameters struct in memory will look like
    ;Connection Mode        [dword (4 bytes)]
    ;Pointer to SSID string [Pointer to array of WCHARS (4 bytes)]
    ;Nothing!               [ptr (4 bytes)]
    ;Nothing!               [ptr (4 bytes)]
    ;BSS Type               [dword (4 bytes)]
    ;Flags                  [dword (4 bytes)]

    Local $tSSID, $tParameters
    Local Const $WLAN_CONNECTION_MODE_PROFILE = 0 ;connection mode
    Local Const $DOT11_BSS_TYPE_INFRASTRUCTURE = 1, $DOT11_BSS_TYPE_INDEPENDENT = 2 ;bss type
    Local Const $WLAN_CONNECTION_HIDDEN_NETWORK = 1, $WLAN_CONNECTION_ADHOC_JOIN_ONLY = 2 ;flags (of interest to us) a value of 3 will be a combination of flags.

    $tSSID = DllStructCreate("WCHAR[32]")
    DllStructSetData($tSSID, 1, $sSSID)
    $tParameters = DllStructCreate("dword Mode; ptr SSID; ptr; ptr; dword BSSType; dword Flags")

    DllStructSetData($tParameters, "Mode", $WLAN_CONNECTION_MODE_PROFILE)
    DllStructSetData($tParameters, "SSID", DllStructGetPtr($tSSID))
    ;Making a bit of an assuption in the next two lines, but you can expand on this later!
    DllStructSetData($tParameters, "BSSType", $DOT11_BSS_TYPE_INFRASTRUCTURE)
    DllStructSetData($tParameters, "Flags", 0)

    $aResult = DllCall($hDLL, "dword", "WlanConnect", "hwnd", $hClientHandle, "ptr", $pGUID, "ptr", DllStructGetPtr($tParameters), "ptr", 0)
    If @error Or $aResult[0] Then
        If @error Then
            ConsoleWrite("DllCall failure - WlanConnect" & @CRLF & "ERROR: " & @error & @CRLF & "EXTENDED: " & @extended & @CRLF)
        Else
            ConsoleWrite("API error - WlanConnect" & @CRLF & "ERROR: " & $aResult[0] & @CRLF)
        EndIf
    EndIf
EndFunc   ;==>_Connect

Func _Exit()
    ;Don't need to error check here - cause if they fail, what can you do!
    DllCall($hDLL, "dword", "WlanCloseHandle", "ptr", $hClientHandle, "ptr", 0)
    DllClose($hDLL)
EndFunc

Hmmm I moved from this function because I knew I had to get the _WLAN_CONNECTION_PARAMETERS so I could pass the to the connection function. It doesn't look so hard though.

How about connecting to a profile that doesn't exists in our store?

Edited by AutID

Share this post


Link to post
Share on other sites
MattyD

Its not really much different to be honest - just takes a bit of tweaking of the connection parameters.

Obviously change wlanConnectionMode  to specify the relevant connection method. Then...

a ) For a temporary profile (connect with an XML profile not in the store), replace the profile name with the entire XML string.

b ) For discovery (OS prompts for password if using secure mode),

 - strprofile must be null

 - you need to create an PDOT11_SSID struct and set pDot11SSID to its pointer

;EXAMPLE CONNECTION PARAMETER STRUCTS

;CONNECTION MODES
Local Enum $WLAN_CONNECTION_MODE_PROFILE, $WLAN_CONNECTION_MODE_TEMPORARY_PROFILE, _
        $WLAN_CONNECTION_MODE_DISCOVERY_SECURE, $WLAN_CONNECTION_MODE_DISCOVERY_UNSECURE

;BSS Types
Local Const $DOT11_BSS_TYPE_INFRASTRUCTURE = 1, $DOT11_BSS_TYPE_INDEPENDENT = 2 ;Independant for Ad Hoc networks!

;FLAGS - Use bitOR to combine if need be!
Local Const $WLAN_CONNECTION_HIDDEN_NETWORK = 1 ;Only valid with infrastructure BSS Type
Local Const $WLAN_CONNECTION_ADHOC_JOIN_ONLY = 2 ;Only valid with Ad Hoc BSS Type
Local Const $WLAN_CONNECTION_IGNORE_PRIVACY_BIT = 4 ;Olny valid for infrastructure BSS Type in temporary profile mode
Local Const $WLAN_CONNECTION_EAPOL_PASSTHROUGH = 8 ;Olny valid for infrastructure BSS Type in temporary profile mode with 802.1x disabled

;CONNECTION PARAMETER STRUCT
$tConnParams = DllStructCreate("dword Mode; dword Profile; ptr SSID; ptr BSSID; ptr BSSType; dword Flags")

;----------------------------------------------------------
;For a Profile Connection:

;This is the only mode supported on XP
;Ignoring optional parameters SSID and BSSID List
;When the SSID paramater is not specified, all SSIDs in the profile will be tried
;The BSSID list paramater specifies prefferd BSSIDs to connect to. It is legal with all connection modes (illeagl in XP)
;----------------------------------------------------------
;Connection Mode        [dword (4 bytes)]
;Ptr to profile name    [Ptr to array of WCHARS (4 bytes)]
;Nothing!               [ptr (4 bytes)]
;Nothing!               [ptr (4 bytes)]
;BSS Type               [dword (4 bytes)]
;Flags                  [dword (4 bytes)]

$tProfile = DllStructCreate("WCHAR[512]")
DllStructSetData($tProfile, 1, "Profile Name")

DllStructSetData($tConnParams, "Mode", $WLAN_CONNECTION_MODE_PROFILE)
DllStructSetData($tConnParams, "Profile", DllStructGetPtr($tProfile))
DllStructSetData($tConnParams, "BSSType", $DOT11_BSS_TYPE_INFRASTRUCTURE)

;if not broadcasting SSID
;   DllStructSetData($tConnParams, "Flags", $WLAN_CONNECTION_HIDDEN_NETWORK)

;if Ad Hoc, and another computer has already created the network
;   DllStructSetData($tConnParams, "Flags", $WLAN_CONNECTION_ADHOC_JOIN_ONLY)
;---------------------------------------------------------
;For a Temporary Profile Connection:

;Again ignoring optional parameters SSID and BSSID List
;---------------------------------------------------------
;Connection Mode        [dword (4 bytes)]
;Ptr to XML profile     [Ptr to array of WCHARS (4 bytes)]
;Nothing!               [ptr (4 bytes)]
;Nothing!               [ptr (4 bytes)]
;BSS Type               [dword (4 bytes)]
;Flags                  [dword (4 bytes)]

$tProfile = DllStructCreate("WCHAR[512]")
DllStructSetData($tProfile, 1, "<XML PROFILE GOES HERE>")

DllStructSetData($tConnParams, "Mode", $WLAN_CONNECTION_MODE_TEMPORARY_PROFILE)
DllStructSetData($tConnParams, "Profile", DllStructGetPtr($tProfile))
DllStructSetData($tConnParams, "BSSType", $DOT11_BSS_TYPE_INFRASTRUCTURE)
DllStructSetData($tConnParams, "Flags", 0)

;---------------------------------------------------------
;For a Discovery Connection:

;SSID Mandatory, Profile element is illegal - ignoring BSSID List
;---------------------------------------------------------
;Connection Mode        [dword (4 bytes)]
;Nothing!               [Ptr to array of WCHARS (4 bytes)]
;Ptr to SSID Struct     [ptr (4 bytes)]
;Nothing!               [ptr (4 bytes)]
;BSS Type               [dword (4 bytes)]
;Flags                  [dword (4 bytes)]
;
;SSID STRUCT:
;CHAR Array Size        [dword (4 bytes)]
;SSID string            [CHAR array (32 bytes) (CHAR = 1 byte, Max SSID Length = 32)]

$sSSID = "MySSID"
$tSSID = DllStructCreate("dword; CHAR[32]")
DllStructSetData($tSSID, 1, StringLen($sSSID))
DllStructSetData($tSSID, 2, $sSSID)

DllStructSetData($tConnParams, "Mode", $WLAN_CONNECTION_MODE_DISCOVERY_SECURE)
;Or, for open networks:
;   DllStructSetData($tConnParams, "Mode", $WLAN_CONNECTION_MODE_DISCOVERY_UNSECURE)
DllStructSetData($tConnParams, "SSID", DllStructGetPtr($tSSID))
DllStructSetData($tConnParams, "BSSType", $DOT11_BSS_TYPE_INFRASTRUCTURE)
DllStructSetData($tConnParams, "Flags", 0)
Edited by MattyD
  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×