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.
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 139 140 141 142 |
# File 'ext/ffi_c/DynamicLibrary.c', line 112
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.
161 162 163 164 165 166 167 |
# File 'ext/ffi_c/DynamicLibrary.c', line 161
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.
98 99 100 101 102 |
# File 'ext/ffi_c/DynamicLibrary.c', line 98
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.
144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'ext/ffi_c/DynamicLibrary.c', line 144
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.
144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'ext/ffi_c/DynamicLibrary.c', line 144
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.
144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'ext/ffi_c/DynamicLibrary.c', line 144
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.
161 162 163 164 165 166 167 |
# File 'ext/ffi_c/DynamicLibrary.c', line 161
static VALUE
library_dlerror(VALUE self)
{
char errmsg[1024];
dl_error(errmsg, sizeof(errmsg));
return rb_tainted_str_new2(errmsg);
}
|