用户名: 密码: 免费注册 忘记密码? 网站地图 | 加入收藏 | 设为首页
首页 | 新闻 | 工具 | 系统 | 办公 | 聊天 | 多媒体 | 网页 | 运营 | 平面 | 欣赏 | 数据库 | 程序 | 服务器 | 组网
网页 | 3dmax | Ghost | Windows Xp| Dreamweaver | photoshop | Flash | office | Alexa | Css | QQ | Asp | PHP | Jsp | Access
Flash MX 2004入门 | 网站推广策略 | CorelDRAW入门 | ASP学习 | 网站建设大师功 | Word入门
  iTbulo.com > 学院 > 操作系统教程 > Windows2000教程 > 文章正文
《Undocumented Windows 2000 Secrets》翻译 --- 第三章(1)
iTbulo.COM 2005-7-29 ccxunmeng()

深入驱动程序的骨架

列表 3-3 展示了向导生成的 TestDrv.c 。与之相关的头文件 TestDrv.h 在 列表 3-4 中。在 列表 3-3 中,请注意标题处的 <MyName> 和 <MyCompany> 标志。如果 w2k_wiz.ini 中的作者和公司名称正确,那你自己的名字和相应的公司名称将会替代它们。

// TestDrv.c

// 08-07-2000 <MyName>

// Copyright @2005 <MyCompany>

#define _TESTDRV_SYS_

#include <ntddk.h>

#include "TestDrv.h"

// =================================================================

// DISCLAIMER

// =================================================================

/*

This software is provided "as is" and any express or implied

warranties, including, but not limited to, the implied warranties of

merchantability and fitness for a particular purpose are disclaimed.

In no event shall the author <MyName> be liable for any

direct, indirect, incidental, special, exemplary, or consequential

damages (including, but not limited to, procurement of substitute

goods or services; loss of use, data, or profits; or business

interruption) however caused and on any theory of liability,

whether in contract, strict liability, or tort (including negligence

or otherwise) arising in any way out of the use of this software,

even if advised of the possibility of such damage.

*/

// =================================================================

// REVISION HISTORY

// =================================================================

/*

08-07-2000 V1.00 Original version.

*/

// =================================================================

// GLOBAL DATA

// =================================================================

PRESET_UNICODE_STRING (usDeviceName, CSTRING (DRV_DEVICE));

PRESET_UNICODE_STRING (usSymbolicLinkName, CSTRING (DRV_LINK ));

PDEVICE_OBJECT gpDeviceObject = NULL;

PDEVICE_CONTEXT gpDeviceContext = NULL;

// =================================================================

// DISCARDABLE FUNCTIONS

// =================================================================

NTSTATUS DriverInitialize (PDRIVER_OBJECT pDriverObject,

PUNICODE_STRING pusRegistryPath);

NTSTATUS DriverEntry (PDRIVER_OBJECT pDriverObject,

PUNICODE_STRING pusRegistryPath);

// -----------------------------------------------------------------

#ifdef ALLOC_PRAGMA

#pragma alloc_text (INIT, DriverInitialize)

#pragma alloc_text (INIT, DriverEntry)

#endif

// =================================================================

// DEVICE REQUEST HANDLER

// =================================================================

NTSTATUS DeviceDispatcher (PDEVICE_CONTEXT pDeviceContext,

PIRP pIrp)

{

PIO_STACK_LOCATION pisl;

DWORD dInfo = 0;

NTSTATUS ns = STATUS_NOT_IMPLEMENTED;

pisl = IoGetCurrentIrpStackLocation (pIrp);

switch (pisl->MajorFunction)

{

case IRP_MJ_CREATE:

case IRP_MJ_CLEANUP:

case IRP_MJ_CLOSE:

{

ns = STATUS_SUCCESS;

break;

}

}

pIrp->IoStatus.Status = ns;

pIrp->IoStatus.Information = dInfo;

IoCompleteRequest (pIrp, IO_NO_INCREMENT);

return ns;

}

// =================================================================

// DRIVER REQUEST HANDLER

// =================================================================

NTSTATUS DriverDispatcher (PDEVICE_OBJECT pDeviceObject,

PIRP pIrp)

{

return (pDeviceObject == gpDeviceObject

? DeviceDispatcher (gpDeviceContext, pIrp)

: STATUS_INVALID_PARAMETER_1);

}

// -----------------------------------------------------------------

void DriverUnload (PDRIVER_OBJECT pDriverObject)

{

IoDeleteSymbolicLink (&usSymbolicLinkName);

IoDeleteDevice (gpDeviceObject);

return;

}

// =================================================================

// DRIVER INITIALIZATION

// =================================================================

NTSTATUS DriverInitialize (PDRIVER_OBJECT pDriverObject,

PUNICODE_STRING pusRegistryPath)

{

PDEVICE_OBJECT pDeviceObject = NULL;

NTSTATUS ns = STATUS_DEVICE_CONFIGURATION_ERROR;

if ((ns = IoCreateDevice (pDriverObject, DEVICE_CONTEXT_,

&usDeviceName, FILE_DEVICE_CUSTOM,

0, FALSE, &pDeviceObject))

== STATUS_SUCCESS)

{

if ((ns = IoCreateSymbolicLink (&usSymbolicLinkName,

&usDeviceName))

== STATUS_SUCCESS)

{

gpDeviceObject = pDeviceObject;

gpDeviceContext = pDeviceObject->DeviceExtension;

gpDeviceContext->pDriverObject = pDriverObject;

gpDeviceContext->pDeviceObject = pDeviceObject;

}

else

{

IoDeleteDevice (pDeviceObject);

}

}

return ns;

}

// -----------------------------------------------------------------

NTSTATUS DriverEntry (PDRIVER_OBJECT pDriverObject,

PUNICODE_STRING pusRegistryPath)

{

PDRIVER_DISPATCH *ppdd;

NTSTATUS ns = STATUS_DEVICE_CONFIGURATION_ERROR;

if ((ns = DriverInitialize (pDriverObject, pusRegistryPath))

== STATUS_SUCCESS)

{

ppdd = pDriverObject->MajorFunction;

ppdd [IRP_MJ_CREATE ] =

ppdd [IRP_MJ_CREATE_NAMED_PIPE ] =

ppdd [IRP_MJ_CLOSE ] =

ppdd [IRP_MJ_READ ] =

ppdd [IRP_MJ_WRITE ] =

ppdd [IRP_MJ_QUERY_INFORMATION ] =

ppdd [IRP_MJ_SET_INFORMATION ] =

ppdd [IRP_MJ_QUERY_EA ] =

ppdd [IRP_MJ_SET_EA ] =

ppdd [IRP_MJ_FLUSH_BUFFERS ] =

ppdd [IRP_MJ_QUERY_VOLUME_INFORMATION] =

ppdd [IRP_MJ_SET_VOLUME_INFORMATION ] =

ppdd [IRP_MJ_DIRECTORY_CONTROL ] =

ppdd [IRP_MJ_FILE_SYSTEM_CONTROL ] =

ppdd [IRP_MJ_DEVICE_CONTROL ] =

ppdd [IRP_MJ_INTERNAL_DEVICE_CONTROL ] =

ppdd [IRP_MJ_SHUTDOWN ] =

ppdd [IRP_MJ_LOCK_CONTROL ] =

ppdd [IRP_MJ_CLEANUP ] =

ppdd [IRP_MJ_CREATE_MAILSLOT ] =

ppdd [IRP_MJ_QUERY_SECURITY ] =

ppdd [IRP_MJ_SET_SECURITY ] =

ppdd [IRP_MJ_POWER ] =

ppdd [IRP_MJ_SYSTEM_CONTROL ] =

ppdd [IRP_MJ_DEVICE_CHANGE ] =

ppdd [IRP_MJ_QUERY_QUOTA ] =

ppdd [IRP_MJ_SET_QUOTA ] =

ppdd [IRP_MJ_PNP ] = DriverDispatcher;

pDriverObject->DriverUnload = DriverUnload;

}

return ns;

}

// =================================================================

// END OF PROGRAM

// =================================================================

列表 3-3. 驱动程序骨架的源代码

// TestDrv.h

// 08-07-2000 <MyName>

// Copyright @2005 <MyCompany>

// =================================================================

// PROGRAM IDENTIFICATION

// =================================================================

#define DRV_BUILD 1

#define DRV_VERSION_HIGH 1

#define DRV_VERSION_LOW 0

// -----------------------------------------------------------------

#define DRV_DAY 07

#define DRV_MONTH 02

#define DRV_YEAR 2005

// -----------------------------------------------------------------

// Customize these settings by editing the configuration file

// D:\etc32\w2k_wiz.ini

#define DRV_MODULE TestDrv

#define DRV_NAME <SBS Windows 2000 Code Wizard Project>

#define DRV_COMPANY <MyCompany>

#define DRV_AUTHOR <MyName>

#define DRV_EMAIL <my@email>

#define DRV_PREFIX <MyPrefix>

// =================================================================

// HEADER FILES

// =================================================================

#include "drvinfo.h" // defines more DRV_* items

////////////////////////////////////////////////////////////////////

#ifndef _RC_PASS_

////////////////////////////////////////////////////////////////////

// =================================================================

// CONSTANTS

// =================================================================

#define FILE_DEVICE_CUSTOM 0x8000

// =================================================================

// STRUCTURES

// =================================================================

typedef struct _DEVICE_CONTEXT

{

PDRIVER_OBJECT pDriverObject;

PDEVICE_OBJECT pDeviceObject;

}

DEVICE_CONTEXT, *PDEVICE_CONTEXT, **PPDEVICE_CONTEXT;

#define DEVICE_CONTEXT_ sizeof (DEVICE_CONTEXT)

////////////////////////////////////////////////////////////////////

#endif // #ifndef _RC_PASS_

////////////////////////////////////////////////////////////////////

// =================================================================

// END OF FILE

// =================================================================

 

上一页  [1] [2] [3] 

文章搜索
相关资讯
相关文章 相关下载
没有相关文章
焦点信息