UserLibs.txt 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. New DLL interface spec
  2. ----------------------
  3. There is now a directory in the root blitzpath called 'userlibs'.
  4. By adding DLLs and '.decls' files to this directory, you can extend blitz's command set.
  5. DLLs contain actually library code, while .decls files contain 'declarations' to be added to the
  6. base command set. These declarations describe the functions contained in the Dll.
  7. Format of decls files
  8. ---------------------
  9. Decls files should always start with a '.lib' directive, followed by the quoted name of the DLL to be
  10. loaded, eg:
  11. .lib "mylib.dll"
  12. Following the .lib is a list of function declarations. The syntax of these is identical to
  13. function declarations in Blitz, with the following exceptions:
  14. * No default parameter values are allowed.
  15. * If no function return type is specified, the function is a 'void' function - ie: it returns nothing.
  16. * Instead of object parameters, you can only specify 'void*' parameters using a '*' type tag. Such
  17. parameters can be assigned ANY object or bank, so BE CAREFUL!
  18. * A declaration may be optionally followed by a 'decorated name'. This takes the form of a ':'
  19. followed by a quoted string denoting the decorated name, eg:
  20. MyFunction( text$ ):"_MyFunction@4"
  21. MessageBox%( hwnd,text$,title$,style ):"MessageBoxA"
  22. The decorated name is the name of the function as it appears in the dll, and only needs to be
  23. specified if it is different from the actual function name.
  24. Writing DLLs
  25. ------------
  26. All functions MUST use the _stdcall calling convention.
  27. Floats are passed and returned as per standard C/C++ conventions.
  28. Strings are passed and returned in 'C' format - ie: a pointer to a null-terminated sequence of
  29. characters.
  30. Returned strings must be in a 'static' memory buffer. Once the function returns, this string is
  31. immediately copied into an internal Blitz style string, so its OK to share this buffer between
  32. multiple functions.
  33. Both banks and objects can be passed to functions. The value passed is the address of the first byte
  34. of storage. No information is sent regarding the size or type of memory passed so, again, BE CAREFUL!
  35. Neither Banks or objects can be returned from functions.
  36. Arrays are not supported at all.
  37. VisualC decorates symbols quite heavily! If you are coding in 'C', the necessary stdcall specifier
  38. will cause a '_' to be prepended, and a '@' (followed by the number of bytes passed to the function
  39. - ie: number of parameters*4) to be appended. So, something like: 'void _stdcall MyFunc( int x )'
  40. will end up as '_MyFunc@4'. In C++, the name decoration is even messier! But you can supress it by
  41. using the 'extern "C"' feature (see examples below) so you're just left with the 'C' stdcall mess.
  42. Other languages such as Delphi and Purebasic don't appear to suffer from this feature, but it can be
  43. worthwhile looking through dll symbols if you're having problems. Check out PEview at
  44. http://www.magma.ca/~wjr Open your dll and select 'SECTION .rdata'/'EXPORT address table'
  45. to have a look at the exported symbols your compiler has seen fit to bestow upon your dll.
  46. Example
  47. -------
  48. Ok, here's a little C++ example, as it would appear in VisualC.
  49. First, we write the DLL:
  50. //demo.dll
  51. //
  52. #include <math.h>
  53. #include <string.h>
  54. #include <stdlib.h>
  55. #define BBDECL extern "C" _declspec(dllexport)
  56. #define BBCALL _stdcall
  57. //returns a float and has 6 float parameters
  58. BBDECL float BBCALL VecDistance( float x1,float y1,float z1,float x2,float y2,float z2 ){
  59. float dx=x1-x2,dy=y1-y2,dz=z1-z2;
  60. return sqrtf( dx*dx+dy*dy+dz*dz );
  61. }
  62. //returns a string and has one string parameter
  63. BBDECL const char * BBCALL ShuffleString( const char *str ){
  64. static char *_buf;
  65. int sz=strlen(str);
  66. delete[] _buf;
  67. _buf=new char[ sz+1 ];
  68. strcpy( _buf,str );
  69. for( int k=0;k<sz;++k ){
  70. int n=rand()%sz;
  71. int t=_buf[k];_buf[k]=_buf[n];_buf[n]=t;
  72. }
  73. return _buf;
  74. }
  75. After building this, the resultant 'demo.dll' should be placed in the userlibs directory.
  76. Now, we also need to create a 'demo.decls' file, describing the functions in our dll. This file
  77. is also placed in the userlibs directory:
  78. .lib "demo.dll"
  79. VecDistance#( x1#,y1#,z1#,x2#,y2#,z2# ):"_VecDistance@24"
  80. ShuffleString$( str$ ):"_ShuffleString@4"
  81. ...Voila! The next time Blitz is started up, the new commands appear within the IDE and can be used.