Skip to content

Add WindwowsString/WindowsFilePath support wrt AFPP #198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 1 addition & 28 deletions System/Win32/DLL.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -31,76 +31,49 @@ module System.Win32.DLL
, lOAD_WITH_ALTERED_SEARCH_PATH
) where

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

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

##include "windows_cconv.h"

#include <windows.h>

disableThreadLibraryCalls :: HMODULE -> IO ()
disableThreadLibraryCalls hmod =
failIfFalse_ "DisableThreadLibraryCalls" $ c_DisableThreadLibraryCalls hmod
foreign import WINDOWS_CCONV unsafe "windows.h DisableThreadLibraryCalls"
c_DisableThreadLibraryCalls :: HMODULE -> IO Bool

freeLibrary :: HMODULE -> IO ()
freeLibrary hmod =
failIfFalse_ "FreeLibrary" $ c_FreeLibrary hmod
foreign import WINDOWS_CCONV unsafe "windows.h FreeLibrary"
c_FreeLibrary :: HMODULE -> IO Bool

getModuleFileName :: HMODULE -> IO String
getModuleFileName hmod =
allocaArray 512 $ \ c_str -> do
failIfFalse_ "GetModuleFileName" $ c_GetModuleFileName hmod c_str 512
peekTString c_str
foreign import WINDOWS_CCONV unsafe "windows.h GetModuleFileNameW"
c_GetModuleFileName :: HMODULE -> LPTSTR -> Int -> IO Bool

getModuleHandle :: Maybe String -> IO HMODULE
getModuleHandle mb_name =
maybeWith withTString mb_name $ \ c_name ->
failIfNull "GetModuleHandle" $ c_GetModuleHandle c_name
foreign import WINDOWS_CCONV unsafe "windows.h GetModuleHandleW"
c_GetModuleHandle :: LPCTSTR -> IO HMODULE

getProcAddress :: HMODULE -> String -> IO Addr
getProcAddress hmod procname =
withCAString procname $ \ c_procname ->
failIfNull "GetProcAddress" $ c_GetProcAddress hmod c_procname

foreign import WINDOWS_CCONV unsafe "windows.h GetProcAddress"
c_GetProcAddress :: HMODULE -> LPCSTR -> IO Addr

loadLibrary :: String -> IO HINSTANCE
loadLibrary name =
withTString name $ \ c_name ->
failIfNull "LoadLibrary" $ c_LoadLibrary c_name
foreign import WINDOWS_CCONV unsafe "windows.h LoadLibraryW"
c_LoadLibrary :: LPCTSTR -> IO HINSTANCE

type LoadLibraryFlags = DWORD

#{enum LoadLibraryFlags,
, lOAD_LIBRARY_AS_DATAFILE = LOAD_LIBRARY_AS_DATAFILE
, lOAD_WITH_ALTERED_SEARCH_PATH = LOAD_WITH_ALTERED_SEARCH_PATH
}

loadLibraryEx :: String -> HANDLE -> LoadLibraryFlags -> IO HINSTANCE
loadLibraryEx name h flags =
withTString name $ \ c_name ->
failIfNull "LoadLibraryEx" $ c_LoadLibraryEx c_name h flags
foreign import WINDOWS_CCONV unsafe "windows.h LoadLibraryExW"
c_LoadLibraryEx :: LPCTSTR -> HANDLE -> LoadLibraryFlags -> IO HINSTANCE

setDllDirectory :: Maybe String -> IO ()
setDllDirectory name =
maybeWith withTString name $ \ c_name ->
failIfFalse_ (unwords ["SetDllDirectory", fromMaybe "NULL" name]) $ c_SetDllDirectory c_name

foreign import WINDOWS_CCONV unsafe "windows.h SetDllDirectoryW"
c_SetDllDirectory :: LPTSTR -> IO BOOL
57 changes: 57 additions & 0 deletions System/Win32/DLL/Internal.hsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#if __GLASGOW_HASKELL__ >= 709
{-# LANGUAGE Safe #-}
#else
{-# LANGUAGE Trustworthy #-}
#endif
-----------------------------------------------------------------------------
-- |
-- Module : System.Win32.DLL.Internal
-- Copyright : (c) Alastair Reid, 1997-2003
-- License : BSD-style (see the file libraries/base/LICENSE)
--
-- Maintainer : Esa Ilari Vuokko <[email protected]>
-- Stability : provisional
-- Portability : portable
--
-- A collection of FFI declarations for interfacing with Win32.
--
-----------------------------------------------------------------------------

module System.Win32.DLL.Internal where

import System.Win32.Types

##include "windows_cconv.h"

#include <windows.h>

foreign import WINDOWS_CCONV unsafe "windows.h DisableThreadLibraryCalls"
c_DisableThreadLibraryCalls :: HMODULE -> IO Bool

foreign import WINDOWS_CCONV unsafe "windows.h FreeLibrary"
c_FreeLibrary :: HMODULE -> IO Bool

foreign import WINDOWS_CCONV unsafe "windows.h GetModuleFileNameW"
c_GetModuleFileName :: HMODULE -> LPTSTR -> Int -> IO Bool

foreign import WINDOWS_CCONV unsafe "windows.h GetModuleHandleW"
c_GetModuleHandle :: LPCTSTR -> IO HMODULE

foreign import WINDOWS_CCONV unsafe "windows.h GetProcAddress"
c_GetProcAddress :: HMODULE -> LPCSTR -> IO Addr

foreign import WINDOWS_CCONV unsafe "windows.h LoadLibraryW"
c_LoadLibrary :: LPCTSTR -> IO HINSTANCE

type LoadLibraryFlags = DWORD

#{enum LoadLibraryFlags,
, lOAD_LIBRARY_AS_DATAFILE = LOAD_LIBRARY_AS_DATAFILE
, lOAD_WITH_ALTERED_SEARCH_PATH = LOAD_WITH_ALTERED_SEARCH_PATH
}

foreign import WINDOWS_CCONV unsafe "windows.h LoadLibraryExW"
c_LoadLibraryEx :: LPCTSTR -> HANDLE -> LoadLibraryFlags -> IO HINSTANCE

foreign import WINDOWS_CCONV unsafe "windows.h SetDllDirectoryW"
c_SetDllDirectory :: LPTSTR -> IO BOOL
62 changes: 3 additions & 59 deletions System/Win32/DebugApi.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -70,29 +70,18 @@ module System.Win32.DebugApi
, outputDebugString
) where

import System.Win32.DebugApi.Internal
import Control.Exception( bracket_ )
import Data.Word ( Word8, Word32 )
import Foreign ( Ptr, nullPtr, ForeignPtr, mallocForeignPtrBytes
, peekByteOff, plusPtr, allocaBytes, castPtr, poke
, withForeignPtr, Storable, sizeOf, peek, pokeByteOff )
import System.IO ( fixIO )
import System.Win32.Types ( HANDLE, BOOL, WORD, DWORD, failIf_, failWith
, getLastError, failIf, LPTSTR, withTString )
import System.Win32.Types ( WORD, DWORD, failIf_, failWith
, getLastError, failIf, withTString )

##include "windows_cconv.h"
#include "windows.h"

type PID = DWORD
type TID = DWORD
type DebugEventId = (PID, TID)
type ForeignAddress = Word32

type PHANDLE = Ptr ()
type THANDLE = Ptr ()

type ThreadInfo = (THANDLE, ForeignAddress, ForeignAddress) -- handle to thread, thread local, thread start
type ImageInfo = (HANDLE, ForeignAddress, DWORD, DWORD, ForeignAddress)
type ExceptionInfo = (Bool, Bool, ForeignAddress) -- First chance, continuable, address


data Exception
Expand Down Expand Up @@ -416,48 +405,3 @@ modifyThreadContext t a = withThreadContext t $ makeModThreadContext a
outputDebugString :: String -> IO ()
outputDebugString s = withTString s $ \c_s -> c_OutputDebugString c_s

--------------------------------------------------------------------------
-- Raw imports

foreign import WINDOWS_CCONV "windows.h SuspendThread"
c_SuspendThread :: THANDLE -> IO DWORD

foreign import WINDOWS_CCONV "windows.h ResumeThread"
c_ResumeThread :: THANDLE -> IO DWORD

foreign import WINDOWS_CCONV "windows.h WaitForDebugEvent"
c_WaitForDebugEvent :: Ptr () -> DWORD -> IO BOOL

foreign import WINDOWS_CCONV "windows.h ContinueDebugEvent"
c_ContinueDebugEvent :: DWORD -> DWORD -> DWORD -> IO BOOL

foreign import WINDOWS_CCONV "windows.h DebugActiveProcess"
c_DebugActiveProcess :: DWORD -> IO Bool

-- Windows XP
-- foreign import WINDOWS_CCONV "windows.h DebugActiveProcessStop"
-- c_DebugActiveProcessStop :: DWORD -> IO Bool

foreign import WINDOWS_CCONV "windows.h ReadProcessMemory" c_ReadProcessMemory ::
PHANDLE -> Ptr () -> Ptr Word8 -> DWORD -> Ptr DWORD -> IO BOOL

foreign import WINDOWS_CCONV "windows.h WriteProcessMemory" c_WriteProcessMemory ::
PHANDLE -> Ptr () -> Ptr Word8 -> DWORD -> Ptr DWORD -> IO BOOL

foreign import WINDOWS_CCONV "windows.h GetThreadContext"
c_GetThreadContext :: THANDLE -> Ptr () -> IO BOOL

foreign import WINDOWS_CCONV "windows.h SetThreadContext"
c_SetThreadContext :: THANDLE -> Ptr () -> IO BOOL

--foreign import WINDOWS_CCONV "windows.h GetThreadId"
-- c_GetThreadId :: THANDLE -> IO TID

foreign import WINDOWS_CCONV "windows.h OutputDebugStringW"
c_OutputDebugString :: LPTSTR -> IO ()

foreign import WINDOWS_CCONV "windows.h IsDebuggerPresent"
isDebuggerPresent :: IO BOOL

foreign import WINDOWS_CCONV "windows.h DebugBreak"
debugBreak :: IO ()
80 changes: 80 additions & 0 deletions System/Win32/DebugApi/Internal.hsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
-----------------------------------------------------------------------------
-- |
-- Module : System.Win32.WindowsString.DebugApi.Internal
-- Copyright : (c) Esa Ilari Vuokko, 2006
-- License : BSD-style (see the file LICENSE)
--
-- Maintainer : Esa Ilari Vuokko <[email protected]>
-- Stability : provisional
-- Portability : portable
--
-- A collection of FFI declarations for using Windows DebugApi.
--
-----------------------------------------------------------------------------

module System.Win32.DebugApi.Internal where

import Data.Word ( Word8, Word32 )
import Foreign ( Ptr )
import System.Win32.Types ( BOOL, DWORD, HANDLE, LPTSTR )

##include "windows_cconv.h"
#include "windows.h"

type PID = DWORD
type TID = DWORD
type DebugEventId = (PID, TID)
type ForeignAddress = Word32

type PHANDLE = Ptr ()
type THANDLE = Ptr ()

type ThreadInfo = (THANDLE, ForeignAddress, ForeignAddress) -- handle to thread, thread local, thread start
type ImageInfo = (HANDLE, ForeignAddress, DWORD, DWORD, ForeignAddress)
type ExceptionInfo = (Bool, Bool, ForeignAddress) -- First chance, continuable, address

--------------------------------------------------------------------------
-- Raw imports

foreign import WINDOWS_CCONV "windows.h SuspendThread"
c_SuspendThread :: THANDLE -> IO DWORD

foreign import WINDOWS_CCONV "windows.h ResumeThread"
c_ResumeThread :: THANDLE -> IO DWORD

foreign import WINDOWS_CCONV "windows.h WaitForDebugEvent"
c_WaitForDebugEvent :: Ptr () -> DWORD -> IO BOOL

foreign import WINDOWS_CCONV "windows.h ContinueDebugEvent"
c_ContinueDebugEvent :: DWORD -> DWORD -> DWORD -> IO BOOL

foreign import WINDOWS_CCONV "windows.h DebugActiveProcess"
c_DebugActiveProcess :: DWORD -> IO Bool

-- Windows XP
-- foreign import WINDOWS_CCONV "windows.h DebugActiveProcessStop"
-- c_DebugActiveProcessStop :: DWORD -> IO Bool

foreign import WINDOWS_CCONV "windows.h ReadProcessMemory" c_ReadProcessMemory ::
PHANDLE -> Ptr () -> Ptr Word8 -> DWORD -> Ptr DWORD -> IO BOOL

foreign import WINDOWS_CCONV "windows.h WriteProcessMemory" c_WriteProcessMemory ::
PHANDLE -> Ptr () -> Ptr Word8 -> DWORD -> Ptr DWORD -> IO BOOL

foreign import WINDOWS_CCONV "windows.h GetThreadContext"
c_GetThreadContext :: THANDLE -> Ptr () -> IO BOOL

foreign import WINDOWS_CCONV "windows.h SetThreadContext"
c_SetThreadContext :: THANDLE -> Ptr () -> IO BOOL

--foreign import WINDOWS_CCONV "windows.h GetThreadId"
-- c_GetThreadId :: THANDLE -> IO TID

foreign import WINDOWS_CCONV "windows.h OutputDebugStringW"
c_OutputDebugString :: LPTSTR -> IO ()

foreign import WINDOWS_CCONV "windows.h IsDebuggerPresent"
isDebuggerPresent :: IO BOOL

foreign import WINDOWS_CCONV "windows.h DebugBreak"
debugBreak :: IO ()
Loading