Add an example to the dynlib module doc
Following a quesiton on Gitter about loading libraries dynamically based upon some runtime parameter, I noticed there's no documentation on the `dynlib` module for something that's quite common.
This commit is contained in:
parent
fc22627dbd
commit
9e0e099cc0
1 changed files with 43 additions and 0 deletions
|
|
@ -10,6 +10,49 @@
|
||||||
## This module implements the ability to access symbols from shared
|
## This module implements the ability to access symbols from shared
|
||||||
## libraries. On POSIX this uses the ``dlsym`` mechanism, on
|
## libraries. On POSIX this uses the ``dlsym`` mechanism, on
|
||||||
## Windows ``LoadLibrary``.
|
## Windows ``LoadLibrary``.
|
||||||
|
##
|
||||||
|
## Examples
|
||||||
|
## --------
|
||||||
|
##
|
||||||
|
## Loading a simple C function
|
||||||
|
## ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
##
|
||||||
|
## The following example demonstrates loading a function called 'greet'
|
||||||
|
## from a library that is determined at runtime based upon a language choice.
|
||||||
|
## If the library fails to load or the function 'greet' is not found,
|
||||||
|
## it quits with a failure error code.
|
||||||
|
##
|
||||||
|
## .. code-block::nim
|
||||||
|
##
|
||||||
|
## import dynlib
|
||||||
|
##
|
||||||
|
## type
|
||||||
|
## greetFunction = proc(): cstring {.gcsafe, stdcall.}
|
||||||
|
##
|
||||||
|
## let lang = stdin.readLine()
|
||||||
|
##
|
||||||
|
## let lib = case lang
|
||||||
|
## of "french":
|
||||||
|
## loadLib("french.dll")
|
||||||
|
## else:
|
||||||
|
## loadLib("english.dll")
|
||||||
|
##
|
||||||
|
## if lib == nil:
|
||||||
|
## echo "Error loading library"
|
||||||
|
## quit(QuitFailure)
|
||||||
|
##
|
||||||
|
## let greet = cast[greetFunction](lib.symAddr("greet"))
|
||||||
|
##
|
||||||
|
## if greet == nil:
|
||||||
|
## echo "Error loading 'greet' function from library"
|
||||||
|
## quit(QuitFailure)
|
||||||
|
##
|
||||||
|
## let greeting = greet()
|
||||||
|
##
|
||||||
|
## echo greeting
|
||||||
|
##
|
||||||
|
## unloadLib(lib)
|
||||||
|
##
|
||||||
|
|
||||||
import strutils
|
import strutils
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue