on master help sign in
Merge pull request #773 from suhorng/fix/master/catch-windows-tryloadlib-exception
ed969b3Edwin Brady
src/Util/DynamicLinker.hs+6−2
@@ -5,11 +5,12 @@ module Util.DynamicLinker where
5
5
6#ifdef IDRIS_FFI
6#ifdef IDRIS_FFI
7import Foreign.LibFFI
7import Foreign.LibFFI
8−import Foreign.Ptr (nullPtr, FunPtr, nullFunPtr,castPtrToFunPtr)
8+import Foreign.Ptr (Ptr(), nullPtr, FunPtr, nullFunPtr, castPtrToFunPtr)
9import System.Directory
9import System.Directory
10#ifndef WINDOWS
10#ifndef WINDOWS
11import System.Posix.DynamicLinker
11import System.Posix.DynamicLinker
12#else
12#else
13+import qualified Control.Exception as Exception (catch, IOException)
13import System.Win32.DLL
14import System.Win32.DLL
14import System.Win32.Types
15import System.Win32.Types
15type DL = HMODULE
16type DL = HMODULE
@@ -63,10 +64,13 @@ tryLoadFn fn (Lib _ h) = do cFn <- dlsym h fn
63tryLoadLib :: String -> IO (Maybe DynamicLib)
64tryLoadLib :: String -> IO (Maybe DynamicLib)
64tryLoadLib lib = do exactName <- doesFileExist lib
65tryLoadLib lib = do exactName <- doesFileExist lib
65 let filename = if exactName then lib else lib ++ "." ++ hostDynamicLibExt
66 let filename = if exactName then lib else lib ++ "." ++ hostDynamicLibExt
66− handle <- loadLibrary filename
67+ handle <- Exception.catch (loadLibrary filename) nullPtrOnException
67 if handle == nullPtr
68 if handle == nullPtr
68 then return Nothing
69 then return Nothing
69 else return . Just $ Lib lib handle
70 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
70
74
71tryLoadFn :: String -> DynamicLib -> IO (Maybe ForeignFun)
75tryLoadFn :: String -> DynamicLib -> IO (Maybe ForeignFun)
72tryLoadFn fn (Lib _ h) = do cFn <- getProcAddress h fn
76tryLoadFn fn (Lib _ h) = do cFn <- getProcAddress h fn