Class: FFI::DynamicLibrary

Inherits:
Object
  • Object
show all
Defined in:
ext/ffi_c/DynamicLibrary.c

Defined Under Namespace

Classes: Symbol

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(libname, libflags) ⇒ FFI::DynamicLibrary

A new DynamicLibrary instance.

Parameters:

  • libname (String)

    name of library to open

  • libflags (Fixnum)

    flags for library to open

Raises:

  • (LoadError)

    if libname cannot be opened



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'ext/ffi_c/DynamicLibrary.c', line 113

static VALUE
library_initialize(VALUE self, VALUE libname, VALUE libflags)
{
    Library* library;
    int flags;

    Check_Type(libflags, T_FIXNUM);

    Data_Get_Struct(self, Library, library);
    flags = libflags != Qnil ? NUM2UINT(libflags) : 0;
    
    library->handle = dl_open(libname != Qnil ? StringValueCStr(libname) : NULL, flags);
    if (library->handle == NULL) {
        char errmsg[1024];
        dl_error(errmsg, sizeof(errmsg));
        rb_raise(rb_eLoadError, "Could not open library '%s': %s",
                libname != Qnil ? StringValueCStr(libname) : "[current process]",
                errmsg);
    }
#ifdef __CYGWIN__
    // On Cygwin 1.7.17 "dlsym(dlopen(0,0), 'getpid')" fails. (dlerror: "No such process")
    // As a workaround we can use "dlsym(RTLD_DEFAULT, 'getpid')" instead.
    // Since 0 == RTLD_DEFAULT we won't call dl_close later.
    if (libname == Qnil) {
        dl_close(library->handle);
        library->handle = RTLD_DEFAULT;
    }
#endif
    rb_iv_set(self, "@name", libname != Qnil ? libname : rb_str_new2("[current process]"));
    return self;
}

Instance Attribute Details

#nameObject (readonly)

Class Method Details

.last_errorString

Returns library's last error string

Returns:

  • (String)

    library's last error string



162
163
164
165
166
167
168
# File 'ext/ffi_c/DynamicLibrary.c', line 162

static VALUE
library_dlerror(VALUE self)
{
    char errmsg[1024];
    dl_error(errmsg, sizeof(errmsg));
    return rb_tainted_str_new2(errmsg);
}

.open(libname, libflags) ⇒ FFI::DynamicLibrary

Open a library.

Parameters:

  • libname (String)

    name of library to open

  • libflags (Fixnum)

    flags for library to open

Returns:

Raises:

  • (LoadError)

    if libname cannot be opened



99
100
101
102
103
# File 'ext/ffi_c/DynamicLibrary.c', line 99

static VALUE
library_open(VALUE klass, VALUE libname, VALUE libflags)
{
    return library_initialize(library_allocate(klass), libname, libflags);
}

Instance Method Details

#find_function(name) ⇒ FFI::DynamicLibrary::Symbol

Returns library function symbol

Parameters:

  • name (String)

    library function's name

Returns:



145
146
147
148
149
150
151
152
153
154
155
156
# File 'ext/ffi_c/DynamicLibrary.c', line 145

static VALUE
library_dlsym(VALUE self, VALUE name)
{
    Library* library;
    void* address = NULL;
    Check_Type(name, T_STRING);

    Data_Get_Struct(self, Library, library);
    address = dl_sym(library->handle, StringValueCStr(name));
    
    return address != NULL ? symbol_new(self, address, name) : Qnil;
}

#find_symbol(name) ⇒ FFI::DynamicLibrary::Symbol

Returns library symbol

Parameters:

  • name (String)

    library symbol's name

Returns:



145
146
147
148
149
150
151
152
153
154
155
156
# File 'ext/ffi_c/DynamicLibrary.c', line 145

static VALUE
library_dlsym(VALUE self, VALUE name)
{
    Library* library;
    void* address = NULL;
    Check_Type(name, T_STRING);

    Data_Get_Struct(self, Library, library);
    address = dl_sym(library->handle, StringValueCStr(name));
    
    return address != NULL ? symbol_new(self, address, name) : Qnil;
}

#find_variable(name) ⇒ FFI::DynamicLibrary::Symbol

Returns library variable symbol

Parameters:

  • name (String)

    library variable's name

Returns:



145
146
147
148
149
150
151
152
153
154
155
156
# File 'ext/ffi_c/DynamicLibrary.c', line 145

static VALUE
library_dlsym(VALUE self, VALUE name)
{
    Library* library;
    void* address = NULL;
    Check_Type(name, T_STRING);

    Data_Get_Struct(self, Library, library);
    address = dl_sym(library->handle, StringValueCStr(name));
    
    return address != NULL ? symbol_new(self, address, name) : Qnil;
}

#last_errorString

Returns library's last error string

Returns:

  • (String)

    library's last error string



162
163
164
165
166
167
168
# File 'ext/ffi_c/DynamicLibrary.c', line 162

static VALUE
library_dlerror(VALUE self)
{
    char errmsg[1024];
    dl_error(errmsg, sizeof(errmsg));
    return rb_tainted_str_new2(errmsg);
}