Skip to content

Commit 4c90b98

Browse files
committed
auto merge of #7131 : Blei/rust/windows-dynamic-lib, r=graydon
The code compiles and runs under windows now, but I couldn't look up any symbol from the current executable (dlopen(NULL)), and calling looked up external function handles doesn't seem to work correctly under windows. This the beginning of a fix for #7095.
2 parents 4bf074c + c7013ba commit 4c90b98

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

src/libstd/unstable/dynamic_lib.rs

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,15 @@ impl DynamicLibrary {
4242
/// Lazily open a dynamic library. When passed None it gives a
4343
/// handle to the calling process
4444
pub fn open(filename: Option<&path::Path>) -> Result<DynamicLibrary, ~str> {
45-
let open_wrapper = |raw_ptr| {
46-
do dl::check_for_errors_in {
47-
unsafe {
48-
DynamicLibrary { handle: dl::open(raw_ptr) }
45+
do dl::check_for_errors_in {
46+
unsafe {
47+
DynamicLibrary { handle:
48+
match filename {
49+
Some(name) => dl::open_external(name),
50+
None => dl::open_internal()
51+
}
4952
}
5053
}
51-
};
52-
53-
match filename {
54-
Some(name) => do name.to_str().as_c_str |raw_name| {
55-
open_wrapper(raw_name)
56-
},
57-
None => open_wrapper(ptr::null())
5854
}
5955
}
6056

@@ -74,6 +70,7 @@ impl DynamicLibrary {
7470
}
7571

7672
#[test]
73+
#[ignore(cfg(windows))]
7774
priv fn test_loading_cosine () {
7875
// The math library does not need to be loaded since it is already
7976
// statically linked in
@@ -106,13 +103,20 @@ priv fn test_loading_cosine () {
106103
#[cfg(target_os = "freebsd")]
107104
mod dl {
108105
use libc;
106+
use path;
109107
use ptr;
110108
use str;
111109
use task;
112110
use result::*;
113111

114-
pub unsafe fn open(filename: *libc::c_char) -> *libc::c_void {
115-
dlopen(filename, Lazy as libc::c_int)
112+
pub unsafe fn open_external(filename: &path::Path) -> *libc::c_void {
113+
do filename.to_str().as_c_str |raw_name| {
114+
dlopen(raw_name, Lazy as libc::c_int)
115+
}
116+
}
117+
118+
pub unsafe fn open_internal() -> *libc::c_void {
119+
dlopen(ptr::null(), Lazy as libc::c_int)
116120
}
117121

118122
pub fn check_for_errors_in<T>(f: &fn()->T) -> Result<T, ~str> {
@@ -159,11 +163,22 @@ mod dl {
159163
mod dl {
160164
use os;
161165
use libc;
166+
use path;
167+
use ptr;
168+
use str;
162169
use task;
163170
use result::*;
164171

165-
pub unsafe fn open(filename: *libc::c_char) -> *libc::c_void {
166-
LoadLibrary(filename)
172+
pub unsafe fn open_external(filename: &path::Path) -> *libc::c_void {
173+
do os::win32::as_utf16_p(filename.to_str()) |raw_name| {
174+
LoadLibraryW(raw_name)
175+
}
176+
}
177+
178+
pub unsafe fn open_internal() -> *libc::c_void {
179+
let mut handle = ptr::null();
180+
GetModuleHandleExW(0 as libc::DWORD, ptr::null(), &handle as **libc::c_void);
181+
handle
167182
}
168183

169184
pub fn check_for_errors_in<T>(f: &fn()->T) -> Result<T, ~str> {
@@ -192,7 +207,9 @@ mod dl {
192207
#[link_name = "kernel32"]
193208
extern "stdcall" {
194209
fn SetLastError(error: u32);
195-
fn LoadLibrary(name: *libc::c_char) -> *libc::c_void;
210+
fn LoadLibraryW(name: *u16) -> *libc::c_void;
211+
fn GetModuleHandleExW(dwFlags: libc::DWORD, name: *u16,
212+
handle: **libc::c_void) -> *libc::c_void;
196213
fn GetProcAddress(handle: *libc::c_void, name: *libc::c_char) -> *libc::c_void;
197214
fn FreeLibrary(handle: *libc::c_void);
198215
}

src/libstd/unstable/mod.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ use task;
1818

1919
pub mod at_exit;
2020

21-
// Currently only works for *NIXes
22-
#[cfg(target_os = "linux")]
23-
#[cfg(target_os = "android")]
24-
#[cfg(target_os = "macos")]
25-
#[cfg(target_os = "freebsd")]
2621
pub mod dynamic_lib;
2722

2823
pub mod global;

0 commit comments

Comments
 (0)