⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 debugprint.cpp

📁 windows 驱动程序开发相关资料与代码 让你深刻知晓windows 开发的魅力所在!
💻 CPP
字号:
// Driver::Works include file
#include <vdw.h>
// DebugPrint.cpp: implementation of the DebugPrint class.
//
//////////////////////////////////////////////////////////////////////
extern "C"
NTKERNELAPI
VOID
ExSystemTimeToLocalTime (
    IN PLARGE_INTEGER SystemTime,
    OUT PLARGE_INTEGER LocalTime
    );

#include "DebugPrint.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

DebugPrint::DebugPrint(char *str)
{
	// Copy the driver's name out of INIT code segment
	//拷贝输出信息的驱动程序名+":"
	DriverNameLen = 1 + strlen(str);
	strncpy(DriverName,str,DriverNameLen);
	DriverName[DriverNameLen - 1]=':';
	KeepRunning=FALSE;
	m_Stage1.Start(LinkTo(Stage1), this);	//启动系统线程
}

DebugPrint::~DebugPrint()
{
	if (KeepRunning) 
	{
		KeepRunning=FALSE;
		m_Stage1.m_Mailbox.Signal();		//发信号
		m_Stage1.Wait();					//等待系统线程结束
	}
	DEBUGPRINT_EVENT* pMyEntry;
	PUCHAR	pMyBuffer;
	while (m_List.Count() > 0)
	{	//删除队列中尚未输出的信息
		pMyEntry = m_List.RemoveHead();
		pMyBuffer = pMyEntry->EventData;

		delete(pMyBuffer);
		delete(pMyEntry);
	}
}
VOID DebugPrint::Stage1(void)
{	//系统线程
	NTSTATUS status;
	KFile m_File;
	KUstring name(L"\\Device\\ReadWriteDevice0");

	//打开ReadWrite设备
	status=m_File.OpenCreate(
			name,
			NULL,
			FILE_GENERIC_READ | FILE_GENERIC_WRITE | SYNCHRONIZE,
			OBJ_CASE_INSENSITIVE,
			0,
			FILE_SHARE_READ | FILE_SHARE_WRITE,
			FILE_OPEN,
			FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT);

	if (!NT_SUCCESS(status))
	{
		DbgPrint("DPrint NO\n");		
		m_Stage1.Terminate(STATUS_SUCCESS);
	}
	KeepRunning=TRUE;
	DbgPrint("DPrint OK\n");		

	while (TRUE)
	{
		m_Stage1.m_Mailbox.Wait();	//等待信号

		if (KeepRunning)
		{
			//从队列中取出信息
			DEBUGPRINT_EVENT* pMyEntry = m_List.RemoveHead();
			PUCHAR	pMyBuffer = pMyEntry->EventData;
			ULONG   dwBytesRead = pMyEntry->Len;
			ULONG i;
			status=m_File.Write(pMyBuffer,dwBytesRead,&i);//写入ReadWrite设备
			if (!NT_SUCCESS(status))
			{
				DbgPrint("Write error\n");		
			}
			else
				DbgPrint("Write OK\n");		
			delete(pMyBuffer);
			delete(pMyEntry);
		}
		else
		{
			m_File.Close();	//关闭ReadWrite设备
			m_Stage1.Terminate(STATUS_SUCCESS);	//终止系统线程
		}
	}
}

///////////////////////////////////////////////////////////////////////////////
// Constructor for TestThread
//
DPTestThread::DPTestThread(void) : m_Mailbox((LONG)0, MAX_MSG) {}//信号灯构造函数

void DebugPrint::DPrint(char *str)
{	//输出字符串函数
	LARGE_INTEGER Now,NowLocal;
	TIME_FIELDS NowTF;
	USHORT MsgLen;
	ULONG EventDataLen;
	//创建队列
	DEBUGPRINT_EVENT* pMyEntry = new (NonPagedPool) DEBUGPRINT_EVENT;
	if ( !pMyEntry )
	{
		return;
	}
	KeQuerySystemTime(&Now);		//系统时间
	ExSystemTimeToLocalTime( &Now, &NowLocal);	//NT函数,转换为本地区时间
	RtlTimeToTimeFields( &NowLocal, &NowTF);	//时间输出格式

	// Get size of Msg and complete event
	MsgLen=strlen(str);
	EventDataLen=sizeof(TIME_FIELDS)+DriverNameLen+MsgLen;
	PUCHAR pMyBuffer = new (NonPagedPool) UCHAR[EventDataLen];
	if ( !pMyBuffer )
	{
		delete(pMyEntry);
		return;
	}
	pMyEntry->EventData=pMyBuffer;
	pMyEntry->Len=EventDataLen;
	RtlCopyMemory(pMyBuffer,&NowTF, sizeof(TIME_FIELDS)); //拷贝时间
	pMyBuffer+=sizeof(TIME_FIELDS);
	RtlCopyMemory(pMyBuffer,DriverName,DriverNameLen); //拷贝驱动程序名
	pMyBuffer+=DriverNameLen;
	RtlCopyMemory(pMyBuffer,str,MsgLen); //拷贝输出信息
	m_List.InsertTail(pMyEntry);	//将信息插入队列尾
	m_Stage1.m_Mailbox.Signal();	//发信号
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -