注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

从C开始

 
 
 

日志

 
 

驱动改名,设备名联动  

2011-08-15 09:23:55|  分类: 驱动编程 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
#include <ntddk.h>

void GetLastName(WCHAR * lpszOutString,const WCHAR* lpszInString)
{
int i,j;

j=0;
for(i=wcslen(lpszInString)-1;i>=0;i--)
{
if(lpszInString[i]==L'\\')
{
j=i;
break;
}
}

if(j!=0)
wcscpy(lpszOutString,&lpszInString[j+1]);
else
wcscpy(lpszOutString,lpszInString);
}

NTSTATUS DrvShutDown(IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp)
{
Irp->IoStatus.Information = 0;
Irp->IoStatus.Status = STATUS_SUCCESS;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return STATUS_SUCCESS;
}

BOOLEAN DeviceControl( IN PFILE_OBJECT FileObject, IN BOOLEAN bWait,
 IN PVOID InputBuffer, IN ULONG InputBufferLength,
 OUT PVOID OutputBuffer, IN ULONG OutputBufferLength,
 IN ULONG IoControlCode, OUT PIO_STATUS_BLOCK IoStatus,
 IN PDEVICE_OBJECT DeviceObject)
{
//////////////////////////////////////////////////////////////////////////
//
//DeviceControl
//we do the device control manage here
//arg1: FileObject :FileObject of the Irp
//arg2: bWait : ??
//arg3: InputBuffer :Input buffer of the DeviceIoControl
//arg4: InputBufferLength :Input Buffer Length of the DeviceIoControl
//arg5: OutputBuffer : Output Buffer of the DeviceIoControl
//arg6: OutputBufferLenght :Output Buffer Length of the DeviceIoControl
//arg7: IoControlCode :Io Control Code of the DeviceIoControl
//arg8: IoStatus :Io status of the Irp
//arg9: DeviceObject :our device object
//
//////////////////////////////////////////////////////////////////////////
IoStatus->Status = STATUS_SUCCESS ;
IoStatus->Information = 0 ;
//set the status success
//set the information to 0 
switch( IoControlCode){
default:
IoStatus->Status = STATUS_INVALID_DEVICE_REQUEST;
//return error
break;
}
return TRUE ;
}

NTSTATUS DrvDispatch(IN PDEVICE_OBJECT DeviceObject,IN PIRP pIrp)
{
//////////////////////////////////////////////////////////////////////////
//
//DrvDispatch
//we do the device routine manage here
//arg1: DeviceObject : DeviceObject which the caller opened
//arg2: pIrp :Irp send to our device
//
//////////////////////////////////////////////////////////////////////////
NTSTATUS status;
PIO_STACK_LOCATION irpStack;
PVOID inputBuffer, outputBuffer;
ULONG inputBufferLength, outputBufferLength;
ULONG ioControlCode;
//if call form old device object,we do nothing
pIrp->IoStatus.Status = STATUS_SUCCESS;
pIrp->IoStatus.Information = 0;
irpStack = IoGetCurrentIrpStackLocation( pIrp);
//get the current Irp stack location 
if (irpStack -> MajorFunction == IRP_MJ_DEVICE_CONTROL )
{
//we only need the device io control
inputBuffer = pIrp->AssociatedIrp.SystemBuffer;
inputBufferLength = irpStack->Parameters.DeviceIoControl.InputBufferLength;
outputBuffer = pIrp->AssociatedIrp.SystemBuffer;
outputBufferLength = irpStack->Parameters.DeviceIoControl.OutputBufferLength;
//system use the same buffer in device io control 
ioControlCode = irpStack->Parameters.DeviceIoControl.IoControlCode;
if( (ioControlCode&3) == METHOD_NEITHER){
outputBuffer = pIrp->UserBuffer;
}
DeviceControl( irpStack->FileObject, TRUE,
inputBuffer, inputBufferLength,
outputBuffer, outputBufferLength,
ioControlCode, &pIrp->IoStatus, DeviceObject);
}
IoCompleteRequest( pIrp, IO_NO_INCREMENT);
status = STATUS_SUCCESS;
return status;
}

NTSTATUS DriverEntry(PDRIVER_OBJECT MyDriverObject, PUNICODE_STRING pRegistryString)
{
NTSTATUS status = STATUS_SUCCESS;
UNICODE_STRING MyDeviceName ; 
UNICODE_STRING MySymbolicLinkName ;
PDEVICE_OBJECT MyDeviceObject ;
NTSTATUS stat ;
WCHAR deviceName[256];
WCHAR devicelink[256];
WCHAR name[256];

RtlZeroMemory(name,512);
wcscpy(devicelink,L"\\DosDevices\\");
wcscpy(deviceName,L"\\Device\\");
GetLastName(name,pRegistryString->Buffer);
wcscat(deviceName,name);
wcscat(devicelink,name);


RtlInitUnicodeString(&MyDeviceName , deviceName) ;
stat = IoCreateDevice( MyDriverObject ,
0x100 ,
&MyDeviceName,
FILE_DEVICE_UNKNOWN ,
0,
FALSE ,
&MyDeviceObject ) ;
if (!NT_SUCCESS(stat ))
{
return stat ;
}
//create our device
RtlInitUnicodeString(&MySymbolicLinkName , devicelink);
stat = IoCreateSymbolicLink(&MySymbolicLinkName ,&MyDeviceName);
//create our symbolic link
if (!NT_SUCCESS(stat))
{
IoDeleteDevice(MyDeviceObject);
return stat ;

MyDriverObject->DriverUnload = 0;
MyDriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DrvDispatch ;
MyDriverObject->MajorFunction[IRP_MJ_CREATE] = DrvDispatch ;
MyDriverObject->MajorFunction[IRP_MJ_CLOSE] = DrvDispatch ;
MyDriverObject->MajorFunction[IRP_MJ_SHUTDOWN] = DrvShutDown;
IoRegisterShutdownNotification(MyDeviceObject);
return STATUS_SUCCESS;
}
  评论这张
 
阅读(923)| 评论(1)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018