Jump to content

CallBack


Recommended Posts

Hi all,

Just curious about CallBack or whatever it's called. I have never really used it but seen lots of people make good use of whatever it does.

Could someone care to explain to me what it does and possibly an example with and without it being used showing the results?

Thanks,

James

I think a good example of where you use a callback is in sorting a list view. Have a look at GUICtrlRegisterListViewSort.

When you call the sort function it doesn't have a clue how to sort the list. If the list is numeric then it can easily be sorted in order, if it is weights then is 2Kg more than 6 lb? Or if it's colours should green go before red? The only person who knows is the programmer.

To deal with this the sort function needs to be given a function it can call to decide how to sort 2 adjacent items. As the sort function goes through the list it keeps calling the function you tell it about. It will pass 2 parameters, say "Apricots" and "Blue jeans", to the function and the function has to tell it which one goes first. So you call the sort listview function and tell it about your sort function, then the listview sort function keeps calling back to your sort function until it's done.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

So it allows you to call the function again but differently depending on the scenario?

I'm not sure how you mean that, but it doesn't sound right to me.

pseudocode

Func SomeFnWHichWantsACallbackFn(param1, "MyCallbackFunc")
while notfinished

  $a = something
  $b = somethingelse
  decision = MyCallBackFunc($a,$b)
  ActOnDecision
wend
endfunc
  


Func MyCallBackFunc($e,$f) 
   if $e > $f then return $e
   return $f
endfunc

It's no different than when you have a function and in that function you call another function. It's just that you are calling an external function and letting it use one of your own internal functions.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

What I believe a callback is is a kind of hook, you register the hook(callback) in the operating system, then the specified hook "procedure"(function) gets called when the hooked function gets called(usually before the hooked function is called), and then you can differ in how the hooked function behaves(depending on the data, etc. which the hooked function is called with).

(

That's atleast how I believe how a callback works. It's kind of a hook, but not in all circumstances(like with the EnumChildWindows function: http://msdn.microsoft.com/en-us/library/ms...94(VS.85).aspx). :)

Link to comment
Share on other sites

A callback is just a function that is called when a specific event occurs. It allows you to specify the code that happens when that event occurs. In C/C++ a callback is just a pointer to a function.

Like the example Freefry used, the EnumWindows takes a callback as a parameter. All that parameter really is, is a pointer to the function that should be called each time a new window is found (while enumerating all the windows). So this is how it works:

EnumWindows(MyFunction);
        
        function MyFunction(window handle)
        {
                  // this is the next window handle in the enumeration.
        }

What really happens is.. (roughly)

function EnumWindows(callback)
  { 
           while (window->next_window != NULL)
           {
                window = window->next_window;
                 callback(window);
           }
  }

So if we pass "MyFunction" to EnumWindows, it is going to call MyFunction for each window in the list. Hope that makes sense. :)

Edited by cppman
Link to comment
Share on other sites

Here's an old example of the EnumChildWindows I've made(don't worry about the errors, they're cus I declare a global variable/array inside a function):

#include <WinAPI.au3>

$a = _WinAPI_EnumChildWindow(WinGetHandle(""))

For $i = 1 To $a[0]
    ConsoleWrite("Window " & WinGetTitle($a[$i]) & " [ " & $a[$i] & "]" & " is of type: " & _WinAPI_GetClassName($a[$i]) & @LF)
Next

Func _WinAPI_EnumChildWindow($hParent, $vClass = "")
    Global $vCWin[1] = [0], $vRetClass = $vClass
    Local $voNumChilds = 0
    Local $LPENUMFUNC = DllCallbackRegister("_WinAPI_EnumChildWindowProc", "int", "hwnd;lparam")
    DllCall("user32.dll", "int", "EnumChildWindows", "hwnd", $hParent, "ptr", DllCallbackGetPtr($LPENUMFUNC), "lparam", 0)
    DllCallbackFree($LPENUMFUNC)
    Return $vCWin
EndFunc  ;==>_WINAPI_ENUMCHILDWINDOW

Func _WINAPI_ENUMCHILDWINDOWPROC($hWnd, $lParam)
    Local $wClass = _WINAPI_GETCLASSNAME($hWnd)
    If $vRetClass = "" Or $vRetClass = $wClass Then
        $vCWin[0] += 1
        ReDim $vCWin[$vCWin[0] + 1]
        $vCWin[$vCWin[0]] = $hWnd
    EndIf
    If $vRetClass = $wClass Then
        Return False
    Else
        Return True
    EndIf
EndFunc  ;==>_WINAPI_ENUMCHILDWINDOWPROC
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...