大家好,除夕/新年快乐,

我正在制作的驱动程序出现问题。一切正常,直到我能够处理WriteFile()请求。我的司机竭尽所能,但我却蓝屏了。

我必须将错误的确切位置确定为以下语句:

pWriteDataBuffer = MmGetSystemAddressForMdlSafe(Irp->MdlAddress, NormalPagePriority);

这是我完整的驱动程序代码(我正在阅读各种教程,所以不要介意可怕的格式,我没有时间整理事情:
#include "ntddk.h"

const WCHAR deviceNameBuffer[] = L"\\Device\\MemoryMirrorDevice";
const WCHAR SymbolicLinkName[] = L"\\DosDevices\\MemoryMirrorLink";
PDEVICE_OBJECT g_DevicePointer= NULL;                      // Global pointer to our device object
int i = 0;
int y;

VOID OnUnload( IN PDRIVER_OBJECT DriverObject ){


UNICODE_STRING SymbolicLinkNameString;

DbgPrint("OnUnload called\n");

    RtlInitUnicodeString(&SymbolicLinkNameString, SymbolicLinkName);
    IoDeleteSymbolicLink(&SymbolicLinkNameString);
    IoDeleteDevice(DriverObject->DeviceObject);


}

int IsStringTerminated(PCHAR pString, unsigned int uiLength){
    int bStringIsTerminated = 0;
    unsigned int uiIndex = 0;

    while(uiIndex < uiLength && bStringIsTerminated == FALSE)
    {
        if(pString[uiIndex] == '\0')
        {
            bStringIsTerminated = 1;
        }
        else
        {
           uiIndex++;
        }
    }

    return bStringIsTerminated;
}



NTSTATUS MyWrite(PDEVICE_OBJECT DeviceObject, PIRP Irp){
    NTSTATUS NtStatus = STATUS_SUCCESS;
    PIO_STACK_LOCATION pIoStackIrp = NULL;
    PCHAR pWriteDataBuffer;

DbgPrint("MyWrite\r\n");

    pIoStackIrp = IoGetCurrentIrpStackLocation(Irp);


    if(pIoStackIrp)
    {


        pWriteDataBuffer = MmGetSystemAddressForMdlSafe(Irp->MdlAddress, NormalPagePriority);

  return NtStatus;

        if( pWriteDataBuffer != NULL )
        {


           if(IsStringTerminated(pWriteDataBuffer, pIoStackIrp->Parameters.Write.Length))
           {
                DbgPrint(pWriteDataBuffer);
           }
        }
    }

  return NtStatus;
}



void CheckErrors(NTSTATUS ntStatus){
i++;

DbgPrint("%i \n",i);


switch(ntStatus){
case STATUS_INSUFFICIENT_RESOURCES:
DbgPrint("INSUFFICIENT RESOURCES\n");
break;
case STATUS_OBJECT_NAME_EXISTS:
DbgPrint("Name exists\n");
break;
case STATUS_OBJECT_NAME_COLLISION:
DbgPrint("Name collission\n");
break;
case STATUS_SUCCESS:
DbgPrint("Success!\n");
break;
default:
DbgPrint("Error is not filtered!\n");
}
}

NTSTATUS OnStubDispatch(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp){
Irp->IoStatus.Status = STATUS_SUCCESS;
IoCompleteRequest(Irp, IO_NO_INCREMENT );
return STATUS_SUCCESS;
}


NTSTATUS DriverEntry( IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING theRegistryPath ){

NTSTATUS ntStatus = STATUS_SUCCESS;
UNICODE_STRING DeviceName;
UNICODE_STRING SymbolicLinkNameString;

DriverObject->DriverUnload = OnUnload;

RtlInitUnicodeString (&DeviceName, deviceNameBuffer );
RtlInitUnicodeString (&SymbolicLinkNameString, SymbolicLinkName);

ntStatus = IoCreateDevice ( DriverObject,0, &DeviceName, FILE_DEVICE_UNKNOWN, FILE_DEVICE_SECURE_OPEN, FALSE, &g_DevicePointer);
CheckErrors(ntStatus);

if(ntStatus == STATUS_SUCCESS){

ntStatus = IoCreateSymbolicLink( &SymbolicLinkNameString,  &DeviceName);
CheckErrors(ntStatus);
}

for(y = 0; y < IRP_MJ_MAXIMUM_FUNCTION; y++ ){
DriverObject->MajorFunction[y] = OnStubDispatch;
}
DriverObject->MajorFunction[IRP_MJ_WRITE] = MyWrite;


return STATUS_SUCCESS;
}

驱动程序收到IRp写入请求后,代码将失败:
#include <iostream>
#include <windows.h>
using namespace std;



int _cdecl main(void){
    HANDLE hFile;
    DWORD dwReturn;
    hFile = CreateFile("\\\\.\\MemoryMirrorLink",
        GENERIC_READ | GENERIC_WRITE, 0, NULL,
        OPEN_EXISTING, 0, NULL);
    if(hFile){
        WriteFile(hFile, "Hello from user mode!",sizeof("Hello from user mode!"), &dwReturn, NULL);
        printf("Succes!");
        CloseHandle(hFile);

    } else {
printf("kthxbye: ");
cerr<<GetLastError()<<endl;
    }

    cin.ignore();
    return 0;
}

有人知道我在做什么错吗?
非常感谢您的帮助!

最佳答案

Irp-> MdlAddress可能为NULL,请改用Irp-> UserBuffer。

熟悉内核调试器并诊断蓝屏。您将需要它。

08-18 05:27