Jump to content

About DllCallbackRegister behaviour


matwachich
 Share

Recommended Posts

Hello!

I was wondering why when I call DllCallbackRegister 2 times on the same function with the same return and parameters types, the function gets registered 2 times?

I think in order to avoid memory leak (in case you cannot or forget to call DllCallbackFree), in this case, we should not have a 2'nd registration.

This behaviour could be usefull in some UDFs that are wrappers for C libraries when you need to passe callback functions. This will permit to the user of the UDF to passe directly functions, then the function is registered inside the UDF. So event if recalling the same function again and again, the callback is registered only once.

Example:

; this function could be called many times with the same AutoIt function as $pfnCFunction parameter
Func _lua_pushCClosure($pState, $pfnCFunction, $iN)
    ; calling DllCallbackRegister again and again on the same function and with same return and parameters type should register the function only once
    Local $hCallback = DllCallbackRegister(IsFunc($pfnCFunction) ? FuncName($pfnCFunction) : $pfnCFunction, "int:cdecl", "ptr")
    DllCall($__gLua_hDLL, "none:cdecl", "lua_pushcclosure", "ptr", $pState, "ptr", DllCallbackGetPtr($hCallback), "int", $iN)
    If @error Then Return SetError(@error, 0, False)
    Return True
EndFunc

Any thoughts?

Edit: as for now, in order to let the UDFs user pass any value he wants, I'm using this function

Func __lua_helper_regFunc($vFunc, $sReturn, $sParams)
    Local Static $oReg = ObjCreate("Scripting.Dictionary")
    Switch VarGetType($vFunc)
        Case "String", "UserFunction"
            $vFunc = IsString($vFunc) ? $vFunc : FuncName($vFunc)
            Local $sKey = $vFunc & "|" & $sReturn & "|" & $sParams
            If $oReg.Exists($sKey) Then
                Return DllCallbackGetPtr($oReg.Item($sKey))
            Else
                Local $hReg = DllCallbackRegister($vFunc, $sReturn, $sParams)
                $oReg.Item($sKey) = $hReg
                Return DllCallbackGetPtr($hReg)
            EndIf
        Case "Int32"
            Return DllCallbackGetPtr($vFunc)
        Case "Ptr"
            Return $vFunc
    EndSwitch
    Return Null
EndFunc

 

Edited by matwachich
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...