驱动程序的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文件,用其他工具进行加载就可以观测执行结果。