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