Call a function in a shared library: Difference between revisions

m
m (→‎{{header|Phix}}: added syntax colouring, marked p2js incompatible)
Line 1,421:
 
=={{header|Ol}}==
 
Note: "strdup" is existent shared function, "strduX" is not existent.
Simplest case. Will produce memory leak, because no C "free" function called for dupped string. Useful when no "free" function call required.
<lang scheme>
(import (otus ffi))
 
(define self (load-dynamic-library #f))
(define strdup (or
(self type-string "strdup" type-string))
(lambda (str)
(list->string (string->list str)))))
 
(print (strduXstrdup "Hello World!"))
(define strduX (or
</lang>
(self type-string "strduX" type-string)
 
(lambda (str)
A bit complex case. No memory leaks, because "free" function called for dupped string.
(list->string (string->list str)))))
<lang scheme>
(import (otus ffi))
 
(define self (load-dynamic-library #f))
(define strduX (orstrdup
(let ((strdup (self type-stringvptr "strduXstrdup" type-string))
(free (self fft-void "free" type-vptr)))
(lambda (str)
(let*((dupped (strdup str))
(result (vptr->string dupped)))
(free dupped)
result))))
 
(print (strdup "Hello World!"))
(print (strduX "Hello World!"))
</lang>
 
{{Out}}
<pre>
Hello World!
Hello World!
</pre>