use std::path::Path;
pub struct DynamicLibrary {
- handle: *mut u8
+ handle: *mut u8,
}
impl Drop for DynamicLibrary {
fn drop(&mut self) {
- unsafe {
- dl::close(self.handle)
- }
+ unsafe { dl::close(self.handle) }
}
}
// run.
match maybe_library {
Err(err) => Err(err),
- Ok(handle) => Ok(DynamicLibrary { handle })
+ Ok(handle) => Ok(DynamicLibrary { handle }),
}
}
// the destructor does not run.
match maybe_symbol_value {
Err(err) => Err(err),
- Ok(symbol_value) => Ok(symbol_value as *mut T)
+ Ok(symbol_value) => Ok(symbol_value as *mut T),
}
}
}
#[cfg(unix)]
mod dl {
- use std::ffi::{CStr, OsStr, CString};
+ use std::ffi::{CStr, CString, OsStr};
use std::os::unix::prelude::*;
use std::ptr;
use std::str;
pub(super) fn open(filename: Option<&OsStr>) -> Result<*mut u8, String> {
- check_for_errors_in(|| {
- unsafe {
- match filename {
- Some(filename) => open_external(filename),
- None => open_internal(),
- }
+ check_for_errors_in(|| unsafe {
+ match filename {
+ Some(filename) => open_external(filename),
+ None => open_internal(),
}
})
}
}
fn check_for_errors_in<T, F>(f: F) -> Result<T, String>
- where F: FnOnce() -> T,
+ where
+ F: FnOnce() -> T,
{
use std::sync::{Mutex, Once};
static INIT: Once = Once::new();
handle: *mut u8,
symbol: *const libc::c_char,
) -> Result<*mut u8, String> {
- check_for_errors_in(|| {
- libc::dlsym(handle as *mut libc::c_void, symbol) as *mut u8
- })
+ check_for_errors_in(|| libc::dlsym(handle as *mut libc::c_void, symbol) as *mut u8)
}
+
pub(super) unsafe fn close(handle: *mut u8) {
- libc::dlclose(handle as *mut libc::c_void); ()
+ libc::dlclose(handle as *mut libc::c_void);
}
}
use std::os::windows::prelude::*;
use std::ptr;
- use libc::{c_uint, c_void, c_char};
-
- type DWORD = u32;
- type HMODULE = *mut u8;
- type BOOL = i32;
- type LPCWSTR = *const u16;
- type LPCSTR = *const i8;
-
- extern "system" {
- fn SetThreadErrorMode(dwNewMode: DWORD,
- lpOldMode: *mut DWORD) -> c_uint;
- fn LoadLibraryW(name: LPCWSTR) -> HMODULE;
- fn GetModuleHandleExW(dwFlags: DWORD,
- name: LPCWSTR,
- handle: *mut HMODULE) -> BOOL;
- fn GetProcAddress(handle: HMODULE,
- name: LPCSTR) -> *mut c_void;
- fn FreeLibrary(handle: HMODULE) -> BOOL;
- }
+ use winapi::shared::minwindef::HMODULE;
+ use winapi::um::errhandlingapi::SetThreadErrorMode;
+ use winapi::um::libloaderapi::{FreeLibrary, GetModuleHandleExW, GetProcAddress, LoadLibraryW};
+ use winapi::um::winbase::SEM_FAILCRITICALERRORS;
pub(super) fn open(filename: Option<&OsStr>) -> Result<*mut u8, String> {
// disable "dll load failed" error dialog.
let prev_error_mode = unsafe {
- // SEM_FAILCRITICALERRORS 0x01
- let new_error_mode = 1;
+ let new_error_mode = SEM_FAILCRITICALERRORS;
let mut prev_error_mode = 0;
- let result = SetThreadErrorMode(new_error_mode,
- &mut prev_error_mode);
+ let result = SetThreadErrorMode(new_error_mode, &mut prev_error_mode);
if result == 0 {
- return Err(io::Error::last_os_error().to_string())
+ return Err(io::Error::last_os_error().to_string());
}
prev_error_mode
};
let result = match filename {
Some(filename) => {
- let filename_str: Vec<_> =
- filename.encode_wide().chain(Some(0)).collect();
- let result = unsafe {
- LoadLibraryW(filename_str.as_ptr())
- };
+ let filename_str: Vec<_> = filename.encode_wide().chain(Some(0)).collect();
+ let result = unsafe { LoadLibraryW(filename_str.as_ptr()) } as *mut u8;
ptr_result(result)
}
None => {
let mut handle = ptr::null_mut();
- let succeeded = unsafe {
- GetModuleHandleExW(0 as DWORD, ptr::null(), &mut handle)
- };
+ let succeeded = unsafe { GetModuleHandleExW(0, ptr::null(), &mut handle) };
if succeeded == 0 {
Err(io::Error::last_os_error().to_string())
} else {
pub(super) unsafe fn symbol(
handle: *mut u8,
- symbol: *const c_char,
+ symbol: *const libc::c_char,
) -> Result<*mut u8, String> {
let ptr = GetProcAddress(handle as HMODULE, symbol) as *mut u8;
ptr_result(ptr)
}
fn ptr_result<T>(ptr: *mut T) -> Result<*mut T, String> {
- if ptr.is_null() {
- Err(io::Error::last_os_error().to_string())
- } else {
- Ok(ptr)
- }
+ if ptr.is_null() { Err(io::Error::last_os_error().to_string()) } else { Ok(ptr) }
}
}