2 * PROJECT: ReactOS Win32 Base API
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/win32/kernel32/mem/procmem.c
5 * PURPOSE: Handles virtual memory APIs
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
9 /* INCLUDES ******************************************************************/
16 /* FUNCTIONS *****************************************************************/
23 ReadProcessMemory(IN HANDLE hProcess
,
24 IN LPCVOID lpBaseAddress
,
27 OUT LPDWORD lpNumberOfBytesRead
)
32 Status
= NtReadVirtualMemory(hProcess
,
37 if (!NT_SUCCESS(Status
))
40 SetLastErrorByStatus (Status
);
53 WriteProcessMemory(IN HANDLE hProcess
,
54 IN LPVOID lpBaseAddress
,
57 OUT SIZE_T
*lpNumberOfBytesWritten
)
65 /* Set parameters for protect call */
69 /* Check the current status */
70 Status
= NtProtectVirtualMemory(hProcess
,
73 PAGE_EXECUTE_READWRITE
,
75 if (NT_SUCCESS(Status
))
77 /* Check if we are unprotecting */
78 UnProtect
= OldValue
& (PAGE_READWRITE
|
80 PAGE_EXECUTE_READWRITE
|
81 PAGE_EXECUTE_WRITECOPY
) ? FALSE
: TRUE
;
84 /* Set the new protection */
85 Status
= NtProtectVirtualMemory(hProcess
,
91 /* Write the memory */
92 Status
= NtWriteVirtualMemory(hProcess
,
96 lpNumberOfBytesWritten
);
97 if (!NT_SUCCESS(Status
))
100 SetLastErrorByStatus(Status
);
105 NtFlushInstructionCache(hProcess
, lpBaseAddress
, nSize
);
110 /* Check if we were read only */
111 if ((OldValue
& PAGE_NOACCESS
) || (OldValue
& PAGE_READONLY
))
113 /* Restore protection and fail */
114 NtProtectVirtualMemory(hProcess
,
119 SetLastErrorByStatus(STATUS_ACCESS_VIOLATION
);
123 /* Otherwise, do the write */
124 Status
= NtWriteVirtualMemory(hProcess
,
128 lpNumberOfBytesWritten
);
130 /* And restore the protection */
131 NtProtectVirtualMemory(hProcess
,
136 if (!NT_SUCCESS(Status
))
139 SetLastErrorByStatus(STATUS_ACCESS_VIOLATION
);
144 NtFlushInstructionCache(hProcess
, lpBaseAddress
, nSize
);
151 SetLastErrorByStatus(Status
);