首页
社区
课程
招聘
[原创]浅谈那些年来我们用过的设备操作
发表于: 2014-1-21 12:12 4199

[原创]浅谈那些年来我们用过的设备操作

2014-1-21 12:12
4199
NTSTATUS Writea(IN PDEVICE_OBJECT pDevice,
			   IN PIRP pIrp)
{
	
	NTSTATUS Status=STATUS_SUCCESS;
	PDEVICE_EXTENSION PDeviN=(PDEVICE_EXTENSION)pDevice->DeviceExtension;
	PIO_STACK_LOCATION stack=IoGetCurrentIrpStackLocation(pIrp);
	ULONG WriteLenth=stack->Parameters.Write.Length;
	ULONG WriteOffset=stack->Parameters.Write.ByteOffset.QuadPart;
	if (WriteLenth+WriteOffset>1024)  //1024是我们分配的内存大小,超过了你很可能就挂彩了
	{
		Status=STATUS_FILE_INVALID;
		WriteLenth=0;
	}
	else
	{
		KdPrint(("write buffer 0x%x\n",PDeviN->buffer));
	KdPrint(("write Ox%x\n",WriteOffset));
	KdPrint(("write Ox%x\n",WriteLenth));


		memcpy(PDeviN->buffer+WriteOffset,pIrp->AssociatedIrp.SystemBuffer,WriteLenth);
		
		Status=STATUS_SUCCESS;
		if (WriteLenth+WriteOffset>PDeviN->file_length)
		{
PDeviN->file_length=WriteLenth+WriteOffset;

		}

	}

	pIrp->IoStatus.Status=Status;
	pIrp->IoStatus.Information=WriteLenth;
	IoCompleteRequest(pIrp,IO_NO_INCREMENT);
	return Status;
}
NTSTATUS Reada(IN PDEVICE_OBJECT pDevObj,
			   IN PIRP pIrp)
{	
PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)pDevObj->DeviceExtension;
NTSTATUS status = STATUS_SUCCESS;
PIO_STACK_LOCATION stack = IoGetCurrentIrpStackLocation(pIrp);
ULONG ReadLength = stack->Parameters.Read.Length;
ULONG ReadOffset = (ULONG)stack->Parameters.Read.ByteOffset.QuadPart;
if (ReadOffset+ReadLength>1024)
{
	status = STATUS_FILE_INVALID;
	ReadLength = 0;
}else
{
	KdPrint(("Read buffer 0x%x\n",pDevExt->buffer));
	KdPrint(("Read WriteOffset Ox%x\n",ReadOffset));
	KdPrint(("Read WriteLenth Ox%x\n",ReadLength));
    memcpy(pIrp->AssociatedIrp.SystemBuffer,pDevExt->buffer+ReadOffset,ReadLength);
	status = STATUS_SUCCESS;
}

     pIrp->IoStatus.Status=status;
	 pIrp->IoStatus.Information=ReadLength;
	 IoCompleteRequest(pIrp,IO_NO_INCREMENT);
   return status;
}
#define MmGetMdlVirtualAddress(Mdl)                                     \
    ((PVOID) ((PCHAR) ((Mdl)->StartVa) + (Mdl)->ByteOffset))

//++
//
// ULONG
// MmGetMdlByteCount (
//     __in PMDL Mdl
//     )
//
// Routine Description:
//
//     The MmGetMdlByteCount returns the length in bytes of the buffer
//     described by the Mdl.
//
// Arguments:
//
//     Mdl - Pointer to an MDL.
//
// Return Value:
//
//     Returns the byte count of the buffer described by the Mdl
//
//--

#define MmGetMdlByteCount(Mdl)  ((Mdl)->ByteCount)

//++
//
// ULONG
// MmGetMdlByteOffset (
//     __in PMDL Mdl
//     )
//
// Routine Description:
//
//     The MmGetMdlByteOffset returns the byte offset within the page
//     of the buffer described by the Mdl.
//
// Arguments:
//
//     Mdl - Pointer to an MDL.
//
// Return Value:
//
//     Returns the byte offset within the page of the buffer described by the Mdl
//
//--

#define MmGetMdlByteOffset(Mdl)  ((Mdl)->ByteOffset)

//++
//
// PVOID
// MmGetMdlStartVa (
//     __in PMDL Mdl
//     )
//
// Routine Description:
//
//     The MmGetMdlBaseVa returns the virtual address of the buffer
//     described by the Mdl rounded down to the nearest page.
//
// Arguments:
//
//     Mdl - Pointer to an MDL.
//
// Return Value:
//
//     Returns the returns the starting virtual address of the MDL.
//
//
//--

#define MmGetMdlBaseVa(Mdl)  ((Mdl)->StartVa)
PVOID Kernel_Address=MmGetSystemAddressForMdlSafe(pIrp->MdlAddress,NormalPagePriority);

[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!

上传的附件:
收藏
免费 5
支持
分享
最新回复 (0)
游客
登录 | 注册 方可回帖
返回
//