Jump to content

My friend the callback


MattyD
 Share

Recommended Posts

Hi all,

This is the issue that has been bugging me for quite a while now.

I don't seem to be able to be able to use the callback fuctionality unless I preemptively put my script into a sleep loop. What I am thinking with my prolably twisted logic is that the callback tries to kick off a new thread which confuzzles AutoIt a bit...

Secondly, some of the notifications come back with reasonably complex structues that takes a little time to extract all the data. If another notification comes in during this time it also will kill the script. No big suprises here (I remember reading somewhere to keep callback functions small). What I was thinking of doing is throwing the whole struture as a single binary element into a global variable to process later. Feels a bit clumsy though - would there be a better way?

But more to the point im really just looking for direction:

Should I be looking into creating a DLL?

I think I remember seeing a thread manipulation script by Trancexx?

Am I completely missing something?

Should I be looking for conselation?

I really would appreaciate some guidance,

Thanks,

Matt

P.S. Sorry - the demo script won't work on XP. and I really tried to keep it as short as possible too!

Global Const $WLAN_NOTIFICATION_SOURCE_NONE = 0
Global Const $WLAN_NOTIFICATION_SOURCE_ALL = 0X0000FFFF
Global Const $WLAN_NOTIFICATION_SOURCE_ACM = 0X00000008
Global Const $WLAN_NOTIFICATION_SOURCE_MSM = 0X00000010
Global Const $WLAN_NOTIFICATION_ACM_SCAN_COMPLETE = 7
Global Const $WLAN_NOTIFICATION_MSM_SIGNAL_QUALITY_CHANGE = 8

Global $hWLANAPI = DllOpen("WlanAPI.dll"), $hClientHandle, $pGUID, $tCallback, $fCallbackComplete, $iDoSomething

$tCallback = DllCallbackRegister("_WLAN_NOTIFICATION_CALLBACK", "none", "ptr;ptr")
If @error Then ConsoleWrite("! Error @ln[" & @ScriptLineNumber & "]" & @CRLF)
_Init()
_WlanRegisterNotification($hClientHandle, $WLAN_NOTIFICATION_SOURCE_ALL, True, DllCallbackGetPtr($tCallback))
For $i = 0 To 5
    _GenerateNotification()
Next
_Finish()

Func _GenerateNotification()
    Local $iTimer = TimerInit()
    $fCallbackComplete = False
    _WinAPI_WlanScan($hClientHandle, $pGUID)
    While TimerDiff($iTimer) < 4000 Or Not $fCallbackComplete ;MS Logo'd drivers need to finish scans in under 4 seconds
        ;$iDoSomething +=1 ;uncomment this line and comment out the sleep.
        Sleep(100) ;doing anything but sleep while a notification occurs will crash the script.
    WEnd

EndFunc

Func _WLAN_NOTIFICATION_CALLBACK($pNotificationData, $pVoid)
    Local $tNotifData, $iNotifSource, $iNotifCode, $iTimer
    $tNotifData = DllStructCreate("dword NotificationSource; dword NotificationCode; byte GUID[16]; dword DataSize; ptr Data", $pNotificationData)
    $iNotifSource = DllStructGetData($tNotifData, "NotificationSource")
    $iNotifCode = DllStructGetData($tNotifData, "NotificationCode")

    Switch $iNotifSource
        Case $WLAN_NOTIFICATION_SOURCE_ACM
            Switch $iNotifCode
                Case $WLAN_NOTIFICATION_ACM_SCAN_COMPLETE
                    ConsoleWrite("Scan Complete!" & @CRLF)
                Case Else
                    ConsoleWrite("ACM Notification Code: " & $iNotifCode & @CRLF)
            EndSwitch
        Case $WLAN_NOTIFICATION_SOURCE_MSM
            Switch $iNotifCode
                Case $WLAN_NOTIFICATION_MSM_SIGNAL_QUALITY_CHANGE
                    ConsoleWrite("Signal Quality Change! " & @CRLF)
                Case Else
                    ConsoleWrite("MSM Notification Code: " & $iNotifCode & @CRLF)
            EndSwitch
        Case Else
            ConsoleWrite("Notification Source: " & $iNotifSource & @CRLF)
            ConsoleWrite("Notification Code: " & $iNotifCode & @CRLF)
    EndSwitch

    ;$iTimer = TimerInit() ;the script will crash if notification occurs while still in a callback
    ;While TimerDiff($iTimer) < 4000
    ;   Sleep(100)
    ;WEnd

    $fCallbackComplete = True
    Return 0
EndFunc

Func _Init()
    Local $aResult, $iClientVersion = 2, $tInterfaceList
    $aResult = DllCall($hWLANAPI, "dword", "WlanOpenHandle", "dword", $iClientVersion, "ptr", 0, "dword*", 0, "hwnd*", 0)
    If @error Or $aResult[0] Then Return ConsoleWrite("! Error @ln[" & @ScriptLineNumber & "]" & @CRLF)
    $hClientHandle = $aResult[4]

    $aResult = DllCall($hWLANAPI, "dword", "WlanEnumInterfaces", "hwnd", $hClientHandle, "ptr", 0, "ptr*", 0)
    If @error Or $aResult[0] Then Return ConsoleWrite("! Error @ln[" & @ScriptLineNumber & "]" & @CRLF)
    $iNumberOfItems = DllStructCreate("dword NumberOfItems", $aResult[3])
    If Not DllStructGetData($iNumberOfItems, "NumberOfItems") Then Return ConsoleWrite("! Error @ln[" & @ScriptLineNumber & "]" & @CRLF)
    $pGUID = Ptr(Number($aResult[3]) + 8)
EndFunc

Func _Finish()
    _WlanRegisterNotification($hClientHandle, $WLAN_NOTIFICATION_SOURCE_NONE, True, DllCallbackGetPtr($tCallback))
    DllCallbackFree($tCallback)
    DllCall($hWLANAPI, "dword", "WlanCloseHandle", "ptr", $hClientHandle, "ptr", 0)
    DllClose($hWLANAPI)
EndFunc

Func _WinAPI_WlanScan($hClientHandle, $pGUID, $pDOT11_SSID = 0, $pWLAN_RAW_DATA = 0)
    $aResult = DllCall($hWLANAPI, "dword", "WlanScan", "hwnd", $hClientHandle, "ptr", $pGUID, "ptr", $pDOT11_SSID, "ptr", $pWLAN_RAW_DATA, "ptr", 0)
    If @error Or $aResult[0] Then Return ConsoleWrite("! Error @ln[" & @ScriptLineNumber & "]" & @CRLF)
EndFunc

Func _WlanRegisterNotification($hClientHandle, $iNotifSource, $fIgnoreDuplicate, $pNotificationCallback = 0, $pCallbackContext = 0)
    $aResult = DllCall($hWLANAPI, "dword", "WlanRegisterNotification", "hwnd", $hClientHandle, "dword", $iNotifSource, "int", $fIgnoreDuplicate, "ptr", $pNotificationCallback, "ptr", $pCallbackContext, "ptr", 0, "ptr*", 0)
    If @error Or $aResult[0] Then Return ConsoleWrite("! Error @ln[" & @ScriptLineNumber & "]" & @CRLF)
    Return SetExtended($aResult[7], True)
EndFunc
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...