📄 kernel32_u.cpp
字号:
break;
case FILE_END:
Offset = 0;
break;
default:
Offset = 0;
break;
}
FilePos.CurrentByteOffset.LowPart = Offset + lDistanceToMove;
return NtSetInformationFile((int) hFile,
0,
(int) &FilePos,
(int) sizeof(FilePos),
FilePositionInformation);
}
/* Mutex */
HANDLE __declspec(dllexport) __stdcall
CreateMutexA(LPSECURITY_ATTRIBUTES lpMutexAttributes,
BOOL bInitialOwner,
LPCSTR lpName)
{
LPWSTR NameU = NULL;
HANDLE Handle;
if (lpName) {
NameU=(LPWSTR)malloc((strlen(lpName)+1)*sizeof(WCHAR));
MultiByteToWideChar(CP_ACP, 0, lpName, strlen(lpName)+1,
NameU, strlen(lpName)+1);
}
Handle = CreateMutexW(lpMutexAttributes, bInitialOwner, NameU);
free(NameU);
return Handle;
}
HANDLE __declspec(dllexport) __stdcall
CreateMutexW(LPSECURITY_ATTRIBUTES lpMutexAttributes,
BOOL bInitialOwner,
LPCWSTR lpName)
{
OBJECT_ATTRIBUTES ObjectAttributes;
NTSTATUS Status;
UNICODE_STRING UnicodeName;
HANDLE MutantHandle;
RtlInitUnicodeString(&UnicodeName,(LPWSTR)lpName);
InitializeObjectAttributes(&ObjectAttributes,
(lpName ? &UnicodeName : NULL),
0,
(lpName ? hBaseDir : NULL),
NULL);
if (lpMutexAttributes != NULL) {
ObjectAttributes.SecurityDescriptor
= lpMutexAttributes->lpSecurityDescriptor;
if (lpMutexAttributes->bInheritHandle) {
ObjectAttributes.Attributes |= OBJ_INHERIT;
}
}
Status = NtCreateMutant((int) &MutantHandle,
(int) MUTEX_ALL_ACCESS,
(int) &ObjectAttributes,
(int) bInitialOwner);
if (!NT_SUCCESS(Status)) {
SetLastError(Status);
return NULL;
}
return MutantHandle;
}
HANDLE __declspec(dllexport) __stdcall
OpenMutexA(DWORD dwDesiredAccess,
BOOL bInheritHandle,
LPCSTR lpName)
{
LPWSTR NameU;
HANDLE Handle;
if (!lpName)
{
SetLastError(ERROR_INVALID_PARAMETER);
return NULL;
}
NameU = (LPWSTR)malloc((strlen(lpName)+1)*sizeof(WCHAR));
MultiByteToWideChar(CP_ACP, 0, lpName, strlen(lpName)+1,
NameU, strlen(lpName)+1);
Handle = OpenMutexW(dwDesiredAccess, bInheritHandle, NameU);
free(NameU);
return Handle;
}
HANDLE __declspec(dllexport) __stdcall
OpenMutexW(DWORD dwDesiredAccess,
BOOL bInheritHandle,
LPCWSTR lpName)
{
OBJECT_ATTRIBUTES ObjectAttributes;
UNICODE_STRING Name;
HANDLE Handle;
NTSTATUS Status;
RtlInitUnicodeString(&Name, (LPWSTR)lpName);
InitializeObjectAttributes(&ObjectAttributes,
&Name,
(bInheritHandle ? OBJ_INHERIT : 0),
hBaseDir,
NULL);
Status = NtOpenMutant((int) &Handle,
(ACCESS_MASK)dwDesiredAccess,
(int) &ObjectAttributes);
if (!NT_SUCCESS(Status)) {
SetLastError(Status);
return NULL;
}
return Handle;
}
BOOL __declspec(dllexport) __stdcall
ReleaseMutex(HANDLE hMutex)
{
NTSTATUS Status;
Status = NtReleaseMutant((int) hMutex, 0);
if (!NT_SUCCESS(Status)) {
SetLastError(Status);
return FALSE;
}
return TRUE;
}
/* Section */
HANDLE __declspec(dllexport) __stdcall
CreateFileMapping(
HANDLE hFile,
LPSECURITY_ATTRIBUTES lpAttributes,
DWORD flProtect,
DWORD dwMaximumSizeHigh,
DWORD dwMaximumSizeLow,
LPCSTR lpName
)
{
HANDLE SectionHandle;
OBJECT_ATTRIBUTES ObjectAttributes;
UNICODE_STRING UniString;
LPWSTR NameU=NULL;
LARGE_INTEGER MaximumSize;
NTSTATUS Status;
if (lpName != NULL) {
NameU = (LPWSTR)malloc((strlen(lpName)+1)*sizeof(WCHAR));
MultiByteToWideChar(CP_ACP, 0, lpName, strlen(lpName)+1,
NameU, strlen(lpName)+1);
UniString.Buffer = NameU;
UniString.Length = wcslen(NameU);
ObjectAttributes.ObjectName = &UniString;
}
MaximumSize.u.HighPart = dwMaximumSizeHigh;
MaximumSize.u.LowPart = dwMaximumSizeLow;
Status = NtCreateSection(
(int) &SectionHandle,
(int) SECTION_ALL_ACCESS,
(int) (lpName ? &ObjectAttributes: NULL),
(int) &MaximumSize,
(int) (flProtect & MASK_PAGE_FLAGS),
(int) (flProtect & MASK_SEC_FLAGS),
(int) hFile
);
free(NameU);
if (!NT_SUCCESS(Status)) {
SetLastError(Status);
return NULL;
}
return SectionHandle;
}
LPVOID __declspec(dllexport) __stdcall
MapViewOfFile(
HANDLE hFileMappingObject,
DWORD dwDesiredAccess,
DWORD dwFileOffsetHigh,
DWORD dwFileOffsetLow,
SIZE_T dwNumberOfBytesToMap
)
{
return MapViewOfFileEx(
hFileMappingObject,
dwDesiredAccess,
dwFileOffsetHigh,
dwFileOffsetLow,
dwNumberOfBytesToMap,
NULL
);
}
LPVOID __declspec(dllexport) __stdcall
MapViewOfFileEx(
HANDLE hFileMappingObject,
DWORD dwDesiredAccess,
DWORD dwFileOffsetHigh,
DWORD dwFileOffsetLow,
SIZE_T dwNumberOfBytesToMap,
LPVOID lpBaseAddress
)
{
NTSTATUS Status;
LARGE_INTEGER SectionOffset;
ULONG ViewSize;
ULONG Protect;
LPVOID BaseAddress;
SectionOffset.u.LowPart = dwFileOffsetLow;
SectionOffset.u.HighPart = dwFileOffsetHigh;
if ((dwDesiredAccess & FILE_MAP_WRITE) == FILE_MAP_WRITE)
Protect = PAGE_READWRITE;
else if ((dwDesiredAccess & FILE_MAP_READ) == FILE_MAP_READ)
Protect = PAGE_READONLY;
else if ((dwDesiredAccess & FILE_MAP_ALL_ACCESS) == FILE_MAP_ALL_ACCESS)
Protect = PAGE_READWRITE;
else if ((dwDesiredAccess & FILE_MAP_COPY) == FILE_MAP_COPY)
Protect = PAGE_WRITECOPY;
else
Protect = PAGE_READWRITE;
BaseAddress = lpBaseAddress;
ViewSize = (ULONG) dwNumberOfBytesToMap;
Status = NtMapViewOfSection((int) hFileMappingObject,
0,
(int) &BaseAddress,
0,
(int) dwNumberOfBytesToMap,
(int) &SectionOffset,
(int) (PSIZE_T)&ViewSize,
0,
0,
Protect);
if (!NT_SUCCESS(Status)) {
SetLastError(Status);
return NULL;
}
return BaseAddress;
}
BOOL __declspec(dllexport) __stdcall
UnmapViewOfFile(
LPCVOID lpBaseAddress
)
{
return !NtUnmapViewOfSection(0, (int) (PVOID)lpBaseAddress);
}
/* Semaphore */
HANDLE __declspec(dllexport) __stdcall
CreateSemaphoreA(LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,
LONG lInitialCount,
LONG lMaximumCount,
LPCSTR lpName)
{
LPWSTR NameU = NULL;
HANDLE Handle;
if (lpName) {
NameU=(LPWSTR)malloc((strlen(lpName)+1)*sizeof(WCHAR));
MultiByteToWideChar(CP_ACP, 0, lpName, strlen(lpName)+1,
NameU, strlen(lpName)+1);
}
Handle = CreateSemaphoreW(lpSemaphoreAttributes,
lInitialCount,
lMaximumCount,
NameU);
free(NameU);
return Handle;
}
HANDLE __declspec(dllexport) __stdcall
CreateSemaphoreW(LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,
LONG lInitialCount,
LONG lMaximumCount,
LPCWSTR lpName)
{
OBJECT_ATTRIBUTES ObjectAttributes;
NTSTATUS Status;
UNICODE_STRING UnicodeName;
HANDLE SemaphoreHandle;
RtlInitUnicodeString(&UnicodeName,(LPWSTR) lpName);
InitializeObjectAttributes(&ObjectAttributes,
(lpName ? &UnicodeName : NULL),
0,
(lpName ? hBaseDir : NULL),
NULL);
if (lpSemaphoreAttributes != NULL)
{
ObjectAttributes.SecurityDescriptor = lpSemaphoreAttributes->lpSecurityDescriptor;
if (lpSemaphoreAttributes->bInheritHandle)
{
ObjectAttributes.Attributes |= OBJ_INHERIT;
}
}
Status = NtCreateSemaphore((int) &SemaphoreHandle,
(int) SEMAPHORE_ALL_ACCESS,
(int) &ObjectAttributes,
(int) lInitialCount,
(int) lMaximumCount);
if (!NT_SUCCESS(Status)) {
SetLastError(Status);
return NULL;
}
return SemaphoreHandle;
}
HANDLE __declspec(dllexport) __stdcall
OpenSemaphoreA(DWORD dwDesiredAccess,
BOOL bInheritHandle,
LPCSTR lpName)
{
LPWSTR NameU;
HANDLE Handle;
if (!lpName)
{
SetLastError(ERROR_INVALID_PARAMETER);
return NULL;
}
NameU = (LPWSTR)malloc((strlen(lpName)+1)*sizeof(WCHAR));
MultiByteToWideChar(CP_ACP, 0, lpName, strlen(lpName)+1,
NameU, strlen(lpName)+1);
Handle = OpenSemaphoreW(dwDesiredAccess, bInheritHandle, NameU);
free(NameU);
return Handle;
}
HANDLE __declspec(dllexport) __stdcall
OpenSemaphoreW(DWORD dwDesiredAccess,
BOOL bInheritHandle,
LPCWSTR lpName)
{
OBJECT_ATTRIBUTES ObjectAttributes;
UNICODE_STRING Name;
HANDLE Handle;
NTSTATUS Status;
RtlInitUnicodeString(&Name,(LPWSTR)lpName);
InitializeObjectAttributes(&ObjectAttributes,
&Name,
(bInheritHandle ? OBJ_INHERIT : 0),
hBaseDir,
NULL);
Status = NtOpenSemaphore((int)(&Handle),
(ACCESS_MASK)dwDesiredAccess,
(int)(&ObjectAttributes));
if (!NT_SUCCESS(Status))
{
SetLastError(Status);
return NULL;
}
return Handle;
}
BOOL __declspec(dllexport) __stdcall
ReleaseSemaphore(HANDLE hSemaphore,
LONG lReleaseCount,
LPLONG lpPreviousCount)
{
NTSTATUS Status;
Status = NtReleaseSemaphore((int)(hSemaphore),
(int)(lReleaseCount),
(int)(lpPreviousCount));
if (!NT_SUCCESS(Status))
{
SetLastError(Status);
return FALSE;
}
return TRUE;
}
/* wait */
DWORD __declspec(dllexport) __stdcall
WaitForMultipleObjects(DWORD nCount,
CONST HANDLE *lpHandles,
BOOL bWaitAll,
DWORD dwMilliseconds)
{
return WaitForMultipleObjectsEx(nCount,
lpHandles,
bWaitAll,
dwMilliseconds,
FALSE);
}
DWORD __declspec(dllexport) __stdcall
WaitForMultipleObjectsEx(DWORD nCount,
CONST HANDLE *lpHandles,
BOOL bWaitAll,
DWORD dwMilliseconds,
BOOL bAlertable)
{
PLARGE_INTEGER TimePtr;
LARGE_INTEGER Time;
NTSTATUS Status;
Time.QuadPart = (LONGLONG)dwMilliseconds;
TimePtr = &Time;
Status = NtWaitForMultipleObjects ((int) nCount,
(int) lpHandles,
(int) bWaitAll,
(int) (BOOLEAN)bAlertable,
(int) TimePtr);
return Status;
}
/* handle */
BOOL __declspec(dllexport) __stdcall
CloseHandle(HANDLE hObject)
{
NTSTATUS Status;
Status = NtClose((int) hObject);
if (!NT_SUCCESS(Status)) {
SetLastError(Status);
return FALSE;
}
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -