Author Topic: API Scripting Helper Library  (Read 2509 times)

JRS

  • Guest
API Scripting Helper Library
« on: December 16, 2010, 03:45:52 PM »
I started building an INCLUDE file of API/Library helper functions to help make scripting of API services easier. This will be an on going project as I expand on the GTK-Server definitions and test their use.

Code: Text
  1. ' ScriptBasic API helper functions and declarations
  2.  
  3. ' INTERFACE
  4. DECLARE SUB DLL ALIAS "_gtk" LIB "gtk-server"
  5. DECLARE SUB VARPTR ALIAS "varptr" LIB "gtk-server"
  6.  
  7. ' Convert standard function calls to a space delimited command string
  8. FUNCTION _DLL(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9)
  9. LOCAL arg_str
  10.   arg_str = STR(a0) & " " & STR(a1) & " " & STR(a2) & " " & STR(a3) & " " & STR(a4) & " " & STR(a5) & " " & STR(a6) & " " & STR(a7) & " " & STR(a8) & " " & STR(a9)
  11.   _DLL = DLL(arg_str)
  12. END FUNCTION
  13.  
  14. ' Returns a data pointer address of a variable or element in a structure as a numeric string value. (Note: VARPTR is SB variable specific)
  15. FUNCTION STRPTR(mem_addr)
  16. LOCAL tmp_ptr
  17.   tmp_ptr = 0
  18.   DLL("strncpy " & VARPTR(tmp_ptr) & " " & mem_addr & " 4")
  19.   STRPTR = tmp_ptr
  20. END FUNCTION
  21.  
  22. ' Returns a ScriptBasic string variable given the memory address in numeric text format of a null terminated string variable or element of a structure.
  23. FUNCTION GETSTR(mem_addr)
  24. LOCAL tmp_ptr
  25.   tmp_ptr = 0
  26.   DLL("strncpy " & VARPTR(tmp_ptr) & " " & mem_addr & " 4")
  27.   tmp_ptr = DLL("strdup " & tmp_ptr)
  28.   GETSTR = tmp_ptr
  29. END FUNCTION
  30.  
  31. ' Returns a string of data given the memory address of a data pointer and the length in bytes requested.
  32. FUNCTION GETMEM(mem_addr, bytes)
  33. LOCAL tmp_ptr, tmp_data
  34.   tmp_ptr = 0
  35.   DLL("strncpy " & VARPTR(tmp_ptr) & " " & mem_addr & " 4")
  36.   tmp_data = 0
  37.   tmp_data = DLL("strncpy " & VARPTR(tmp_data) & " " & tmp_ptr & " " & bytes)
  38.   GETMEM = tmp_data
  39. END FUNCTION
  40.  
  41. ' Convert a string to a hex representation of it.
  42. FUNCTION Str2Hex(str_arg)
  43.   LOCAL str_len, pos, hex_str
  44.   str_len = LEN(str_arg)
  45.   IF str_len THEN
  46.     FOR pos = 1 TO str_len
  47.       hex_str &= RIGHT("0" & HEX(ASC(MID(str_arg, pos, 1))),2)
  48.     NEXT
  49.   ELSE
  50.     hex_str = ""
  51.   END IF
  52.   Str2Hex = hex_str
  53. END FUNCTION
  54.  
« Last Edit: December 16, 2010, 08:15:55 PM by JRS »