summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorDominik Picheta <dominikpicheta@googlemail.com>2018-03-21 12:23:44 +0000
committerGitHub <noreply@github.com>2018-03-21 12:23:44 +0000
commit43c636c449eda62b5abaf38ad309d19433eaa4bd (patch)
tree9b56c660509aaddaf593f796858552d5e4d011cb
parentfc22627dbd9209e58fcc3f95e83650c9c0d8495c (diff)
parent9e0e099cc0cc9f9eef6489a1fc0d4e6041036f27 (diff)
downloadNim-43c636c449eda62b5abaf38ad309d19433eaa4bd.tar.gz
Merge pull request #7388 from euantorano/patch-1
Add an example to the `dynlib` module doc
-rw-r--r--lib/pure/dynlib.nim43
1 files changed, 43 insertions, 0 deletions
diff --git a/lib/pure/dynlib.nim b/lib/pure/dynlib.nim
index fda41dadb..ec6cbb232 100644
--- a/lib/pure/dynlib.nim
+++ b/lib/pure/dynlib.nim
@@ -10,6 +10,49 @@
 ## This module implements the ability to access symbols from shared
 ## libraries. On POSIX this uses the ``dlsym`` mechanism, on
 ## 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