# Typelib: a C/C++ type/value manipulation library, and dynamic function calls

**URL:** <https://www.ruby-forum.com/t/typelib-a-c-c-type-value-manipulation-library-and-dynamic-function-calls/129641>\
**Category:** Ruby\
**Created:** [February 20, 2008, 10:12am UTC](https://www.ruby-forum.com/t/typelib-a-c-c-type-value-manipulation-library-and-dynamic-function-calls/129641 "2008-02-20T10:12:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sylvain\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ab992/32.png) [@Sylvain\_J](https://www.ruby-forum.com/u/Sylvain_J)\
**Post date:** [February 20, 2008, 10:12am UTC](https://www.ruby-forum.com/t/typelib-a-c-c-type-value-manipulation-library-and-dynamic-function-calls/129641/1 "2008-02-20T10:12:10Z")

</div>

Typelib 1.0rc1 is out !  
Author: Sylvain J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Project page: [typelib download | SourceForge.net](http://sourceforge.net/projects/typelib)  
Documentation:  
C++ library: [typeLib: Typelib: a C++ type and value introspection library](http://typelib.sourceforge.net/html/)  
Ruby bindings: [http://typelib.sourceforge.net/html/ruby](http://typelib.sourceforge.net/html/ruby)

Typelib is a C++ library which allows introspection on data types  
and data values. Its value model is based on the C type model. The  
library allows to load definition from various type description files  
(including plain C), build types programmatically, create and manipulate  
values defined using these types.

The Ruby bindings to the C++ Typelib library allow to represent and  
easily manipulate C types and in-memory values from within Ruby. Based  
on that capability, the bindings offer a nice way to dynamically  
interface with C shared libraries. The dynamic function calls are using  
the dyncall library, to get rid of the many Ruby/DL bugs and  
limitations, and most importantly its lack of maintainership. The  
dyncall source code is provided in the Typelib source packages.

== Example

Letâ€˜s assume that a C dynamic library called libtest.so contains a  
function with the following prototype: int test\_function(SimpleStruct\*).  
The SimpleStruct type is defined in a test\_header.h file which looks  
like this:

typedef struct SimpleStruct {  
int a;  
double d;  
} SimpleStruct;

Using Typelib, a Ruby script can interface with this library this way:

require ‘typelib’  
include Typelib

# Load all types defined in the C file my\_header.h

registry = Registry.import(‘test\_header.h’)

# Get a Library object for the target shared library, and get

# a function handle in this library. +registry+ is used as the

# type registry to access functions in this library

lib = Library.open(“libtest.so”, registry)

# This searches the test\_function in libtest.so. Note that MyStruct

# must be

# already defined in +registry+ (this is the case here). See the

# documentation of Typelib::Function for the various ways to define

# ruby/C

# function bindings.

test\_function = lib.find(‘test\_function’).  
returns(‘int’).  
with\_arguments(‘MyStruct\*’)

# Get the Ruby description of the MyStruct type

my\_struct = registry.get(‘MyStruct’)

# Create an uninitialized MyStruct parameter

arg = my\_struct.new  
arg.a = 10  
arg.b = 20.35498

# and call the function, getting the integer return value

result = test\_function[struct]

# It is also possible to use named parameters

struct = my\_struct.new :a =\> 10, :b =\> 20

# … or to even create the structure argument on the fly

result = test\_function[:a =\> 10, :b =\> 20]

# In fact, the value of this MyStruct argument is not used by

# test\_function:

# This argument is used as a buffer for output values. This can be

# set up

# in Typelib with:

test\_function = registry.find(‘test\_function’).  
returns(‘int’).  
returns(‘MyStruct\*’)  
result, struct = test\_function[]

# If the value was both used by +test\_function+ and modified by it,

# we

# would have used the following:

test\_function = registry.find(‘test\_function’).  
returns(‘int’).  
modifies(‘MyStruct\*’)  
result, arg = test\_function[arg]

# … which can be useful if you want Typelib to build the object

# on-the-fly

result, struct = test\_function[:a =\> 10, :b =\> 20]

# Then, getting the returned values is as simple as

puts struct.a  
puts struct.b

Enjoy !

---

<div class="post-metadata">

**Author:** ![Sylvain\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ab992/32.png) [@Sylvain\_J](https://www.ruby-forum.com/u/Sylvain_J)\
**Post date:** [February 20, 2008, 4:39pm UTC](https://www.ruby-forum.com/t/typelib-a-c-c-type-value-manipulation-library-and-dynamic-function-calls/129641/2 "2008-02-20T16:39:52Z")

</div>

Amazing!

I have to inspect it, but i was going to do exactly same thing ! 🙂

I heope it’s as good as it looks
