@@ -42,19 +42,15 @@ impl DynamicLibrary {
42
42
/// Lazily open a dynamic library. When passed None it gives a
43
43
/// handle to the calling process
44
44
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
+ }
49
52
}
50
53
}
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 ( ) )
58
54
}
59
55
}
60
56
@@ -74,6 +70,7 @@ impl DynamicLibrary {
74
70
}
75
71
76
72
#[ test]
73
+ #[ ignore( cfg( windows) ) ]
77
74
priv fn test_loading_cosine ( ) {
78
75
// The math library does not need to be loaded since it is already
79
76
// statically linked in
@@ -106,13 +103,20 @@ priv fn test_loading_cosine () {
106
103
#[ cfg( target_os = "freebsd" ) ]
107
104
mod dl {
108
105
use libc;
106
+ use path;
109
107
use ptr;
110
108
use str;
111
109
use task;
112
110
use result:: * ;
113
111
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 )
116
120
}
117
121
118
122
pub fn check_for_errors_in < T > ( f : & fn ( ) ->T ) -> Result < T , ~str > {
@@ -159,11 +163,22 @@ mod dl {
159
163
mod dl {
160
164
use os;
161
165
use libc;
166
+ use path;
167
+ use ptr;
168
+ use str;
162
169
use task;
163
170
use result:: * ;
164
171
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
167
182
}
168
183
169
184
pub fn check_for_errors_in < T > ( f : & fn ( ) ->T ) -> Result < T , ~str > {
@@ -192,7 +207,9 @@ mod dl {
192
207
#[ link_name = "kernel32" ]
193
208
extern "stdcall" {
194
209
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 ;
196
213
fn GetProcAddress ( handle : * libc:: c_void , name : * libc:: c_char ) -> * libc:: c_void ;
197
214
fn FreeLibrary ( handle : * libc:: c_void ) ;
198
215
}
0 commit comments