{-# LINE 1 "libraries\\Win32\\System\\Win32\\WindowsString\\DLL.hsc" #-}
-----------------------------------------------------------------------------

-- |

-- Module      :  System.Win32.DLL

-- Copyright   :  (c) Alastair Reid, 1997-2003

-- License     :  BSD-style (see the file libraries/base/LICENSE)

--

-- Maintainer  :  Esa Ilari Vuokko <ei@vuokko.info>

-- Stability   :  provisional

-- Portability :  portable

--

-- A collection of FFI declarations for interfacing with Win32.

--

-----------------------------------------------------------------------------


module System.Win32.WindowsString.DLL
    ( module System.Win32.WindowsString.DLL
    , module System.Win32.DLL
    ) where

import System.Win32.DLL hiding
  (   disableThreadLibraryCalls
    , freeLibrary
    , getModuleFileName
    , getModuleHandle
    , getProcAddress
    , loadLibrary
    , loadLibraryEx
    , setDllDirectory
    , lOAD_LIBRARY_AS_DATAFILE
    , lOAD_WITH_ALTERED_SEARCH_PATH
  )
import System.Win32.DLL.Internal
import System.Win32.WindowsString.Types

import Foreign
import Data.Maybe (fromMaybe)
import System.OsString.Windows
import GHC.IO.Encoding.UTF16 ( mkUTF16le )
import GHC.IO.Encoding.Failure ( CodingFailureMode(..) )

getModuleFileName :: HMODULE -> IO WindowsString
getModuleFileName :: HMODULE -> IO WindowsString
getModuleFileName HMODULE
hmod =
  Int -> (Ptr TCHAR -> IO WindowsString) -> IO WindowsString
forall a b. Storable a => Int -> (Ptr a -> IO b) -> IO b
allocaArray Int
512 ((Ptr TCHAR -> IO WindowsString) -> IO WindowsString)
-> (Ptr TCHAR -> IO WindowsString) -> IO WindowsString
forall a b. (a -> b) -> a -> b
$ \ Ptr TCHAR
c_str -> do
  String -> IO Bool -> IO ()
failIfFalse_ String
"GetModuleFileName" (IO Bool -> IO ()) -> IO Bool -> IO ()
forall a b. (a -> b) -> a -> b
$ HMODULE -> Ptr TCHAR -> Int -> IO Bool
c_GetModuleFileName HMODULE
hmod Ptr TCHAR
c_str Int
512
  Ptr TCHAR -> IO WindowsString
peekTString Ptr TCHAR
c_str

getModuleHandle :: Maybe WindowsString -> IO HMODULE
getModuleHandle :: Maybe WindowsString -> IO HMODULE
getModuleHandle Maybe WindowsString
mb_name =
  (WindowsString -> (Ptr TCHAR -> IO HMODULE) -> IO HMODULE)
-> Maybe WindowsString -> (Ptr TCHAR -> IO HMODULE) -> IO HMODULE
forall a b c.
(a -> (Ptr b -> IO c) -> IO c)
-> Maybe a -> (Ptr b -> IO c) -> IO c
maybeWith WindowsString -> (Ptr TCHAR -> IO HMODULE) -> IO HMODULE
forall a. WindowsString -> (Ptr TCHAR -> IO a) -> IO a
withTString Maybe WindowsString
mb_name ((Ptr TCHAR -> IO HMODULE) -> IO HMODULE)
-> (Ptr TCHAR -> IO HMODULE) -> IO HMODULE
forall a b. (a -> b) -> a -> b
$ \ Ptr TCHAR
c_name ->
  String -> IO HMODULE -> IO HMODULE
forall a. String -> IO (Ptr a) -> IO (Ptr a)
failIfNull String
"GetModuleHandle" (IO HMODULE -> IO HMODULE) -> IO HMODULE -> IO HMODULE
forall a b. (a -> b) -> a -> b
$ Ptr TCHAR -> IO HMODULE
c_GetModuleHandle Ptr TCHAR
c_name

loadLibrary :: WindowsString -> IO HMODULE
loadLibrary :: WindowsString -> IO HMODULE
loadLibrary WindowsString
name =
  WindowsString -> (Ptr TCHAR -> IO HMODULE) -> IO HMODULE
forall a. WindowsString -> (Ptr TCHAR -> IO a) -> IO a
withTString WindowsString
name ((Ptr TCHAR -> IO HMODULE) -> IO HMODULE)
-> (Ptr TCHAR -> IO HMODULE) -> IO HMODULE
forall a b. (a -> b) -> a -> b
$ \ Ptr TCHAR
c_name ->
  String -> IO HMODULE -> IO HMODULE
forall a. String -> IO (Ptr a) -> IO (Ptr a)
failIfNull String
"LoadLibrary" (IO HMODULE -> IO HMODULE) -> IO HMODULE -> IO HMODULE
forall a b. (a -> b) -> a -> b
$ Ptr TCHAR -> IO HMODULE
c_LoadLibrary Ptr TCHAR
c_name

loadLibraryEx :: WindowsString -> HANDLE -> LoadLibraryFlags -> IO HMODULE
loadLibraryEx :: WindowsString -> HMODULE -> LoadLibraryFlags -> IO HMODULE
loadLibraryEx WindowsString
name HMODULE
h LoadLibraryFlags
flags =
  WindowsString -> (Ptr TCHAR -> IO HMODULE) -> IO HMODULE
forall a. WindowsString -> (Ptr TCHAR -> IO a) -> IO a
withTString WindowsString
name ((Ptr TCHAR -> IO HMODULE) -> IO HMODULE)
-> (Ptr TCHAR -> IO HMODULE) -> IO HMODULE
forall a b. (a -> b) -> a -> b
$ \ Ptr TCHAR
c_name ->
  String -> IO HMODULE -> IO HMODULE
forall a. String -> IO (Ptr a) -> IO (Ptr a)
failIfNull String
"LoadLibraryEx" (IO HMODULE -> IO HMODULE) -> IO HMODULE -> IO HMODULE
forall a b. (a -> b) -> a -> b
$ Ptr TCHAR -> HMODULE -> LoadLibraryFlags -> IO HMODULE
c_LoadLibraryEx Ptr TCHAR
c_name HMODULE
h LoadLibraryFlags
flags

setDllDirectory :: Maybe WindowsString -> IO ()
setDllDirectory :: Maybe WindowsString -> IO ()
setDllDirectory Maybe WindowsString
name =
  (WindowsString -> (Ptr TCHAR -> IO ()) -> IO ())
-> Maybe WindowsString -> (Ptr TCHAR -> IO ()) -> IO ()
forall a b c.
(a -> (Ptr b -> IO c) -> IO c)
-> Maybe a -> (Ptr b -> IO c) -> IO c
maybeWith WindowsString -> (Ptr TCHAR -> IO ()) -> IO ()
forall a. WindowsString -> (Ptr TCHAR -> IO a) -> IO a
withTString Maybe WindowsString
name ((Ptr TCHAR -> IO ()) -> IO ()) -> (Ptr TCHAR -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ Ptr TCHAR
c_name -> do
    let nameS :: Maybe String
nameS = Maybe WindowsString
name Maybe WindowsString
-> (WindowsString -> Maybe String) -> Maybe String
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (EncodingException -> Maybe String)
-> (String -> Maybe String)
-> Either EncodingException String
-> Maybe String
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Maybe String -> EncodingException -> Maybe String
forall a b. a -> b -> a
const Maybe String
forall a. Maybe a
Nothing) String -> Maybe String
forall a. a -> Maybe a
Just (Either EncodingException String -> Maybe String)
-> (WindowsString -> Either EncodingException String)
-> WindowsString
-> Maybe String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TextEncoding -> WindowsString -> Either EncodingException String
decodeWith (CodingFailureMode -> TextEncoding
mkUTF16le CodingFailureMode
TransliterateCodingFailure)
    String -> IO Bool -> IO ()
failIfFalse_ ([String] -> String
unwords [String
"SetDllDirectory", String -> Maybe String -> String
forall a. a -> Maybe a -> a
fromMaybe String
"NULL" Maybe String
nameS]) (IO Bool -> IO ()) -> IO Bool -> IO ()
forall a b. (a -> b) -> a -> b
$ Ptr TCHAR -> IO Bool
c_SetDllDirectory Ptr TCHAR
c_name