網上找了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