feat: 增加事件通信代码

This commit is contained in:
2025-03-14 00:11:07 +08:00
parent cef75c0adf
commit e3d29b1183
11 changed files with 341 additions and 140 deletions

View File

@@ -1,11 +1,21 @@
#include <iostream>
#include <cstdlib>
#include <Windows.h>
#include "../KernelCommunication/ioctl.h"
#include <memory>
#include "TestDrvice.h"
#define TEST_SERVICE_NAME "aaayhytestservice"
std::thread gWorkThread;
bool gThreadFlags = false;
void workThread(HANDLE eventHandle) {
while (gThreadFlags) {
WaitForSingleObject(eventHandle, INFINITE);
// TODO: 处理内核通知
printf("Kernel Event!\n");
ResetEvent(eventHandle); // 重置事件以便下次触发
}
// 退出线程时,关闭句柄
printf("Exit WorkThread! eventHandle: %08x", eventHandle);
if (eventHandle != NULL) { CloseHandle(eventHandle); }
return;
}
void ShowError(const char* szName) {
DWORD dwErrorCode = GetLastError();
@@ -126,32 +136,46 @@ int UnLoadDriver(const char* szServiceName) {
void UseDriver() {
HANDLE hFile = CreateFileW(L"\\\\.\\" MY_DRVICE_NAME,
unique_handle hFile = make_unique_handle(CreateFileW(KERNEL_FILE_NAME,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
printf("CreateFile hFile:%p ErrCode:%08x\n", hFile, GetLastError());
NULL));
printf("CreateFile hFile:%p ErrCode:%08x\n", hFile.get(), GetLastError());
DWORD dwBytes = 0;
// 1. 发送让驱动创建事件对象的消息
bool result = DeviceIoControl(hFile.get(), IOCTL_KERNEL_CREATE_EVENT_OBJECT, NULL, 0, NULL, 0, &dwBytes, NULL);
if (!result) {
printf("IOCTL_KERNEL_CREATE_EVENT_OBJECT False!\n");
return;
}
// 2. 打开内核驱动的命名同步对象
HANDLE eventHandle = OpenEventW(EVENT_ALL_ACCESS, FALSE, KERNEL_TO_USER_EVENT_NAME);
printf("eventHandle: %08x\n", eventHandle);
gWorkThread = std::thread([=] {
gThreadFlags = true;
workThread(eventHandle);
});
// 测试ReadFile 功能
char Buffer[MAXBYTE] = { 0 };
DWORD dwBytes = 0;
BOOL Result = ReadFile(hFile, Buffer, sizeof(Buffer), &dwBytes, NULL);
BOOL Result = ReadFile(hFile.get(), Buffer, 6, &dwBytes, NULL);
printf("ReadFile Result:%d dwBytes:%d Buffer:%s ErrCode:%08x\n", Result, dwBytes, Buffer, GetLastError());
// 测试WriteFile 功能
WriteFile(hFile, Buffer, sizeof(Buffer), &dwBytes, NULL);
printf("WriteFile Result:%d dwBytes:%d Buffer:%s ErrCode:%08x\n", Result, dwBytes, Buffer, GetLastError());
// 测试DeviceIoControl 功能
char data[4] = { 0x78, 0x56, 0x34, 0x12 };
printf("Data value: %p\n", *(int*)data);
DeviceIoControl(hFile, IOCTL_READ_MEMORY, &data, sizeof(data),
data, sizeof(data), &dwBytes, NULL);
printf("DeviceIoControl Result:%d dwBytes:%d Data:%s ErrCode:%08x\n", Result, dwBytes, data, GetLastError());
printf("Data newValue: %p\n", *(int*)data);
// 关闭文件
CloseHandle(hFile);
//// 测试WriteFile 功能
//WriteFile(hFile, Buffer, sizeof(Buffer), &dwBytes, NULL);
//printf("WriteFile Result:%d dwBytes:%d Buffer:%s ErrCode:%08x\n", Result, dwBytes, Buffer, GetLastError());
// 退出
gThreadFlags = false;
result = DeviceIoControl(hFile.get(), IOCTL_CLOSE_EVENT_OBJECT, NULL, 0, NULL, 0, &dwBytes, NULL);
gWorkThread.join();
return;
}
@@ -175,7 +199,6 @@ int main(int argc, char* argv[])
// TODO: 做一些事情
UseDriver();
if (UnLoadDriver(TEST_SERVICE_NAME) != 0) {
cerr << "[x] UnLoadDriver Faile! " << endl;
system("pause");

16
TestDrvice/TestDrvice.h Normal file
View File

@@ -0,0 +1,16 @@
#pragma once
#include <iostream>
#include <cstdlib>
#include <Windows.h>
#include "../KernelCommunication/ioctl.h"
#include <memory>
#include <thread>
using unique_handle = std::unique_ptr<
std::remove_pointer_t<HANDLE>, // 类型为 void
BOOL(WINAPI*)(HANDLE) // 函数指针类型
>;
unique_handle make_unique_handle(HANDLE h = INVALID_HANDLE_VALUE) {
return unique_handle(h, &CloseHandle);
}

View File

@@ -131,6 +131,9 @@
<ItemGroup>
<ClCompile Include="TestDrvice.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="TestDrvice.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>

View File

@@ -19,4 +19,9 @@
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="TestDrvice.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
</Project>