QScheme provides a way to dynamically load dynamic library, to call functions of that library and to share variables with libraries.
(load-library name)-> <boolean>
Dynamically try to load library. If dynamic linking is successful and the library
contains a function like scm_init_name QScheme will call this
function to initialize the module. (See scm_init_regex in regex.c
for an example)
To call a foreign function, you will first have to declare the foreign function for QScheme.
For example, this is the declaration of the system and printf functions from the libc:
(define printf (make-extfunc *lib* :void "printf" '(:string . :any)))
May I ? 10 hello world It works...
>
You can pass string as argument to a foreign function. The string is passed to the function. The size of the string will not be adjusted automatically. You have to do it with the string functions. For example this is a function to list the content of a file using the stdio functions:
(let ((fd) (buf) (bufsize 256))
(set! fd (fopen file "r"))
(if (not (null-pointer? fd))
(begin
(while
(not (null?
(fgets
(set! buf (make-string bufsize #\space))
bufsize
fd)))
(printf "%s\n" (string-chop buf)))
(fclose fd))
(printf "cannot open file %s" file))))
QScheme is able to access variable defined in dynamically loaded modules (see load-library).
(make-extern-variable libname type name)-> <external-variable>
Create an external variable. libname is a string containing the path
to the dynamically loaded library. The library will be loaded on need. The type
is a keyword as describe in table
|
External string are a little complex because we have many cases:
When referring a string stored in a static character buffer, you need to use the :string-buffer type. In this case the value is just copied in the buffer. Beware that no range check occurs. From QScheme, don't try to assign a string bigger than the static buffer size.
You should look at tstlib.c and tstlib.defs for an example of static and dynamic string.
This is a sample of how to share variable value between C and QScheme. In your C code, you have the following:
void test_func() {
printf("shared_var = %d\n", testvar_w);
}
(define shared-var (make-extern-variable *lib* :int "shared_var")
(define test-func (make-extfunc *lib* :void "test_func" '())
> (test-func)
shared_var = 100
> (display shared-var) (newline)
100
>