NT式驱动程序的helloworld

本文详细介绍了驱动程序的HelloWorld实现过程,包括包含头文件、函数定义、参数解析及驱动卸载等关键步骤。此外,还阐述了如何在特定环境下进行编译和加载测试程序,并观察执行结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

驱动程序的HelloWorld:

/*first.c*/

#include <ntddk.h>
VOID DriverUnload(PDRIVER_OBJECT driver)
{
DbgPrint("first: Our driver is unloading/r/n");
}

NTSTATUS DriverEntry(PDRIVER_OBJECT driver,PUNICODE_STRING reg_path)
{

DbgPrint("first: hello my future!");
driver->DriverUnload=DriverUnload;
return STATUS_SUCCESS;
}

稍微解释下,首先是包含的一个头文件,<ntddk.h>,程序在函数DriverEntry进入,这个函数相当于C中的main函数。DriverEntry函数有两个参数,其中driver我初步的理解就是指驱动程序本身,类似C++中的this,另外一个参数reg_path驱动程序在注册表的中的路径。DriverEntry有个NTSTATUS的返回值.DbgPrint类似于C中的printf。driver有个固有的DriverUnload函数,此函数在驱动卸载时调用,类似C++中的析构函数。任何函数使用前都需要声明。

为了测试该程序,需要用WDK或者DDK进行build,在build之前,在源文件的同一目录下,还需要有makefile和source两个文件。makefile文件的一般内容如下,不用更改。

 

!IF 0

Copyright (C) Microsoft Corporation, 1997 - 1998

Module Name:

    makefile.

!ENDIF

#
# DO NOT EDIT THIS FILE!!!  Edit ./sources. if you want to add a new source
# file to this component.  This file merely indirects to the real make file
# that is shared by all the components of Windows NT
#

#
# if building in a DDK environment
#
!IF defined(DDK_TARGET_OS)

#
# ensure that said build environment is at least Windows XP
# 0x500 == Windows 2000
# 0x501 == Windows XP
# 0x502 == Windows .NET
#
!    IF defined(_NT_TARGET_VERSION) && $(_NT_TARGET_VERSION)>=0x501
!        INCLUDE $(NTMAKEENV)/makefile.def
!    ELSE
!        message BUILDMSG: Warning : The sample "$(MAKEDIR)" is not valid for the current OS target.
!    ENDIF

!ELSE

#
# not a DDK environment, probably RAZZLE, so build
#
!    INCLUDE $(NTMAKEENV)/makefile.def

!ENDIF

而source文件的格式如下:

 

TARGETNAME=first
TARGETTYPE=DRIVER
SOURCES=first.c

 

每个程序根据实际情况,修改source。build后在objchk_wxp_x86/i386目录下有个first.sys文件,用其他工具进行加载就可以观测执行结果。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值