📄 kernel32_u.cpp
字号:
/*
* kernel32_u.cpp: for kernel32_u.dll
*
* Copyright (C) 2006 Insigme Co., Ltd
*
* Authors:
* - Decao Mao, Chenzhan Hu, Lixing Chu, Limin Jin, Liwei Zhou, Zhiqiang Jiao
*
* This software has been developed while working on the Linux Unified Kernel
* project (http://linux.insigma.com.cn) in the Insigma Reaserch Institute,
* which is a subdivision of Insigma Co., Ltd (http://www.insigma.com.cn).
*
* The project is sponsored by Insigma Co., Ltd.
*
* The authors can be reached at linux@insigma.com.cn.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* Revision History:
* Jan 2006 - Created.
*/
#include "stdafx.h"
#include "ntdef.h"
#include <stdlib.h>
#include <stdio.h>
HANDLE hBaseDir = NULL;
/* Event */
__declspec(dllimport) __stdcall
NtCreateEvent(int param0, int param1, int param2, int param3,
int param4);
__declspec(dllimport) __stdcall
NtOpenEvent(int param0, int param1, int param2);
__declspec(dllimport) __stdcall
NtSetEvent(int param0, int param1);
__declspec(dllimport) __stdcall
NtResetEvent(int param0, int param1);
__declspec(dllimport) __stdcall
NtPulseEvent(int param0, int param1);
/* File */
__declspec(dllimport) __stdcall
NtCreateFile(int param0, int param1, int param2, int param3,
int param4, int param5, int param6, int param7,
int param8, int param9, int param10);
__declspec(dllimport) __stdcall
NtOpenFile(int param0, int param1, int param2, int param3,
int param4, int param5);
__declspec(dllimport) __stdcall
NtReadFile(int param0, int param1, int param2, int param3,
int param4, int param5, int param6, int param7,
int param8);
__declspec(dllimport) __stdcall
NtWriteFile(int param0, int param1, int param2, int param3,
int param4, int param5, int param6, int param7,
int param8);
__declspec(dllimport) __stdcall
NtFlushBuffersFile(int param0, int param1);
__declspec(dllimport) __stdcall
NtQueryInformationFile(int param0, int param1, int param2, int param3,
int param4);
__declspec(dllimport) __stdcall
NtSetInformationFile(int param0, int param1, int param2, int param3,
int param4);
/* Mutex */
__declspec(dllimport) __stdcall
NtCreateMutant(int param0, int param1, int param2, int param3);
__declspec(dllimport) __stdcall
NtOpenMutant(int param0, int param1, int param2);
__declspec(dllimport) __stdcall
NtReleaseMutant(int param0, int param1);
/* Section */
__declspec(dllimport) __stdcall
NtCreateSection(int param0, int param1, int param2, int param3,
int param4, int param5, int param6);
__declspec(dllimport) __stdcall
NtMapViewOfSection(int param0, int param1, int param2, int param3,
int param4, int param5, int param6, int param7,
int param8, int param9);
__declspec(dllimport) __stdcall
NtUnmapViewOfSection(int param0, int param1);
/* Semaphore */
__declspec(dllimport) __stdcall
NtCreateSemaphore(int param0, int param1, int param2, int param3,
int param4);
__declspec(dllimport) __stdcall
NtOpenSemaphore(int param0, int param1, int param2);
__declspec(dllimport) __stdcall
NtReleaseSemaphore(int param0, int param1, int param2);
/* wait */
__declspec(dllimport) __stdcall
NtWaitForMultipleObjects(int param0, int param1, int param2,
int param3, int param4);
/* handle */
__declspec(dllimport) __stdcall
NtClose(int param0);
/* DllMain */
BOOL APIENTRY DllMain(HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved)
{
return TRUE;
}
VOID __stdcall
RtlInitUnicodeString(IN OUT PUNICODE_STRING DestinationString,
IN PCWSTR SourceString)
{
ULONG DestSize;
if (SourceString == NULL)
{
DestinationString->Length = 0;
DestinationString->MaximumLength = 0;
}
else
{
DestSize = wcslen((PWSTR)SourceString) * sizeof(WCHAR);
DestinationString->Length = DestSize;
DestinationString->MaximumLength = DestSize + sizeof(WCHAR);
}
DestinationString->Buffer = (PWSTR)SourceString;
}
/* DLL export */
/* Event */
HANDLE __declspec(dllexport) __stdcall
CreateEventA(LPSECURITY_ATTRIBUTES lpEventAttributes,
BOOL bManualReset,
BOOL bInitialState,
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 = CreateEventW(lpEventAttributes,
bManualReset,
bInitialState,
NameU);
free(NameU);
return Handle;
}
HANDLE __declspec(dllexport) __stdcall
CreateEventW(LPSECURITY_ATTRIBUTES lpEventAttributes,
BOOL bManualReset,
BOOL bInitialState,
LPCWSTR lpName)
{
NTSTATUS Status;
HANDLE EventHandle;
UNICODE_STRING UnicodeName;
OBJECT_ATTRIBUTES ObjectAttributes;
RtlInitUnicodeString(&UnicodeName, (LPWSTR)lpName);
InitializeObjectAttributes(&ObjectAttributes,
(lpName ? &UnicodeName : NULL),
0,
(lpName ? hBaseDir : NULL),
NULL);
if (lpEventAttributes)
{
ObjectAttributes.SecurityDescriptor = lpEventAttributes->lpSecurityDescriptor;
if (lpEventAttributes->bInheritHandle)
ObjectAttributes.Attributes |= OBJ_INHERIT;
}
Status = NtCreateEvent((int) (&EventHandle),
(int) (STANDARD_RIGHTS_ALL),
(int)(&ObjectAttributes),
(int) (bManualReset ? NotificationEvent : SynchronizationEvent),
(int) (bInitialState));
if (!NT_SUCCESS(Status)){
SetLastError(Status);
return NULL;
}
return EventHandle;
}
HANDLE __declspec(dllexport) __stdcall
OpenEventA(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 = OpenEventW(dwDesiredAccess,
bInheritHandle,
NameU);
free(NameU);
return Handle;
}
HANDLE __declspec(dllexport) __stdcall
OpenEventW(DWORD dwDesiredAccess,
BOOL bInheritHandle,
LPCWSTR lpName)
{
NTSTATUS Status;
HANDLE Handle;
UNICODE_STRING Name;
OBJECT_ATTRIBUTES ObjectAttributes;
RtlInitUnicodeString(&Name, (LPWSTR)lpName);
InitializeObjectAttributes(&ObjectAttributes,
&Name,
(bInheritHandle ? OBJ_INHERIT : 0),
hBaseDir,
NULL);
Status = NtOpenEvent((int) &Handle,
(int) dwDesiredAccess,
(int) &ObjectAttributes);
if (!NT_SUCCESS(Status)) {
SetLastError(Status);
return NULL;
}
return Handle;
}
BOOL __declspec(dllexport) __stdcall
SetEvent(HANDLE hEvent)
{
NTSTATUS Status;
int state;
Status = NtSetEvent((int)hEvent, (int)&state);
if (!NT_SUCCESS(Status)) {
SetLastError(Status);
return false;
}
return true;
}
BOOL __declspec(dllexport) __stdcall
ResetEvent(HANDLE hEvent)
{
NTSTATUS Status;
int state;
Status = NtResetEvent((int)hEvent, (int)&state);
if (!NT_SUCCESS(Status)) {
SetLastError(Status);
return false;
}
return true;
}
BOOL __declspec(dllexport) __stdcall
PulseEvent(HANDLE hEvent)
{
NTSTATUS Status;
int state;
Status = NtPulseEvent((int)hEvent, (int)&state);
if (!NT_SUCCESS(Status)){
SetLastError(Status);
return false;
}
return true;
}
/* File */
HANDLE __declspec(dllexport) __stdcall
CreateFileA(
LPCSTR lpFilename,
DWORD dwDesiredAccess,
DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile
)
{
int i;
HANDLE h;
UNICODE_STRING unistr;
OBJECT_ATTRIBUTES objattr;
LPWSTR NameU;
IO_STATUS_BLOCK io_status;
if (!lpFilename){
SetLastError(ERROR_INVALID_PARAMETER);
return NULL;
}
NameU = (LPWSTR)malloc((strlen(lpFilename)+1)*sizeof(WCHAR));
MultiByteToWideChar(CP_ACP, 0, lpFilename, strlen(lpFilename)+1,
NameU, strlen(lpFilename)+1);
unistr.Buffer = NameU;
unistr.Length = wcslen(NameU);
objattr.ObjectName = &unistr;
switch (dwCreationDisposition)
{
case CREATE_NEW:
dwCreationDisposition = FILE_CREATE;
break;
case CREATE_ALWAYS:
dwCreationDisposition = FILE_OVERWRITE_IF;
break;
case OPEN_EXISTING:
dwCreationDisposition = FILE_OPEN;
break;
case OPEN_ALWAYS:
dwCreationDisposition = FILE_OPEN_IF;
break;
case TRUNCATE_EXISTING:
dwCreationDisposition = FILE_OVERWRITE;
break;
default:
return NULL;
}
i = NtCreateFile((int) &h,
(int) dwDesiredAccess,
(int) &objattr,
(int) &io_status,
(int) NULL,
(int) dwFlagsAndAttributes,
(int) dwShareMode,
(int) dwCreationDisposition,
0,
0,
0
);
free(NameU);
return i < 0 ? NULL : h;
} /* end CreateFileA() */
BOOL __declspec(dllexport) __stdcall
ReadFile(HANDLE hFile,
LPVOID lpBuffer,
DWORD nNumberOfBytesToRead,
LPDWORD lpNumberOfBytesRead,
LPOVERLAPPED lpOverlapped
)
{
IO_STATUS_BLOCK IoStatusBlock;
BOOL ret = NtReadFile(
(int) hFile,
0,
0,
0,
(int) &IoStatusBlock,
(int) lpBuffer,
(int) nNumberOfBytesToRead,
0,
0
);
*lpNumberOfBytesRead = IoStatusBlock.Information;
return ret;
}
BOOL __declspec(dllexport) __stdcall
WriteFile(HANDLE hFile,
LPVOID lpBuffer,
DWORD nNumberOfBytesToWrite,
LPDWORD lpNumberOfBytesWrite,
LPOVERLAPPED lpOverlapped
)
{
IO_STATUS_BLOCK IoStatusBlock;
BOOL ret = NtWriteFile(
(int) hFile,
0,
0,
0,
(int) &IoStatusBlock,
(int) lpBuffer,
(int) nNumberOfBytesToWrite,
0,
0
);
*lpNumberOfBytesWrite = IoStatusBlock.Information;
return ret;
}
DWORD __declspec(dllexport) __stdcall
SetFilePointer(HANDLE hFile,
LONG lDistanceToMove,
PLONG lpDistanceToMoveHigh,
DWORD dwMoveMethod
)
{
LONG Offset;
FILE_POSITION_INFORMATION FilePos;
if (!hFile){
SetLastError(-1);
return -1;
}
switch (dwMoveMethod)
{
case FILE_BEGIN:
Offset = 0;
break;
case FILE_CURRENT:
NtQueryInformationFile((int) hFile,
0,
(int) &FilePos,
(int) sizeof(FilePos),
(int) FilePositionInformation);
Offset = FilePos.CurrentByteOffset.LowPart;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -