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 

NumGet() not working inside a function? [solved]

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Peter



Joined: 30 Dec 2005
Posts: 448

PostPosted: Tue Nov 17, 2009 5:15 am    Post subject: NumGet() not working inside a function? [solved] Reply with quote

When using Numget inside the function CheckBytes, it returns mostly wrong values.
The following testscript fills all bytes in bitList with initValue, but Numget reads some other values.
The GetIntAt() function reads always the correct initValue though.
Is there a way to get it right with NumGet in a function?
Code:
   showEveryByte := true  ; false avoids checking every byte by user
   byteCount := 64  ,  initValue := 123
   GrantedCapacity := VarSetCapacity(bitList, byteCount, initValue)
   CheckBytes(&bitList, GrantedCapacity)      ; test for NumGet in function

   ; If not in a function, NumGet never causes a problem:
   Loop %GrantedCapacity% {
      byteNumber := A_index - 1
      numByte := NumGet(&bitList, byteNumber, "char")
      intByte := GetIntAt(&bitList, byteNumber, 1)
      if (showEveryByte  ||  numByte <> initValue  ||  intByte <> initValue){
         msgbox, 1, Outside: byte %byteNumber%  (V%A_AhkVersion%)
                  , numByte= %numByte% `nintByte= %intByte%  `n(initValue= %initValue%)
         IfMsgBox, Cancel
            break         
      }
   }
   VarSetCapacity(bitList, 0)    ; free bitList
   ExitApp
F9::ExitApp
return

CheckBytes(pBitList, GrantedCapacity){
   ; W/ NumGet in a function, it causes always a problem at some byte(s)
   global showEveryByte, initValue
   
   Loop %GrantedCapacity% {
      byteNumber := A_index - 1
      numByte := NumGet(pBitList, byteNumber, "char")   ; pBitList or *pBitList does NOT work for me
      intByte := GetIntAt(pBitList, byteNumber, 1)
      if (showEveryByte  ||  (numByte <> initValue)  ||  (intByte <> initValue)){
         msgbox, 1, Inside: byte %byteNumber%  (V%A_AhkVersion%)
                  , numByte= %numByte% `nintByte= %intByte%  `n(initValue= %initValue%  baseAddress= %pBitList%)
         IfMsgBox, Cancel
            return
      }
   }
}
GetIntAt(address, offset, size){      ; replacement for NumGet works always fine
   int := 0
   Loop %size% {
      byteNumber := A_index - 1
      int += *(address + offset + byteNumber) << (8 * byteNumber)
   }
   return int
}


Last edited by Peter on Tue Nov 17, 2009 7:14 am; edited 1 time in total
Back to top
View user's profile Send private message
hd0202



Joined: 13 Aug 2006
Posts: 174
Location: Germany

PostPosted: Tue Nov 17, 2009 5:55 am    Post subject: Re: NumGet() not working inside a function? Reply with quote

change the following line without &
Code:
CheckBytes(bitList, GrantedCapacity)

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



Joined: 26 Dec 2005
Posts: 7159

PostPosted: Tue Nov 17, 2009 6:19 am    Post subject: Reply with quote

Hi Peter.. Smile


Code:
CheckBytes(pBitList, GrantedCapacity){
   ; W/ NumGet in a function, it causes always a problem at some byte(s)
   global showEveryByte, initValue
   
   Loop %GrantedCapacity% {
      byteNumber := A_index - 1
      numByte := NumGet(pBitList+0, byteNumber, "char")   ; pBitList or *pBitList does NOT work for me
      intByte := GetIntAt(pBitList, byteNumber, 1)
      if (showEveryByte  ||  (numByte <> initValue)  ||  (intByte <> initValue)){
         msgbox, 1, Inside: byte %byteNumber%  (V%A_AhkVersion%)
                  , numByte= %numByte% `nintByte= %intByte%  `n(initValue= %initValue%  baseAddress= %pBitList%)
         IfMsgBox, Cancel
            return
      }
   }
}
Back to top
View user's profile Send private message
Peter



Joined: 30 Dec 2005
Posts: 448

PostPosted: Tue Nov 17, 2009 7:13 am    Post subject: Reply with quote

Thanks Skan. Nice trick. While I saw the trick, I remembered it is mentioned in some way in AHK Help as well.
I guess struggling too long with it, that I overlooked that option.
But actually confusing/weird. Suppose you have to do the same with other functions.
I.e. using Sin(x) outside a function and Sin(x+0) inside a function Confused .
I guess the possibility to choose between NumGet(var) and NumGet(&var) is to blame. ( Idea Better get rid of that choice?)
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 7159

PostPosted: Tue Nov 17, 2009 7:26 am    Post subject: Reply with quote

Using pVar+0 was side-effect and Laszlo informed it to Mr.Chris, IIRC.

Peter wrote:
I guess the possibility to choose between NumGet(var) and NumGet(&var) is to blame. ( Idea Better get rid of that choice?)


I am unable to comment as I am already accustomed to these current methods.
There was a huge discussion: Choose naming and syntax for built-in Extract/InsertInteger
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
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