Ooops
November 25, 2020, 04:56:49 AM

Author Topic: [bb] bank -> pointer and object -> pointer conversion by Koriolis [ 1+ years ago ]  (Read 608 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : bank -> pointer and object -> pointer conversion
Author : Koriolis
Posted : 1+ years ago

Description : Nothing impressive, but a handy trick.

Code :
Code: BlitzBasic
  1. ;.lib "kernel32.dll"
  2. ;ptr_helper__%(a*,b%,c%):"MulDiv"
  3.  
  4.  
  5. ; Converts a bank handle to the memory pointer.
  6. ; Useful to call functions from external DLLs that expect pointers
  7. ; as parameters. Especially if the pointer is allowed to be NULL, because
  8. ; then the usual solution is to have 2 declarations of the same function,
  9. ; one with the said parameter declared with "*", the other with "%".
  10. ; OK that's maybe not very well explained, so if anyone wants to explain
  11. ; it properly, feel free :)
  12. ; WARNING: don't use it to pass a pointer to a funciton if you
  13. ;          need a very very fast call, as there is a slight
  14. ;          overhead (here ptr_helper__ is used a no op, but in
  15. ;          fact it does something and thus consumes takes
  16. ;          a little time to execute; in normal cases this overhead
  17. ;          is simply peanuts)
  18. Function ptr%(b)
  19.         Return ptr_helper__(b, 1, 1)
  20. End Function
  21.  
  22.  
  23.  
  24.  
  25. ; Also the same can be done for types, but then you need a version of the function
  26. ; for each type:
  27.  
  28. Type MyType
  29.         Field a%
  30.         Field b#
  31. End Type
  32.  
  33. Function MyType_ptr%(obj.MyType)
  34.         Return ptr_helper__(obj, 1, 1)
  35. End Function
  36.  
  37.  
  38.  
  39. ; === Example
  40. ; === Not a common case usage, but it shows it works ===
  41. ; === For a real life usage, see this thread http://www.blitzbasic.com/Community/posts.php?topic=31927
  42. Graphics 640, 480, 2
  43. ;.lib "kernel32.dll"
  44. ;RtlMoveMemory(Destination%, Source%, Length%) ; copy memory from [Source, Source+Length[ to [Destination, Destination+Length[
  45.  
  46.  
  47. srcBank% = CreateBank(15)
  48. For i = 0 To 14 ; we fill srcBank
  49.         PokeByte(srcBank, i, 3*i)
  50. Next
  51.  
  52. destBank% = CreateBank(15)
  53.  
  54. ; The way we declared 'RtlMoveMemory', it directly expects pointers (as integers)
  55. ; We'll get them using 'ptr'
  56. RtlMoveMemory(ptr(destBank), ptr(srcBank), 15)
  57.  
  58. ; we check srcBank has been copied into destBank
  59. For i = 0 To 14
  60.         Print PeekByte(destBank, i)
  61. Next
  62.  
  63.  
  64.  
  65. ; OK, it works, now we'll check for types:
  66.  
  67. srcObj.MyType = New MyType
  68. srcObja = 123
  69. srcObj = 1.2345
  70. destObj.MyType = New MyType
  71.  
  72. Print "srcObj=" + Str(srcObj) + ", destObj=" + Str(destObj)
  73.  
  74. ; Now we copy srcObj int dstObj
  75. RtlMoveMemory(MyType_ptr(destObj), MyType_ptr(srcObj), 8) ; Sizeof MyType = 8 bytes
  76.  
  77.  
  78. ; Check that now destObj = srcObj
  79. Print "srcObj=" + Str(srcObj) + ", destObj=" + Str(destObj)


Comments :


Danny(Posted 1+ years ago)

 Thanks!!!!


bytecode77(Posted 1+ years ago)

 - some of the decls are missing (eg. RtlMoveMemory)- {640, 480, 2} is no vaild graphics mode- MAV on ptr_helper


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal