如何编译驱动程序

如何编译驱动程序 [转贴 2008-07-30 11:29:26]    
我顶  字号:  
驱动的编译和上层应用程序的编译完全不同,作为初学者应该先了解一下,即使你还不懂得怎么写驱动程序。

       首先安装DDK,然后随便找一个例子来测试。在菜单中找到BUILD环境菜单执行,不同的系统要使用不同的BUILD环境。会打开一个DOS窗口,这时CD到那个例子程序,输入 build –cZ回车就可以了。 驱动程序都是用一个由DDK提供的叫build.exe的工具编译的。此程序以一个名为SOURCES的文件作为输入,该文件中包含目标可执行文件的名称、类型和要创建的可执行文件的路径,注意这个文件没有后缀名。

SOURCES的文件格式:

TARGETNAME=drivername ,

-          本参数用于指定生成的设备驱动程序名称(不需后缀名),所产生的文件

-          为drivername.sys.

 

TARGETPATH=./lib

-          本参数用于指定生成的设备驱动程序所存放的路径. 一般采用./lib.

 

TARGETTYPE=DRIVER

-          build能够生成许多不同的目标对象,设备驱动程序一般选用 DRIVER.

 

INCLUDES=path1;path2;...

-          本参数是可选的, 用于指定其他的#include文件的搜索路径.   

 

TARGETLIBS=lib1;lib2;...

-          本参数是可选的, 用于指定其他的lib库文件的搜索路径.   

 

SOURCES=file1.c  file2.c ...  

-          本参数用于指定需被编译的全部源文件名称, 后缀名不能省略,文件名之间用空格分开. 

SOURCES文件是必需的,如果没有它则表示没有任何源文件需要编译。

如果要换行可以用 ‘/’ 符号,表示对上一行的继续。

 

也可以创建DIRS文件,DIRS文件用于指定在当前目录下必须创建的子目录。

DIRS文件格式:

DIRS文件的内容由一系列用空格分开的目录名组成

DIRS = /

      subdir1 /

      subdir2 /

      subdir3

DIRS文件是可选的。

 

       有的时候,会提示找不到依赖的文件(.h,.lib 之类),其实设置好 source 文件的

INCLUDES和TARGETLIBS就可以,我第一次编译时就碰到这个问题,和VC环境区别较大,但习惯就好。

 

 

网上找了N多资料,说得人晕头转向的,都是TMD资深顾问级别的文章,最后简单几步也可以搞定,过程如下:

我的SDK本来就是装好的,再装个DDK,就是重装系统,只要DDK目录还在,重新指定下即可,不用重装DDK
DDK目录为: f:/WINDDK/3790.1830

以下以HelloWorld为例
-----------------------------------------HelloWorld.h---------------------------------------------------
#ifndef __HELLOWORLD_H__
#define __HELLOWORLD_H__
#include <ntddk.h>
#define DEVICE_HELLO_INDEX 0x860
#define START_HELLOWORLD CTL_CODE( FILE_DEVICE_UNKNOWN,DEVICE_HELLO_INDEX,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define STOP_HELLOWORLD CTL_CODE(FILE_DEVICE_UNKNOWN,DEVICE_HELLO_INDEX+1,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define NT_DEVICE_NAME L"//Device//HelloWorld"
#define DOS_DEVICE_NAME L"//DosDevices//HelloWorld"
NTSTATUS HelloWorldDispatch(IN PDEVICE_OBJECT DeviceObject,IN PIRP pIrp);
VOID HelloWorldUnload(IN PDRIVER_OBJECT DriverObject);
#endif
-----------------------------------------HelloWorld.c---------------------------------------------------
#ifndef __HELLOWORLD_C__
#define __HELLOWORLD_C__
#define DEBUGMSG
#include "HelloWorld.h"

NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING RegistryPath)
{
    NTSTATUS ntStatus=STATUS_SUCCESS;
    PDEVICE_OBJECT IpDeviceObject=NULL;
    UNICODE_STRING DeviceNameString;
    UNICODE_STRING DeviceLinkString;

    #ifdef DEBUGMSG
        DbgPrint("hi, Starting DriverEntry()/n");
    #endif
        
    RtlInitUnicodeString(&DeviceNameString,NT_DEVICE_NAME);
    ntStatus=IoCreateDevice(DriverObject,0,&DeviceNameString,FILE_DEVICE_UNKNOWN,0,FALSE,&IpDeviceObject);
    if(!NT_SUCCESS(ntStatus))
    {
        #ifdef DEBUGMSG
                DbgPrint("hi, Error IoCreateDevice()/n");
        #endif
        goto Error;
    }
    RtlInitUnicodeString(&DeviceLinkString,DOS_DEVICE_NAME);
    ntStatus=IoCreateSymbolicLink(&DeviceLinkString,&DeviceNameString);
    if(!NT_SUCCESS(ntStatus))
    {
        #ifdef DEBUGMSG
                DbgPrint("hi, Error IoCreateSymbolicLink()/n");
        #endif
        goto Error;
    }
    DriverObject->MajorFunction[IRP_MJ_CREATE]=HelloWorldDispatch;
    DriverObject->MajorFunction[IRP_MJ_CLOSE]=HelloWorldDispatch;
    DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL]=HelloWorldDispatch;
    DriverObject->DriverUnload=HelloWorldUnload;
    return ntStatus;
Error:
    #ifdef DEBUGMSG
        DbgPrint("hi, Error DriverEntry()/n");
    #endif
    return ntStatus;
}
NTSTATUS HelloWorldDispatch(IN PDEVICE_OBJECT DeviceObject,IN PIRP pIrp)
{
    NTSTATUS ntStatus=STATUS_SUCCESS;
    ULONG IoControlCodes=0;
    PIO_STACK_LOCATION IrpStack=NULL;
    pIrp->IoStatus.Status=STATUS_SUCCESS;
    pIrp->IoStatus.Information=0;
    #ifdef DEBUGMSG
        DbgPrint("hi, Starting HelloWorldDispatch()/n");
    #endif
    IrpStack=IoGetCurrentIrpStackLocation(pIrp);
    switch(IrpStack->MajorFunction)
    {
    case IRP_MJ_CREATE:
        #ifdef DEBUGMSG
                DbgPrint("hi, IRP_MJ_CREATE/n");
        #endif
        break;
    case IRP_MJ_CLOSE:
        #ifdef DEBUGMSG
                DbgPrint("hi, IRP_MJ_CLOSE/n");
        #endif
        break;
    case IRP_MJ_DEVICE_CONTROL:
        #ifdef DEBUGMSG
                DbgPrint("hi, IRP_MJ_DEVICE_CONTROL/n");
        #endif
        IoControlCodes=IrpStack->Parameters.DeviceIoControl.IoControlCode;
        switch(IoControlCodes)
        {
        case START_HELLOWORLD:
            DbgPrint("hi, Starting /"Hello World /"/n");
            break;
        case STOP_HELLOWORLD:
            DbgPrint("hi, Stoping /"Hello World /"/n");
            break;
        default:
            pIrp->IoStatus.Status=STATUS_INVALID_PARAMETER;
            break;
        }
        break;
    default:
        break;
    }
    ntStatus=pIrp->IoStatus.Status;
    IoCompleteRequest(pIrp,IO_NO_INCREMENT);
    return ntStatus;
}

VOID HelloWorldUnload(IN PDRIVER_OBJECT DriverObject)
{
    UNICODE_STRING DeviceLinkString;
    PDEVICE_OBJECT DeviceObjectTemp1=NULL;
    PDEVICE_OBJECT DeviceObjectTemp2=NULL;
    #ifdef DEBUGMSG
        DbgPrint("hi,Starting HelloWorldUnload()/n");
    #endif
    RtlInitUnicodeString(&DeviceLinkString,DOS_DEVICE_NAME);
    IoDeleteSymbolicLink(&DeviceLinkString);
    if(DriverObject)
    {
        DeviceObjectTemp1=DriverObject->DeviceObject;
        while(DeviceObjectTemp1)
        {
            DeviceObjectTemp2=DeviceObjectTemp1;
            DeviceObjectTemp1=DeviceObjectTemp1->NextDevice;
            IoDeleteDevice(DeviceObjectTemp2);
        }
    }
}
#endif
----------------------------------------Makefile----------------------------------------
#
#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 DRIVER COMPONENTS OF THE WINDOWS NT DDK
#
!INCLUDE $(NTMAKEENV)/makefile.def
-----------------------------------------Sources----------------------------------------
TARGETNAME=HelloWorld
TARGETPATH=.
TARGETTYPE=DRIVER
SOURCES=HelloWorld.c

共四个文件:HelloWorld.c, Makefile, Sources, HelloWorld.h
到命令行执行: 
f:/WINDDK/3790.1830/bin/setenv.bat f:/WINDDK/3790.1830 chk
helloworld目录下执行:
build

提示如下说明成功了:
BUILD: Adding /Y to COPYCMD so xcopy ops won't hang.
BUILD: Using 2 child processes
BUILD: Object root set to: ==> objchk_wnet_x86
BUILD: Compile and Link for i386
BUILD: Loading f:/WINDDK/3790.1830/build.dat...
BUILD: Computing Include file dependencies:
BUILD: Examining c:/sample directory for files to compile.
    c:/sample - 1 source files (116 lines)
BUILD: Saving f:/WINDDK/3790.1830/build.dat...
BUILD: Compiling (NoSync) c:/sample directory
1>Compiling - helloworld.c for i386
BUILD: Compiling  c:/sample directory
BUILD: Linking c:/sample directory
1>Linking Executable - i386/helloworld.sys for i386
BUILD: Done

    2 files compiled
    1 executable built

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值