on master help sign in
catch exception thrown by tryLoadLib on Windows

`tryLoadLib` uses `loadLibrary` to load DLL on Windows, which throws `IOException` upon failure such as file not found. Currently we only checks whether the return value is `nullPtr` or not. This commit adds a handler that tries to catch the `IOException` thrown by `loadLibrary` and returns `nullPtr` instead.

commit9f1add7a090442937f6a873559797f5508880e51 authorsuhorng <suhorng@gmail.com> dateThu 9 Jan 2014, 01:42 (13 years ago) parent6808ce5
1 file changed+6−2
src/Util/DynamicLinker.hs+6−2
src/Util/DynamicLinker.hs+6−2
@@ -5,11 +5,12 @@ module Util.DynamicLinker where
55
66#ifdef IDRIS_FFI
77import Foreign.LibFFI
8−import Foreign.Ptr (nullPtr, FunPtr, nullFunPtr,castPtrToFunPtr)
8+import Foreign.Ptr (Ptr(), nullPtr, FunPtr, nullFunPtr, castPtrToFunPtr)
99import System.Directory
1010#ifndef WINDOWS
1111import System.Posix.DynamicLinker
1212#else
13+import qualified Control.Exception as Exception (catch, IOException)
1314import System.Win32.DLL
1415import System.Win32.Types
1516type DL = HMODULE
@@ -63,10 +64,13 @@ tryLoadFn fn (Lib _ h) = do cFn <- dlsym h fn
6364tryLoadLib :: String -> IO (Maybe DynamicLib)
6465tryLoadLib lib = do exactName <- doesFileExist lib
6566 let filename = if exactName then lib else lib ++ "." ++ hostDynamicLibExt
66− handle <- loadLibrary filename
67+ handle <- Exception.catch (loadLibrary filename) nullPtrOnException
6768 if handle == nullPtr
6869 then return Nothing
6970 else return . Just $ Lib lib handle
71+ where nullPtrOnException :: Exception.IOException -> IO DL
72+ nullPtrOnException e = return nullPtr
73+ -- `show e` will however give broken error message
7074
7175tryLoadFn :: String -> DynamicLib -> IO (Maybe ForeignFun)
7276tryLoadFn fn (Lib _ h) = do cFn <- getProcAddress h fn