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

📄 debugprint.cpp

📁 Windows 2000/XP WDM設備驅動程式開發 使用 Numega 公司出版的軟體快速建置驅動程式
💻 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");

	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);
			if (!NT_SUCCESS(status))
			{
				DbgPrint("Write error\n");		
			}
			else
				DbgPrint("Write OK\n");		
			delete(pMyBuffer);
			delete(pMyEntry);
		}
		else
		{
			m_File.Close();
			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 only
	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 + -