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

{-# LINE 2 "System\\Win32\\DLL.hsc" #-}
{-# LANGUAGE Safe #-}

{-# LINE 6 "System\\Win32\\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.DLL
    ( disableThreadLibraryCalls
    , freeLibrary
    , getModuleFileName
    , getModuleHandle
    , getProcAddress
    , loadLibrary
    , loadLibraryEx
    , setDllDirectory
    , LoadLibraryFlags
    , lOAD_LIBRARY_AS_DATAFILE
    , lOAD_WITH_ALTERED_SEARCH_PATH
    ) where

import System.Win32.DLL.Internal
import System.Win32.Types

import Foreign
import Foreign.C
import Data.Maybe (fromMaybe)

disableThreadLibraryCalls :: HMODULE -> IO ()
disableThreadLibraryCalls :: HMODULE -> IO ()
disableThreadLibraryCalls HMODULE
hmod =
  String -> IO Bool -> IO ()
failIfFalse_ String
"DisableThreadLibraryCalls" (IO Bool -> IO ()) -> IO Bool -> IO ()
forall a b. (a -> b) -> a -> b
$ HMODULE -> IO Bool
c_DisableThreadLibraryCalls HMODULE
hmod

freeLibrary :: HMODULE -> IO ()
freeLibrary :: HMODULE -> IO ()
freeLibrary HMODULE
hmod =
  String -> IO Bool -> IO ()
failIfFalse_ String
"FreeLibrary" (IO Bool -> IO ()) -> IO Bool -> IO ()
forall a b. (a -> b) -> a -> b
$ HMODULE -> IO Bool
c_FreeLibrary HMODULE
hmod

getModuleFileName :: HMODULE -> IO String
getModuleFileName :: HMODULE -> IO String
getModuleFileName HMODULE
hmod =
  Int -> (Ptr TCHAR -> IO String) -> IO String
forall a b. Storable a => Int -> (Ptr a -> IO b) -> IO b
allocaArray Int
512 ((Ptr TCHAR -> IO String) -> IO String)
-> (Ptr TCHAR -> IO String) -> IO String
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 String
peekTString Ptr TCHAR
c_str

getModuleHandle :: Maybe String -> IO HMODULE
getModuleHandle :: Maybe String -> IO HMODULE
getModuleHandle Maybe String
mb_name =
  (String -> (Ptr TCHAR -> IO HMODULE) -> IO HMODULE)
-> Maybe String -> (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 String -> (Ptr TCHAR -> IO HMODULE) -> IO HMODULE
forall a. String -> (Ptr TCHAR -> IO a) -> IO a
withTString Maybe String
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

getProcAddress :: HMODULE -> String -> IO Addr
getProcAddress :: HMODULE -> String -> IO HMODULE
getProcAddress HMODULE
hmod String
procname =
  String -> (CString -> IO HMODULE) -> IO HMODULE
forall a. String -> (CString -> IO a) -> IO a
withCAString String
procname ((CString -> IO HMODULE) -> IO HMODULE)
-> (CString -> IO HMODULE) -> IO HMODULE
forall a b. (a -> b) -> a -> b
$ \ CString
c_procname ->
  String -> IO HMODULE -> IO HMODULE
forall a. String -> IO (Ptr a) -> IO (Ptr a)
failIfNull String
"GetProcAddress" (IO HMODULE -> IO HMODULE) -> IO HMODULE -> IO HMODULE
forall a b. (a -> b) -> a -> b
$ HMODULE -> CString -> IO HMODULE
c_GetProcAddress HMODULE
hmod CString
c_procname

loadLibrary :: String -> IO HMODULE
loadLibrary :: String -> IO HMODULE
loadLibrary String
name =
  String -> (Ptr TCHAR -> IO HMODULE) -> IO HMODULE
forall a. String -> (Ptr TCHAR -> IO a) -> IO a
withTString String
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 :: String -> HANDLE -> LoadLibraryFlags -> IO HMODULE
loadLibraryEx :: String -> HMODULE -> LoadLibraryFlags -> IO HMODULE
loadLibraryEx String
name HMODULE
h LoadLibraryFlags
flags =
  String -> (Ptr TCHAR -> IO HMODULE) -> IO HMODULE
forall a. String -> (Ptr TCHAR -> IO a) -> IO a
withTString String
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 String -> IO ()
setDllDirectory :: Maybe String -> IO ()
setDllDirectory Maybe String
name =
  (String -> (Ptr TCHAR -> IO ()) -> IO ())
-> Maybe String -> (Ptr TCHAR -> IO ()) -> IO ()
forall a b c.
(a -> (Ptr b -> IO c) -> IO c)
-> Maybe a -> (Ptr b -> IO c) -> IO c
maybeWith String -> (Ptr TCHAR -> IO ()) -> IO ()
forall a. String -> (Ptr TCHAR -> IO a) -> IO a
withTString Maybe String
name ((Ptr TCHAR -> IO ()) -> IO ()) -> (Ptr TCHAR -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ Ptr TCHAR
c_name ->
  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
name]) (IO Bool -> IO ()) -> IO Bool -> IO ()
forall a b. (a -> b) -> a -> b
$ Ptr TCHAR -> IO Bool
c_SetDllDirectory Ptr TCHAR
c_name