AutoHotkey Community

It is currently May 27th, 2012, 5:14 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: December 24th, 2010, 11:08 am 
Offline

Joined: December 29th, 2007, 9:40 pm
Posts: 142
I am seeing an unexpected result with the following code:

(I actually have two issues, but the latter is prolly just me.?. I'll explain more at end)

Code:
;rcb_test.ahk
;===============================================================================
test_msg2(text_2="") {
   msgbox, % "Registered Function 2 Called: " . text_2   ;%
   return
   }

test_msg2("2 conventional call, not DLLCall...")

aFunc2 := registercallback("test_msg2")
   string2 := "this is testing rcb 2 via dllcall"
   result := dllcall(aFunc2,"str",string2)
   If (ErrorLevel <> 0)   {
      msgbox, % "registercallback 2 was OK.  Got error level of: " . ErrorLevel   . " on use"   ;%
      }
;===============================================================================



The first issue I am seeing is with the RegisterCallback construct. The issue is that the code, as reflected above, yields an A4 error with the DLLCall. If, however, I change the RegisterCallback construct line to:
Code:
aFunc2 := registercallback("test_msg2","",1)

OR

If I remove the default parameter, altering the functions's definition to:
Code:
test_msg2(text_2) {

It works fine and Error_Level is not set on the DLLCall.

This kicked my arse a bit as I was thinking that an error with the RegisterCallback construct would have been able to be trapped with ErrorLevel, but that is not the case...

Is this considered to be normal operation?

...

Ok, the 2nd query: Why is the function printing the parameter text just file for a conventional call, but only prints the pointer when called via DLLCall? Is the DLLCall or RegisterCallback constructs in error?

Do I need to have the function be smarter and coded to react differently if called via a callback vs. a conventional call?

This seems to be a bit of an bug as I would expect the function's code to behave the same in both cases.?.

Thoughts? Plz advise. TIA. -t

_________________
When replying, please feel free to address me as Tod. My AHK.net site...


Last edited by TodWulff on December 25th, 2010, 10:07 pm, edited 2 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 24th, 2010, 8:30 pm 
Offline

Joined: December 29th, 2007, 9:40 pm
Posts: 142
I am seeing this behavior on both 1.0.48.05 and 1.0.90.00_ANSI.

Just a bit more info to consider.

-t

_________________
When replying, please feel free to address me as Tod. My AHK.net site...


Report this post
Top
 Profile  
Reply with quote  
PostPosted: December 25th, 2010, 2:10 pm 
TodWulff wrote:
The issue is that the code, as reflected above, yields an A4 error with the DLLCall.
You passed too many parameter. ParamCount: "If entirely omitted, it defaults to the number of mandatory parameters in the definition of FunctionName."
Quote:
This kicked my arse a bit as I was thinking that an error with the RegisterCallback construct would have been able to be trapped with ErrorLevel, but that is not the case...
If RegisterCallback returned a callback address, it succeeded in doing what you told it to do.
Quote:
Ok, the 2nd query: Why is the function printing the parameter text just file for a conventional call, but only prints the pointer when called via DLLCall?
If you call the function directly, the callback has nothing to do with it. For callbacks, "All incoming parameters are integers between 0 and 4294967295."
Quote:
This seems to be a bit of an bug as I would expect the function's code to behave the same in both cases.?.
How should the callback know what type of value you gave it?


Report this post
Top
  
Reply with quote  
PostPosted: December 25th, 2010, 8:44 pm 
Offline

Joined: December 29th, 2007, 9:40 pm
Posts: 142
Anonymous wrote:
TodWulff wrote:
The issue is that the code, as reflected above, yields an A4 error with the DLLCall.
You passed too many parameter. ParamCount: "If entirely omitted, it defaults to the number of mandatory parameters in the definition of FunctionName."
Touche'. /me is a bit thick skulled at times. Thank you.

Quote:
Quote:
This kicked my arse a bit as I was thinking that an error with the RegisterCallback construct would have been able to be trapped with ErrorLevel, but that is not the case...
If RegisterCallback returned a callback address, it succeeded in doing what you told it to do.
Yes, I figured this out here. Thank you.

Quote:
Quote:
Ok, the 2nd query: Why is the function printing the parameter text just file for a conventional call, but only prints the pointer when called via DLLCall?
If you call the function directly, the callback has nothing to do with it. For callbacks, "All incoming parameters are integers between 0 and 4294967295."
Quote:
This seems to be a bit of an bug as I would expect the function's code to behave the same in both cases.?.
How should the callback know what type of value you gave it?
Touche' (once again...). That cranium maximus on top of muh neck is embarrassing at times. :oops:

One approach I can think of is to have some code at the beginning of the function to test the parameter to see if it is a pointer and pull in the data conditionally, and then run the function as intended.

Another is to have optional parameters that are used only by the callback, and have some specific code therein (or a call to a tool function) to pull in the data.

Any other suggestions (or best practices) that would allow one to leverage a function so that it can be used both conventionally as well as via a callback?

In closing, thank you for helping to draw my attention to the obvious. Sometimes one can get too close to what they are working on such that they lose objectivity. I think I suffered that fate here.

If you celebrate: Merry Christmas. If not, have a great day! :)

-t

_________________
When replying, please feel free to address me as Tod. My AHK.net site...


Report this post
Top
 Profile  
Reply with quote  
PostPosted: December 25th, 2010, 10:06 pm 
Offline

Joined: December 29th, 2007, 9:40 pm
Posts: 142
This approach is functional...

Code:
;rcb_test.ahk
;===============================================================================
test_msg2(text_2, pFlag="") {
   if (pFlag <> "") {
      text_2 := DllCall("MulDiv","int",text_2,"int",1,"int",1,"str")  ; from: http://www.autohotkey.com/forum/viewtopic.php?t=27644  TY Lex!
      If (ErrorLevel <> 0)   {
         msgbox, % "On dllcall got ErrorLeve|Result|a_lasterror:" . ErrorLevel . "|" . result . "|" . a_lasterror   ;%
         exitapp
         }
      }
      
   msgbox, % text_2   ;%
   
   return
   }

test_msg2("Testing via conventional call (not DLLCall...).")

aFunc2 := registercallback("test_msg2","Cdecl",2)
   If (aFunc2 = "")   {
      msgbox, % "Fegistercallback 2 fail" ;%
      exitapp
      }
   string2 := "Now testing via DLLCall..."
   result := dllcall(aFunc2,"str",string2,"int",1,"Cdecl")
   If (ErrorLevel <> 0)   {
      msgbox, % "On dllcall got ErrorLeve|Result|a_lasterror:" . ErrorLevel . "|" . result . "|" . a_lasterror   ;%
      exitapp
      }
;===============================================================================

_________________
When replying, please feel free to address me as Tod. My AHK.net site...


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Google [Bot], XstatyK, Yahoo [Bot] and 72 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group