 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
JGR
Joined: 15 Jun 2006 Posts: 52 Location: Unavailable until ~30th August
|
Posted: Mon May 21, 2007 4:06 pm Post subject: |
|
|
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 |
|
 |
corrupt
Joined: 29 Dec 2004 Posts: 2397
|
Posted: Mon May 21, 2007 4:30 pm Post subject: |
|
|
| JGR wrote: | | Slightly revised. | Thanks . 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 |
|
 |
JGR
Joined: 15 Jun 2006 Posts: 52 Location: Unavailable until ~30th August
|
Posted: Mon May 21, 2007 4:37 pm Post subject: |
|
|
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 |
|
 |
corrupt
Joined: 29 Dec 2004 Posts: 2397
|
Posted: Mon May 21, 2007 4:48 pm Post subject: |
|
|
Fair enough. Thanks . Here's a modified version of the sample script that uses #Include files for SetCallbackFunction and GetDeRefInteger for fun | 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 |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 5887
|
|
| Back to top |
|
 |
JGR
Joined: 15 Jun 2006 Posts: 52 Location: Unavailable until ~30th August
|
Posted: Tue May 22, 2007 7:44 pm Post subject: |
|
|
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 |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 5887
|
Posted: Tue May 22, 2007 7:54 pm Post subject: |
|
|
| 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!
 |
|
| Back to top |
|
 |
Sean
Joined: 12 Feb 2007 Posts: 1338
|
Posted: Wed May 23, 2007 12:06 am Post subject: |
|
|
| 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 |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 5887
|
Posted: Wed May 23, 2007 3:44 pm Post subject: |
|
|
| 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.  |
|
| Back to top |
|
 |
Sean
Joined: 12 Feb 2007 Posts: 1338
|
Posted: Wed May 23, 2007 5:24 pm Post subject: |
|
|
| 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 |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 5887
|
Posted: Wed May 23, 2007 5:35 pm Post subject: |
|
|
| Sean wrote: | | I got a concrete idea about how to handle this, so leave only experiment. |
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.
 |
|
| Back to top |
|
 |
Joy2DWorld
Joined: 04 Dec 2006 Posts: 422 Location: Galil, Israel
|
Posted: Thu May 24, 2007 1:38 am Post subject: |
|
|
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 |
|
 |
Sean
Joined: 12 Feb 2007 Posts: 1338
|
Posted: Thu May 24, 2007 2:31 am Post subject: |
|
|
| 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 |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4016 Location: Pittsburgh
|
Posted: Thu May 24, 2007 3:41 am Post subject: |
|
|
| 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 |
|
 |
Joy2DWorld
Joined: 04 Dec 2006 Posts: 422 Location: Galil, Israel
|
Posted: Thu May 24, 2007 4:40 am Post subject: |
|
|
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 |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|