module System.Win32.Mem where
import System.Win32.Types
import Foreign
import Foreign.C.Types
#include "windows_cconv.h"
data MEMORY_BASIC_INFORMATION = MEMORY_BASIC_INFORMATION
{ mbiBaseAddress :: Addr
, mbiAllocationBase :: Addr
, mbiAllocationProtect :: DWORD
, mbiRegionSize :: SIZE_T
, mbiState :: DWORD
, mbiProtect :: DWORD
, mbiType :: DWORD
} deriving (Show)
instance Storable MEMORY_BASIC_INFORMATION where
sizeOf _ = (48)
alignment = sizeOf
poke buf mbi = do
((\hsc_ptr -> pokeByteOff hsc_ptr 0)) buf (mbiBaseAddress mbi)
((\hsc_ptr -> pokeByteOff hsc_ptr 8)) buf (mbiAllocationBase mbi)
((\hsc_ptr -> pokeByteOff hsc_ptr 16)) buf (mbiAllocationProtect mbi)
((\hsc_ptr -> pokeByteOff hsc_ptr 24)) buf (mbiRegionSize mbi)
((\hsc_ptr -> pokeByteOff hsc_ptr 32)) buf (mbiState mbi)
((\hsc_ptr -> pokeByteOff hsc_ptr 36)) buf (mbiProtect mbi)
((\hsc_ptr -> pokeByteOff hsc_ptr 40)) buf (mbiType mbi)
peek buf = do
baseAddress <- ((\hsc_ptr -> peekByteOff hsc_ptr 0)) buf
allocationBase <- ((\hsc_ptr -> peekByteOff hsc_ptr 8)) buf
allocationProtect <- ((\hsc_ptr -> peekByteOff hsc_ptr 16)) buf
regionSize <- ((\hsc_ptr -> peekByteOff hsc_ptr 24)) buf
state <- ((\hsc_ptr -> peekByteOff hsc_ptr 32)) buf
protect <- ((\hsc_ptr -> peekByteOff hsc_ptr 36)) buf
ty <- ((\hsc_ptr -> peekByteOff hsc_ptr 40)) buf
return $ MEMORY_BASIC_INFORMATION baseAddress allocationBase allocationProtect regionSize state protect ty
copyMemory :: Ptr a -> Ptr a -> DWORD -> IO ()
copyMemory dest src nbytes = copyBytes dest src (fromIntegral nbytes)
moveMemory :: Ptr a -> Ptr a -> DWORD -> IO ()
moveMemory dest src nbytes = moveBytes dest src (fromIntegral nbytes)
fillMemory :: Ptr a -> DWORD -> BYTE -> IO ()
fillMemory dest nbytes val =
memset dest (fromIntegral val) (fromIntegral nbytes)
zeroMemory :: Ptr a -> DWORD -> IO ()
zeroMemory dest nbytes = memset dest 0 (fromIntegral nbytes)
foreign import ccall unsafe "string.h" memset :: Ptr a -> CInt -> CSize -> IO ()
foreign import WINDOWS_CCONV unsafe "windows.h GetProcessHeap"
getProcessHeap :: IO HANDLE
foreign import WINDOWS_CCONV unsafe "windows.h GetProcessHeaps"
getProcessHeaps :: DWORD -> Addr -> IO DWORD
type HGLOBAL = Addr
type GlobalAllocFlags = UINT
gMEM_INVALID_HANDLE :: GlobalAllocFlags
gMEM_INVALID_HANDLE = 32768
gMEM_FIXED :: GlobalAllocFlags
gMEM_FIXED = 0
gMEM_MOVEABLE :: GlobalAllocFlags
gMEM_MOVEABLE = 2
gPTR :: GlobalAllocFlags
gPTR = 64
gHND :: GlobalAllocFlags
gHND = 66
gMEM_DDESHARE :: GlobalAllocFlags
gMEM_DDESHARE = 8192
gMEM_SHARE :: GlobalAllocFlags
gMEM_SHARE = 8192
gMEM_LOWER :: GlobalAllocFlags
gMEM_LOWER = 4096
gMEM_NOCOMPACT :: GlobalAllocFlags
gMEM_NOCOMPACT = 16
gMEM_NODISCARD :: GlobalAllocFlags
gMEM_NODISCARD = 32
gMEM_NOT_BANKED :: GlobalAllocFlags
gMEM_NOT_BANKED = 4096
gMEM_NOTIFY :: GlobalAllocFlags
gMEM_NOTIFY = 16384
gMEM_ZEROINIT :: GlobalAllocFlags
gMEM_ZEROINIT = 64
globalAlloc :: GlobalAllocFlags -> DWORD -> IO HGLOBAL
globalAlloc flags size =
failIfNull "GlobalAlloc" $ c_GlobalAlloc flags size
foreign import WINDOWS_CCONV unsafe "windows.h GlobalAlloc"
c_GlobalAlloc :: GlobalAllocFlags -> DWORD -> IO HGLOBAL
globalFlags :: HGLOBAL -> IO GlobalAllocFlags
globalFlags mem =
failIf (== gMEM_INVALID_HANDLE) "GlobalFlags" $ c_GlobalFlags mem
foreign import WINDOWS_CCONV unsafe "windows.h GlobalFlags"
c_GlobalFlags :: HGLOBAL -> IO GlobalAllocFlags
globalFree :: HGLOBAL -> IO HGLOBAL
globalFree mem =
failIfNull "GlobalFree" $ c_GlobalFree mem
foreign import WINDOWS_CCONV unsafe "windows.h GlobalFree"
c_GlobalFree :: HGLOBAL -> IO HGLOBAL
globalHandle :: Addr -> IO HGLOBAL
globalHandle addr =
failIfNull "GlobalHandle" $ c_GlobalHandle addr
foreign import WINDOWS_CCONV unsafe "windows.h GlobalHandle"
c_GlobalHandle :: Addr -> IO HGLOBAL
globalLock :: HGLOBAL -> IO Addr
globalLock mem =
failIfNull "GlobalLock" $ c_GlobalLock mem
foreign import WINDOWS_CCONV unsafe "windows.h GlobalLock"
c_GlobalLock :: HGLOBAL -> IO Addr
globalReAlloc :: HGLOBAL -> DWORD -> GlobalAllocFlags -> IO HGLOBAL
globalReAlloc mem size flags =
failIfNull "GlobalReAlloc" $ c_GlobalReAlloc mem size flags
foreign import WINDOWS_CCONV unsafe "windows.h GlobalReAlloc"
c_GlobalReAlloc :: HGLOBAL -> DWORD -> GlobalAllocFlags -> IO HGLOBAL
globalSize :: HGLOBAL -> IO DWORD
globalSize mem =
failIfZero "GlobalSize" $ c_GlobalSize mem
foreign import WINDOWS_CCONV unsafe "windows.h GlobalSize"
c_GlobalSize :: HGLOBAL -> IO DWORD
globalUnlock :: HGLOBAL -> IO ()
globalUnlock mem =
failIfFalse_ "GlobalUnlock" $ c_GlobalUnlock mem
foreign import WINDOWS_CCONV unsafe "windows.h GlobalUnlock"
c_GlobalUnlock :: HGLOBAL -> IO Bool
type HeapAllocFlags = DWORD
hEAP_GENERATE_EXCEPTIONS :: HeapAllocFlags
hEAP_GENERATE_EXCEPTIONS = 4
hEAP_NO_SERIALIZE :: HeapAllocFlags
hEAP_NO_SERIALIZE = 1
hEAP_ZERO_MEMORY :: HeapAllocFlags
hEAP_ZERO_MEMORY = 8
heapAlloc :: HANDLE -> HeapAllocFlags -> DWORD -> IO Addr
heapAlloc heap flags size =
failIfNull "HeapAlloc" $ c_HeapAlloc heap flags size
foreign import WINDOWS_CCONV unsafe "windows.h HeapAlloc"
c_HeapAlloc :: HANDLE -> HeapAllocFlags -> DWORD -> IO Addr
heapCompact :: HANDLE -> HeapAllocFlags -> IO UINT
heapCompact heap flags =
failIfZero "HeapCompact" $ c_HeapCompact heap flags
foreign import WINDOWS_CCONV unsafe "windows.h HeapCompact"
c_HeapCompact :: HANDLE -> HeapAllocFlags -> IO UINT
heapCreate :: HeapAllocFlags -> DWORD -> DWORD -> IO HANDLE
heapCreate flags initSize maxSize =
failIfNull "HeapCreate" $ c_HeapCreate flags initSize maxSize
foreign import WINDOWS_CCONV unsafe "windows.h HeapCreate"
c_HeapCreate :: HeapAllocFlags -> DWORD -> DWORD -> IO HANDLE
heapDestroy :: HANDLE -> IO ()
heapDestroy heap =
failIfFalse_ "HeapDestroy" $ c_HeapDestroy heap
foreign import WINDOWS_CCONV unsafe "windows.h HeapDestroy"
c_HeapDestroy :: HANDLE -> IO Bool
heapFree :: HANDLE -> HeapAllocFlags -> Addr -> IO ()
heapFree heap flags addr =
failIfFalse_ "HeapFree" $ c_HeapFree heap flags addr
foreign import WINDOWS_CCONV unsafe "windows.h HeapFree"
c_HeapFree :: HANDLE -> HeapAllocFlags -> Addr -> IO Bool
heapLock :: HANDLE -> IO ()
heapLock heap =
failIfFalse_ "HeapLock" $ c_HeapLock heap
foreign import WINDOWS_CCONV unsafe "windows.h HeapLock"
c_HeapLock :: HANDLE -> IO Bool
heapReAlloc :: HANDLE -> HeapAllocFlags -> Addr -> DWORD -> IO Addr
heapReAlloc heap flags addr size =
failIfNull "HeapReAlloc" $ c_HeapReAlloc heap flags addr size
foreign import WINDOWS_CCONV unsafe "windows.h HeapReAlloc"
c_HeapReAlloc :: HANDLE -> HeapAllocFlags -> Addr -> DWORD -> IO Addr
heapSize :: HANDLE -> HeapAllocFlags -> Addr -> IO DWORD
heapSize heap flags addr =
failIfZero "HeapSize" $ c_HeapSize heap flags addr
foreign import WINDOWS_CCONV unsafe "windows.h HeapSize"
c_HeapSize :: HANDLE -> HeapAllocFlags -> Addr -> IO DWORD
heapUnlock :: HANDLE -> IO ()
heapUnlock heap =
failIfFalse_ "HeapUnlock" $ c_HeapUnlock heap
foreign import WINDOWS_CCONV unsafe "windows.h HeapUnlock"
c_HeapUnlock :: HANDLE -> IO Bool
foreign import WINDOWS_CCONV unsafe "windows.h HeapValidate"
heapValidate :: HANDLE -> HeapAllocFlags -> Addr -> IO Bool
type VirtualAllocFlags = DWORD
mEM_COMMIT :: VirtualAllocFlags
mEM_COMMIT = 4096
mEM_RESERVE :: VirtualAllocFlags
mEM_RESERVE = 8192
type ProtectFlags = DWORD
pAGE_READONLY :: ProtectFlags
pAGE_READONLY = 2
pAGE_READWRITE :: ProtectFlags
pAGE_READWRITE = 4
pAGE_EXECUTE :: ProtectFlags
pAGE_EXECUTE = 16
pAGE_EXECUTE_READ :: ProtectFlags
pAGE_EXECUTE_READ = 32
pAGE_EXECUTE_READWRITE :: ProtectFlags
pAGE_EXECUTE_READWRITE = 64
pAGE_GUARD :: ProtectFlags
pAGE_GUARD = 256
pAGE_NOACCESS :: ProtectFlags
pAGE_NOACCESS = 1
pAGE_NOCACHE :: ProtectFlags
pAGE_NOCACHE = 512
type FreeFlags = DWORD
mEM_DECOMMIT :: FreeFlags
mEM_DECOMMIT = 16384
mEM_RELEASE :: FreeFlags
mEM_RELEASE = 32768
virtualAlloc :: Addr -> DWORD -> VirtualAllocFlags -> ProtectFlags -> IO Addr
virtualAlloc addt size ty flags =
failIfNull "VirtualAlloc" $ c_VirtualAlloc addt size ty flags
foreign import WINDOWS_CCONV unsafe "windows.h VirtualAlloc"
c_VirtualAlloc :: Addr -> DWORD -> DWORD -> DWORD -> IO Addr
virtualFree :: Addr -> DWORD -> FreeFlags -> IO ()
virtualFree addr size flags =
failIfFalse_ "VirtualFree" $ c_VirtualFree addr size flags
foreign import WINDOWS_CCONV unsafe "windows.h VirtualFree"
c_VirtualFree :: Addr -> DWORD -> FreeFlags -> IO Bool
virtualLock :: Addr -> DWORD -> IO ()
virtualLock addr size =
failIfFalse_ "VirtualLock" $ c_VirtualLock addr size
foreign import WINDOWS_CCONV unsafe "windows.h VirtualLock"
c_VirtualLock :: Addr -> DWORD -> IO Bool
virtualProtect :: Addr -> DWORD -> ProtectFlags -> IO ProtectFlags
virtualProtect addr size new_prot =
alloca $ \ p_old -> do
failIfFalse_ "VirtualProtect" $ c_VirtualProtect addr size new_prot p_old
peek p_old
foreign import WINDOWS_CCONV unsafe "windows.h VirtualProtect"
c_VirtualProtect :: Addr -> DWORD -> DWORD -> Ptr DWORD -> IO Bool
virtualProtectEx :: HANDLE -> Addr -> DWORD -> ProtectFlags -> IO ProtectFlags
virtualProtectEx proc addr size new_prot =
alloca $ \ p_old -> do
failIfFalse_ "VirtualProtectEx" $
c_VirtualProtectEx proc addr size new_prot p_old
peek p_old
foreign import WINDOWS_CCONV unsafe "windows.h VirtualProtectEx"
c_VirtualProtectEx :: HANDLE -> Addr -> DWORD -> DWORD -> Ptr DWORD -> IO Bool
virtualQueryEx :: HANDLE -> LPVOID -> Ptr MEMORY_BASIC_INFORMATION -> SIZE_T -> IO DWORD
virtualQueryEx hProcess lpAddress lpBuffer dwLength =
failIfZero "VirtualQueryEx" $ c_VirtualQueryEx hProcess lpAddress lpBuffer dwLength
foreign import WINDOWS_CCONV unsafe "windows.h VirtualQueryEx"
c_VirtualQueryEx :: HANDLE -> LPVOID -> Ptr MEMORY_BASIC_INFORMATION -> SIZE_T -> IO DWORD
virtualUnlock :: Addr -> DWORD -> IO ()
virtualUnlock addr size =
failIfFalse_ "VirtualUnlock" $ c_VirtualUnlock addr size
foreign import WINDOWS_CCONV unsafe "windows.h VirtualUnlock"
c_VirtualUnlock :: Addr -> DWORD -> IO Bool