File: Win32.hs

package info (click to toggle)
haskell-executable-path 0.0.3-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 88 kB
  • sloc: haskell: 421; makefile: 2
file content (55 lines) | stat: -rw-r--r-- 1,397 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

{-# LANGUAGE ForeignFunctionInterface #-}

module System.Environment.Executable.Win32
  ( getExecutablePath  
  , getModulePath
  )
  where

import Data.Bits
import Data.Word
import Data.Int

import Control.Monad

import Foreign
import Foreign.C
import Foreign.Marshal

--import System.Win32
--import System.Win32.DLL

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

foreign import stdcall unsafe "Windows.h GetModuleFileNameW" c_GetModuleFileNameW
  :: HMODULE -> Ptr CWchar -> Word32 -> IO Word32

type HMODULE = Ptr ()

getModulePath :: HMODULE -> IO FilePath
getModulePath = getModulePath' 512

getModulePath' :: Word32 -> HMODULE -> IO FilePath
getModulePath' size hmodule = do
  mpath <- allocaArray0 (fromIntegral size) $ \p -> do
    k <- c_GetModuleFileNameW hmodule p size 
    case k of
      0 -> error "getModulePath: unknown error"
      _ -> if k == size
             then return Nothing
             else liftM Just $ peekCWString p
  case mpath of
    Just path -> return path
    Nothing -> getModulePath' (2*size) hmodule
             
{-
-- | Returns the full path + name of the module.
getModulePath :: HMODULE -> IO FilePath
getModulePath hmodule = getModuleFileName hmodule
-}

getExecutablePath :: IO FilePath  
getExecutablePath = getModulePath nullPtr
  
--------------------------------------------------------------------------------