@@ -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) ) ]
7774priv 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" ) ]
107104mod 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 {
159163mod 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 }
0 commit comments