Class: FFI::DynamicLibrary
- Inherits:
-
Object
- Object
- FFI::DynamicLibrary
- Defined in:
- ext/ffi_c/DynamicLibrary.c
Defined Under Namespace
Classes: Symbol
Instance Attribute Summary collapse
- #name ⇒ Object readonly
Class Method Summary collapse
-
.last_error ⇒ String
Library's last error string.
-
.open(libname, libflags) ⇒ FFI::DynamicLibrary
Open a library.
Instance Method Summary collapse
-
#find_function(name) ⇒ FFI::DynamicLibrary::Symbol
Library function symbol.
-
#find_symbol(name) ⇒ FFI::DynamicLibrary::Symbol
Library symbol.
-
#find_variable(name) ⇒ FFI::DynamicLibrary::Symbol
Library variable symbol.
-
#initialize(libname, libflags) ⇒ FFI::DynamicLibrary
constructor
A new DynamicLibrary instance.
-
#last_error ⇒ String
Library's last error string.
Constructor Details
#initialize(libname, libflags) ⇒ FFI::DynamicLibrary
A new DynamicLibrary instance.
108 109 110 111 112 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 |
# File 'ext/ffi_c/DynamicLibrary.c', line 108
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
#name ⇒ Object (readonly)
Class Method Details
.last_error ⇒ String
Returns library's last error string.
157 158 159 160 161 162 163 |
# File 'ext/ffi_c/DynamicLibrary.c', line 157
static VALUE
library_dlerror(VALUE self)
{
char errmsg[1024];
dl_error(errmsg, sizeof(errmsg));
return rb_str_new2(errmsg);
}
|
.open(libname, libflags) ⇒ FFI::DynamicLibrary
Open a library.
94 95 96 97 98 |
# File 'ext/ffi_c/DynamicLibrary.c', line 94
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.
140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'ext/ffi_c/DynamicLibrary.c', line 140
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.
140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'ext/ffi_c/DynamicLibrary.c', line 140
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.
140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'ext/ffi_c/DynamicLibrary.c', line 140
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_error ⇒ String
Returns library's last error string.
157 158 159 160 161 162 163 |
# File 'ext/ffi_c/DynamicLibrary.c', line 157
static VALUE
library_dlerror(VALUE self)
{
char errmsg[1024];
dl_error(errmsg, sizeof(errmsg));
return rb_str_new2(errmsg);
}
|