AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Generic Callback Example - EnumWindows
Goto page Previous  1, 2, 3, 4  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
JGR



Joined: 15 Jun 2006
Posts: 52
Location: Unavailable until ~30th August

PostPosted: Mon May 21, 2007 4:06 pm    Post subject: Reply with quote

Slightly revised. Your version leaks memory (OK, only 60 bytes), and assumes two arguments to the callback function, and that the user doesn't want an ID passed to the message handler in wParam.
Note that that feature was included to allow similar callbacks (or all callbacks), to use the same message handler...

Code:
SetCallbackFunction(FunctionName, params, _Remove=0, wparam=0) {
Global
Static _CallbackDllLoaded := false
Local _pid, _hwnd, _hHookModule, _MsgNmb
If (_Remove) or (_Remove = "Off") {
  If %FunctionName% is integer
  {
    OnMessage(%FunctionName%, "")
    DllCall("GlobalFree", "UInt", %FunctionName%_PS)
    %FunctionName% := ""
    %FunctionName%_PS := ""
    Return "Removed Callback"
  }
  Else
    Return "ERROR: Invalid Message Number"
}
_pid:=DllCall("GetCurrentProcessId","Uint")
DetectHiddenWindows, On
_hwnd:=WinExist("ahk_pid " . _pid)
If !(_CallbackLoaded) {
   SplitPath, A_AhkPath,,x_LibPath
   x_LibPath .= "\Include\callback.dll"
   Loop, %x_LibPath%
     x_LibPath = %A_LoopFileShortPath%
  _hHookModule := DllCall("LoadLibrary", "str", x_LibPath)
  _CallbackLoaded := true
  }
_MsgNmb := DllCall("RegisterWindowMessage", "Str", "AHK__" . FunctionName)
%FunctionName%_PS:=DllCall(x_LibPath . "\callbackit", "UInt", params,"UInt", _hwnd, "UInt", _MsgNmb, "UInt", wparam)
OnMessage(_MsgNmb, FunctionName)
%FunctionName% := _MsgNmb
Return _MsgNmb
}
Back to top
View user's profile Send private message
corrupt



Joined: 29 Dec 2004
Posts: 2397

PostPosted: Mon May 21, 2007 4:30 pm    Post subject: Reply with quote

JGR wrote:
Slightly revised.
Thanks Smile . Do you think it would be typical to have to set the params param frequently? If not would this work ok?
Code:
SetCallbackFunction(FunctionName, _Remove=0, params=2, wparam=0) {
That way most calls would only require 1 param(?)
Back to top
View user's profile Send private message Visit poster's website
JGR



Joined: 15 Jun 2006
Posts: 52
Location: Unavailable until ~30th August

PostPosted: Mon May 21, 2007 4:37 pm    Post subject: Reply with quote

The params variable should not be set to a default value, because if it is wrong, the stack will not be corrected properly after the callback, and the program will crash.
AHK should mop up the error for you, but there is a possibility of irreversible random memory corruption screwing up the process.
The callback functions take different numbers of parameters, it is only chance that EnumWindows takes 2, DdeCallback takes 8, EnumFontsProc takes 4, etc.

JGR
Back to top
View user's profile Send private message
corrupt



Joined: 29 Dec 2004
Posts: 2397

PostPosted: Mon May 21, 2007 4:48 pm    Post subject: Reply with quote

Fair enough. Thanks Smile . Here's a modified version of the sample script that uses #Include files for SetCallbackFunction and GetDeRefInteger for fun Smile
Code:
Gui, Add, ListView, x6 y10 w770 h350 , HWND|Title|Class
Gui, Show, h377 w787, EnumWindows Example

SetCallbackFunction("EnumWindows", "2")
DllCall("EnumWindows","UInt",EnumWindows_PS,"UInt",0)
Return

GuiClose:
SetCallbackFunction("EnumWindows", "", "Off")
ExitApp

EnumWindows(wParam, lParam, msg, hwnd) {
  DetectHiddenWindows, On
  winid:=GetDeRefInteger(lParam)
  otherparam:=GetDeRefInteger(lParam+4)
  WinGetTitle, text, ahk_id %winid%
  WinGetClass, class, ahk_id %winid%
  LV_Add("", winid, text, class)
  LV_ModifyCol()
  return 1
}

#Include SetCallbackFunction.ahk
#Include GetDeRefInteger.ahk
Back to top
View user's profile Send private message Visit poster's website
SKAN



Joined: 26 Dec 2005
Posts: 5887

PostPosted: Tue May 22, 2007 6:19 pm    Post subject: Reply with quote

Will this work with UrlMon.dll\UrlDownloadToFile ?
Can someone demonstrate it.. Please ?

Rolling Eyes
Back to top
View user's profile Send private message
JGR



Joined: 15 Jun 2006
Posts: 52
Location: Unavailable until ~30th August

PostPosted: Tue May 22, 2007 7:44 pm    Post subject: Reply with quote

Bleh... COM
Yes callbacks could be used, but there is really no point unless you want to also run a progress bar, etc.
You would need to pass COM interface structures to the last (and first?) parameters to do this, which is a lot of messing about for a progress bar...

If you simply pass 0 to parameters 1, 4 and 5 there is no need for a callback function.

JGR
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 5887

PostPosted: Tue May 22, 2007 7:54 pm    Post subject: Reply with quote

JGR wrote:
If you simply pass 0 to parameters 1, 4 and 5 there is no need for a callback function.


Yes! That is exactly the thing already available: URLDownloadToFile.
I want a Progress bar ( So does many around this forum ), and had previously requested Sean to look into it. Sean said that is not possible without handling a Callback. Maybe now he can give it a try!

Smile
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 1338

PostPosted: Wed May 23, 2007 12:06 am    Post subject: Reply with quote

Skan wrote:
I want a Progress bar ( So does many around this forum ), and had previously requested Sean to look into it. Sean said that is not possible without handling a Callback. Maybe now he can give it a try!

It's not a mere callback, it should be a callback COM interface, i.e., an event sink.
So, I'm afraid that it's not possible currently with AHK.

PS. I think that COM sink can be viewed as an array of callback functions, in a certain sense.
But, I'm not sure if that is essentially enough/all aspect to care about it.
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 5887

PostPosted: Wed May 23, 2007 3:44 pm    Post subject: Reply with quote

Sean wrote:
PS. I think that COM sink can be viewed as an array of callback functions, in a certain sense.
But, I'm not sure if that is essentially enough/all aspect to care about it.


Sean, It would be very nice if you give us a solution, someday.
Thanks. Smile
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 1338

PostPosted: Wed May 23, 2007 5:24 pm    Post subject: Reply with quote

Skan wrote:
It would be very nice if you give us a solution, someday.

Actually I got a concrete idea about how to handle this, so leave only experiment.
However, I'm feeling too tired to pursue it atm. Looks like I coded too much lately.
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 5887

PostPosted: Wed May 23, 2007 5:35 pm    Post subject: Reply with quote

Sean wrote:
I got a concrete idea about how to handle this, so leave only experiment.


Surprised Glad to know it.

Quote:
However, I'm feeling too tired to pursue it atm.


No problem.. We have been waiting for a long time.

Quote:
Looks like I coded too much lately.


Yes! Much to the benefit of our community! Thanks.

Smile
Back to top
View user's profile Send private message
Joy2DWorld



Joined: 04 Dec 2006
Posts: 422
Location: Galil, Israel

PostPosted: Thu May 24, 2007 1:38 am    Post subject: Reply with quote

very cool, very needed.


and here is something either very brilliant, or very not!:

Have been working to code in assembly a callback, and seems like you might can easily do it with yours -->

without all the dll stuff....

forego the dll, and put the machine code directly into an AHK var.

ie. unpack hex into var and give var pointer as callback address...


(i was trying to use the var space itself to pass the params from the callback-- which maybe is easier... maybe NOT...)
but the machine code seems fairly short if dropping all the DLL issues, and using just the basic handler...
and would eliminate reliance on external dll...
_________________
Joyce Jamce
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 1338

PostPosted: Thu May 24, 2007 2:31 am    Post subject: Reply with quote

Joy2DWorld wrote:
forego the dll, and put the machine code directly into an AHK var.

Of course you can do it if you know how to deal with machine codes.
JGR kindly provides the source code, even the machine code itself, i.e., the compiled dll.
So, you just have to copy the relevant part and refill the necessary infos into it at each usage.
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4016
Location: Pittsburgh

PostPosted: Thu May 24, 2007 3:41 am    Post subject: Reply with quote

Joy2DWorld wrote:
forego the dll, and put the machine code directly into an AHK var
This is what Shimanov did more than a year ago in his short period, high resolution timer script.
Back to top
View user's profile Send private message
Joy2DWorld



Joined: 04 Dec 2006
Posts: 422
Location: Galil, Israel

PostPosted: Thu May 24, 2007 4:40 am    Post subject: Reply with quote

amazing.



ok... based on the asm code,


we don't need the "returns address to pass to callback func" code,
as address is var location,


am unclear of how to deal with the messaging end...




@Sean

Quote:
JGR kindly provides the source code, even the machine code itself, i.e., the compiled dll.
So, you just have to copy the relevant part and refill the necessary infos into it at each usage.


well, obviously, need a new assembly of the cut & minimized asm... modified to handle direct insertion for the ahk side of the params as Laszlo points to Shimanov's brilliant [and year old!!] solution ! (no need for dll structure, etc.),


been cracking my head on it (in conjunction with building a DDE function) since last year <<http://www.autohotkey.com/forum/viewtopic.php?t=14740&postdays=0&postorder=asc&highlight=call+back&start=15>>


and now Sean's made it look so easy with his up and working DDE client side.. <<http://www.autohotkey.com/forum/viewtopic.php?t=19169&postdays=0&postorder=asc&start=0>>


and if can just squeeze JGR's solution into a var... 'dll less' callback and DDE server in AHK... wow.
_________________
Joyce Jamce
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page Previous  1, 2, 3, 4  Next
Page 2 of 4

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group