📄 regsample.cpp
字号:
// RegSample.cpp
//
// Generated by DriverWizard version DriverStudio 3.1.0 (Build 1722)
// Requires Compuware's DriverWorks classes
//
#define VDW_MAIN
#include <vdw.h>
#include "RegSample.h"
#include "RegSampleDevice.h"
#pragma hdrstop("RegSample.pch")
// Generated by DriverWizard version DriverStudio 3.1.0 (Build 1722)
// Set a default 32-bit tag value to be stored with each heap block
// allocated by operator new. Use BoundsChecker to view the memory pool.
// This value can be overridden using the global function SetPoolTag().
POOLTAG DefaultPoolTag('SgeR');
// Create the global driver trace object
// TODO: Use KDebugOnlyTrace if you want trace messages
// to appear only in debug builds. Use KTrace if
// you want trace messages to always appear.
KTrace t("RegSample"); //用于在DriverMonitor上输出调试信息
UNICODE_STRING SampleRegistryPath; //用于保存注册表路径
/////////////////////////////////////////////////////////////////////
// Begin INIT section
#pragma code_seg("INIT")
DECLARE_DRIVER_CLASS(RegSample, NULL)
/////////////////////////////////////////////////////////////////////
// RegSample::DriverEntry
//
// Routine Description:
// This is the first entry point called by the system when the
// driver is loaded.
//
// Parameters:
// RegistryPath - String used to find driver parameters in the
// registry. To locate RegSample look for:
// HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\RegSample
//
// Return Value:
// NTSTATUS - Return STATUS_SUCCESS if no errors are encountered.
// Any other indicates to the system that an error has occured.
//
// Comments:
//
NTSTATUS RegSample::DriverEntry(PUNICODE_STRING RegistryPath)
{
SampleRegistryPath.MaximumLength = RegistryPath->MaximumLength;
//Unicode串(见第6章),最大可存储的字符串长度,与RegistryPath的相同
SampleRegistryPath.Length = 0; //当前有用的信息字符串长度
SampleRegistryPath.Buffer = (PWSTR)ExAllocatePool( PagedPool, SampleRegistryPath.MaximumLength);
//分配Unicode串存储缓冲区
if( SampleRegistryPath.Buffer==NULL) return STATUS_INSUFFICIENT_RESOURCES;
RtlCopyUnicodeString(&SampleRegistryPath,RegistryPath);
//将RegistryPath串内容拷贝到SampleRegistryPath中
KRegistryKey Params(RegistryPath, L"Parameters");//注册表类实例构造函数
t << "RegistryPath:" << RegistryPath << "\n"; //输出注册表路径
if ( NT_SUCCESS(Params.LastError()) )
{
// Load driver data members from the registry
LoadRegistryParameters(Params); //调用加载注册表参数函数
}
m_Unit = 0;
return STATUS_SUCCESS;
}
/////////////////////////////////////////////////////////////////////
// RegSample::LoadRegistryParameters
//
// Routine Description:
// Load driver data members from the registry.
//
// Parameters:
// Params - Open registry key pointing to "Parameters"
//
// Return Value:
// None
//
// Comments:
// Member variables are updated with values read from registry.
//
// The parameters are found as values under the "Parameters" key,
// HKLM\SYSTEM\CurrentControlSet\Services\RegSample\Parameters\...
//
void RegSample::LoadRegistryParameters(KRegistryKey &Params)
{ //加载注册表参数函数
ULONG RegLength;
m_RegDword = 0x123456;
Params.QueryValue(L"RegDword", &m_RegDword);//读注册表参数RegDword的值
t << "RegDword:" << m_RegDword << "\n"; //输出RegDword的值
m_RegString = L"WDM";
RegLength = 0;
Params.QueryValue(L"RegString", m_RegString, RegLength, NonPagedPool);
//读注册表参数RegString的值,因是字符串,置RegLength=0,表示重新分配内存空间
t << "RegString:" << m_RegString << "\n"; //输出RegString的值
// Setting RegLength to 0 indicates a new block should be allocated.
// TODO: If RegLength is returned > 0, a new block was allocated
// and should be deleted when no longer needed.
if (RegLength != 0) delete m_RegString; //必须添加这一条语句
//RegLength != 0,表示重新分配了内存空间。不再用时,要释放所分配的内存空间,
//自动生成时,少了这一条语句,应加上
m_RegBoolean = TRUE;
Params.QueryValue(L"RegBoolean", &m_RegBoolean);
//读注册表参数RegBoolean的值
t << "RegBoolean:" << m_RegBoolean << "\n"; //输出RegBoolean的值
}
// End INIT section
/////////////////////////////////////////////////////////////////////
#pragma code_seg()
/////////////////////////////////////////////////////////////////////
// RegSample::AddDevice
//
// Routine Description:
// Called when the system detects a device for which this
// driver is responsible.
//
// Parameters:
// Pdo - Physical Device Object. This is a pointer to a system device
// object that represents the physical device.
//
// Return Value:
// NTSTATUS - Success or failure code.
//
// Comments:
// This function creates the Functional Device Object, or FDO. The FDO
// enables this driver to handle requests for the physical device.
//
NTSTATUS RegSample::AddDevice(PDEVICE_OBJECT Pdo)
{
// Create the device object. Note that we used a form of "placement" new,
// that is a member operator of KDevice. This form will use storage
// allocated by the system in the device object's device to store our
// class instance.
RegSampleDevice * pDevice = new (
NULL,
FILE_DEVICE_UNKNOWN,
NULL,
0,
DO_DIRECT_IO
| DO_POWER_PAGABLE
)
RegSampleDevice(Pdo, m_Unit);
if (pDevice == NULL)
{
return STATUS_INSUFFICIENT_RESOURCES;
}
NTSTATUS status = pDevice->ConstructorStatus();
if ( !NT_SUCCESS(status) )
{
delete pDevice;
}
else
{
m_Unit++;
pDevice->ReportNewDevicePowerState(PowerDeviceD0);
}
return status;
}
///////////////////////////////////////////////////////////////////
// Unload
//
//
VOID RegSample::Unload(void)
{ //重载Unload例程
t << "Unloading RegSample WDM driver\n";
// TODO: implmenent driver specific unload operations here
if( SampleRegistryPath.Buffer!=NULL)
ExFreePool(SampleRegistryPath.Buffer);
// 驱动程序卸载时,须释放在DriverEntry例程中分配的内存空间
// call the base class handler
KDriver::Unload(); //调用基类Unload()例程
}
// EOF
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -