好友
阅读权限10
听众
最后登录1970-1-1
|
#include <iostream>
#include <Windows.h>
#include <stdlib.h>
int main()
{
HANDLE hdevice = NULL;
UCHAR readBuffer[50] = { 0 };
DWORD bread = 0;
hdevice = CreateFile("\\\\.\\MyFirstDevice",GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if (hdevice == INVALID_HANDLE_VALUE) {
printf("Open Device Faile\n");
system("pause");
return 0;
}
printf("Open Sucess\n");
system("pause");
ReadFile(hdevice, (PVOID)readBuffer, 50, &bread, NULL);
printf("--%p--%s--%d--\n", readBuffer, readBuffer, bread);
printf("Write!\n");
system("pause");
WriteFile(hdevice, "This Message come from R3.", strlen("This Message come from R3."), &bread, NULL);
CloseHandle(hdevice);
system("pause");
return 0;
}这是R3代码
以下是驱动的部分关键代码:
#include <ntifs.h>
#define DEVICE_NAME L"\\Device\\MyFirstDevice"
#define SYM_NAME L"\\??\\MyFirstDevice"
void nothing(HANDLE ppid, HANDLE mypid, BOOLEAN bcreate) {
DbgPrint("ProcessNotify\n");
}
NTSTATUS MyWrite(PDEVICE_OBJECT pdevice, PIRP pirp) {
NTSTATUS status = STATUS_SUCCESS;
DbgPrint("My Device has be Writeed\n");
PIO_STACK_LOCATION pstack = IoGetCurrentIrpStackLocation(pirp);
ULONG Writesize = pstack->Parameters.Write.Length;
PCHAR Writebuffer = pirp->AssociatedIrp.SystemBuffer;
RtlZeroMemory(pdevice->DeviceExtension,200);
RtlCopyMemory(pdevice->DeviceExtension,Writebuffer, Writesize);
DbgPrint("--%llx--%s\n", Writebuffer, (PCHAR)pdevice->DeviceExtension);
pirp->IoStatus.Status = status;
pirp->IoStatus.Information = 13;
IoCompleteRequest(pirp, IO_NO_INCREMENT);
return STATUS_SUCCESS;
}
//入口函数,相当于main函数
NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObject, PUNICODE_STRING pRegistryPath) {
//驱动程序的入口
NTSTATUS status = STATUS_SUCCESS;
UNICODE_STRING devicename = { 0 };
//设置卸载函数
pDriverObject->DriverUnload = DriverUnload;
RtlInitUnicodeString(&devicename, DEVICE_NAME);
//创建设备
PDEVICE_OBJECT pdevice = NULL;
status = IoCreateDevice(pDriverObject, 0, &devicename, FILE_DEVICE_UNKNOWN, 0, TRUE, &pdevice);
if (!NT_SUCCESS(status)) {
DbgPrint("Create Device Failed:%x\n", status);
return status;
}
//设置交互数据的方式
pdevice->Flags |= DO_BUFFERED_IO;
//创建符号连接名称
UNICODE_STRING symname = { 0 };
RtlInitUnicodeString(&symname, SYM_NAME);
//创建符号链接
status = IoCreateSymbolicLink(&symname, &devicename);
if (!NT_SUCCESS(status)) {
DbgPrint("创建符号链接失败!\n");
DbgPrint("Create SymbolicLink Failed :%x\n", status);
IoDeleteDevice(pdevice);
return status;
}
pDriverObject->MajorFunction[IRP_MJ_CREATE] = MyCreate;
pDriverObject->MajorFunction[IRP_MJ_CLOSE] = MyClose;
pDriverObject->MajorFunction[IRP_MJ_CLEANUP] = MyCleanup;
pDriverObject->MajorFunction[IRP_MJ_READ] = MyRead;
pDriverObject->MajorFunction[IRP_MJ_WRITE] = MyWrite;
return 0;
}
在实际测试的时候,安装好驱动,R3的那个代码一运行到
WriteFile(hdevice, "This Message come from R3.", strlen("This Message come from R3."), &bread, NULL);
就直接蓝屏
有没有大佬知道该怎么解决?
|
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|