阅读:2207回复:2
有Ndis驱动程序和应用程序程序通讯的完整的例子吗?
在线等待。。。。
|
|
最新喜欢:antspo... |
沙发#
发布于:2003-08-13 13:29
// device.c
// ///////////////////////////////////////////////////////////////////////////// //#define TRACE_ON 0 #include \"main.h\" #include \"device.h\" #if NDIS_WDM NDIS_STRING DeviceName = NDIS_STRING_CONST(\"\\\\Device\\\\DummyNic.sys\"); NDIS_STRING DosDeviceName = NDIS_STRING_CONST(\"\\\\DosDevices\\\\DummyNic.sys\"); static PDRIVER_OBJECT gpDriverObject = NULL; static PDEVICE_OBJECT gpDeviceObject = NULL; static PDRIVER_DISPATCH MajorFunction[IRP_MJ_MAXIMUM_FUNCTION + 1]; ///////////////////////////////////////////////////////////////////////////// // // 函数名称: DispatchDefault // 功能描述: 处理其它的IRP_MJ_XXX消息 // 输入参数: // DeviceObject: 设备对象 // Irp: IRP包 // 返回结果: 成功返回STATUS_SUCCESS,否则STATUS_UNSUCCESSFUL // NTSTATUS DispatchDefault( IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp ) { PEXTENSION pDE = (PEXTENSION)DeviceObject->DeviceExtension; PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation( Irp ); //TRACE_IN(\"DispatchDefault\"); if( pDE->Signature != SIGNATURE ) { //TRACE_OUT(\"Forward to old function\", NO_RETURN); return (*MajorFunction[IrpSp->MajorFunction])(DeviceObject, Irp); } Irp->IoStatus.Status = STATUS_UNSUCCESSFUL; IoCompleteRequest( Irp, IO_NO_INCREMENT ); //TRACE_OUT(\"Failure\", STATUS_UNSUCCESSFUL); return STATUS_UNSUCCESSFUL; } ///////////////////////////////////////////////////////////////////////////// // // 函数名称: DispatchPnp // 功能描述: 处理IRP_MJ_PNP消息 // 输入参数: // DeviceObject: 设备对象 // Irp: IRP包 // 返回结果: 成功返回STATUS_SUCCESS,否则STATUS_UNSUCCESSFUL // NTSTATUS DispatchPnp( IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp ) { PEXTENSION pDE = (PEXTENSION)DeviceObject->DeviceExtension; NTSTATUS Status = STATUS_SUCCESS; //函数返回值 PIO_STACK_LOCATION IrpStack; //本设备栈 PDEVICE_CAPABILITIES pdc; //设备属性 PADAPTER Adapter; TRACE_IN(\"DispatchPnp\"); //查看是否可以处理该PNP消息 if( pDE->Signature != SIGNATURE ) { TRACE0(\"Forward to old function\"); Status=(*MajorFunction[IRP_MJ_PNP])(DeviceObject, Irp); TRACE_OUT(\"\",Status); return Status; } Irp->IoStatus.Status = STATUS_SUCCESS; IoCompleteRequest( Irp, IO_NO_INCREMENT ); TRACE_OUT(\"\", Irp->IoStatus.Status); return Irp->IoStatus.Status; } ///////////////////////////////////////////////////////////////////////////// // // 函数名称: DispatchPower // 功能描述: 处理IRP_MJ_POWER消息 // 输入参数: // DeviceObject: 设备对象 // Irp: IRP包 // 返回结果: 成功返回STATUS_SUCCESS,否则STATUS_UNSUCCESSFUL // NTSTATUS DispatchPower( IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp ) { PEXTENSION pDE = (PEXTENSION)DeviceObject->DeviceExtension; PADAPTER Adapter; NTSTATUS ntStatus; //返回值 TRACE_IN(\"DispatchPower\"); if( pDE->Signature != SIGNATURE ) { TRACE_OUT(\"Forward to old function\", NO_RETURN); return (*MajorFunction[IRP_MJ_POWER])(DeviceObject, Irp); } else return DispatchDefault( DeviceObject, Irp ); } ///////////////////////////////////////////////////////////////////////////// // // 函数名称: DispatchCreate // 功能描述: 处理IRP_MJ_CREATE消息 // 输入参数: // DeviceObject: 设备对象 // Irp: IRP包 // 返回结果: 成功返回STATUS_SUCCESS,否则STATUS_UNSUCCESSFUL // NTSTATUS DispatchCreate( IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp ) { PEXTENSION pDE = (PEXTENSION)DeviceObject->DeviceExtension; //TRACE_IN(\"DispatchCreate\"); if( pDE->Signature != SIGNATURE ) { //TRACE_OUT(\"Forward to old function\", NO_RETURN); return (*MajorFunction[IRP_MJ_CREATE])(DeviceObject, Irp); } else if( DeviceObject != gpDeviceObject ) return DispatchDefault( DeviceObject, Irp ); Irp->IoStatus.Status = STATUS_SUCCESS; IoCompleteRequest(Irp, IO_NO_INCREMENT); //TRACE_OUT(\"\", Irp->IoStatus.Status); return Irp->IoStatus.Status; } ///////////////////////////////////////////////////////////////////////////// // // 函数名称: DispatchClose // 功能描述: 处理IRP_MJ_CLOSE消息 // 输入参数: // DeviceObject: 设备对象 // Irp: IRP包 // 返回结果: 总是返回STATUS_SUCCESS // NTSTATUS DispatchClose( IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp ) { PEXTENSION pDE = (PEXTENSION)DeviceObject->DeviceExtension; //TRACE_IN(\"DispatchClose\"); if( pDE->Signature != SIGNATURE ) { //TRACE_OUT(\"Forward to old function\", NO_RETURN); return (*MajorFunction[IRP_MJ_CLOSE])(DeviceObject, Irp); } Irp->IoStatus.Status = STATUS_SUCCESS; IoCompleteRequest(Irp, IO_NO_INCREMENT); //TRACE_OUT(\"\", Irp->IoStatus.Status); return Irp->IoStatus.Status; } ///////////////////////////////////////////////////////////////////////////// // // 函数名称: DispatchIoControl // 功能描述: 处理IRP_MJ_DEVICE_CONTROL消息 // 输入参数: // DeviceObject: 设备对象 // Irp: IRP包 // 返回结果: STATUS_XXXXXX // NTSTATUS DispatchIoControl( IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp ) { PEXTENSION pDE = (PEXTENSION)DeviceObject->DeviceExtension; PADAPTER Adapter = pDE->Adapter; PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp); ULONG Oid; PVOID InBuffer, OutBuffer; UINT InBufferLength, OutBufferLength; ULONG Answer; PVOID Source = (PVOID)&Answer; ULONG Length = sizeof Answer; UINT Index; //TRACE_IN(\"DeviceIoControl\"); if( pDE->Signature != SIGNATURE ) { return (*MajorFunction[IRP_MJ_DEVICE_CONTROL])(DeviceObject, Irp); } else if( DeviceObject != gpDeviceObject ) return DispatchDefault( DeviceObject, Irp ); Irp->RequestorMode = KernelMode; Irp->IoStatus.Status = NDIS_STATUS_SUCCESS; Oid = IrpSp->Parameters.DeviceIoControl.IoControlCode; InBuffer = IrpSp->Parameters.DeviceIoControl.Type3InputBuffer; InBufferLength = IrpSp->Parameters.DeviceIoControl.InputBufferLength; OutBuffer = Irp->UserBuffer; OutBufferLength = IrpSp->Parameters.DeviceIoControl.OutputBufferLength; switch( Oid ) { default: Irp->IoStatus.Status = NDIS_STATUS_INVALID_OID; TRACE1(\"Invalid Oid(0x%08X)\", Oid); break; } if( Irp->IoStatus.Status == NDIS_STATUS_SUCCESS ) { Irp->IoStatus.Information = Length; if( Length > OutBufferLength || Source == NULL ) Irp->IoStatus.Status = NDIS_STATUS_INVALID_LENGTH; else if( Length > 0 ) NdisMoveMemory(OutBuffer, Source, Length); } IoCompleteRequest(Irp, IO_NO_INCREMENT); //TRACE_OUT(\"\", Irp->IoStatus.Status); return Irp->IoStatus.Status; } ///////////////////////////////////////////////////////////////////////////// // // 函数名称: DeviceInitialize // 功能描述: 登记IRP_MJ_XXX消息函数入口 // 输入参数: // DriverObject: 驱动对象 // 返回结果: 总是返回TRUE // BOOLEAN DeviceInitialize( IN PDRIVER_OBJECT DriverObject ) { int i; TRACE_IN(\"DeviceInitialize\"); // // 保存旧入口 // NdisMoveMemory( MajorFunction, DriverObject->MajorFunction, sizeof MajorFunction ); // // 设置新入口 // for( i = 0; i < IRP_MJ_MAXIMUM_FUNCTION; i++ ) DriverObject->MajorFunction = DispatchDefault; //DriverObject->MajorFunction[IRP_MJ_PNP] = DispatchPnp; //DriverObject->MajorFunction[IRP_MJ_POWER] = DispatchPower; DriverObject->MajorFunction[IRP_MJ_CREATE] = DispatchCreate; DriverObject->MajorFunction[IRP_MJ_CLOSE] = DispatchClose; DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DispatchIoControl; //DriverObject->DriverExtension->AddDevice = MsgAddDevice; gpDriverObject = DriverObject; TRACE_OUT(\"\", TRUE); return TRUE; } ///////////////////////////////////////////////////////////////////////////// // // 函数名称: DeviceCreate // 功能描述: 创建设备对象及其符号链接 // 输入参数: // Adapter: 设备上下文 // 返回结果: 成功返回TRUE,否则FALSE // BOOLEAN DeviceCreate( IN PADAPTER Adapter ) { NTSTATUS Status; PEXTENSION pDE; GUID DeviceGuid={0xa4a8e3a4,0x4a95,0x8484,{0x86, 0xca, 0xcc, 0x12, 0xf7, 0xa4, 0xc6, 0x39}}; PDEVICE_OBJECT PhysicalDeviceObject = NULL; // // 如果设备对象已经创建…… // if( gpDeviceObject != NULL ) { TRACE0(\"Device created already!\"); return FALSE; } // // 创建设备对象 // Status = IoCreateDevice( gpDriverObject, sizeof *pDE, &DeviceName, FILE_DEVICE_UNKNOWN, 0, 0, &gpDeviceObject ); if( Status == STATUS_OBJECT_NAME_COLLISION || Status == STATUS_OBJECT_NAME_EXISTS ) { gpDeviceObject = NULL; TRACE1(\"Fail to create device(0x%08X)\", Status); return FALSE; } // // 为设备对象创建符号链接 // IoCreateSymbolicLink(&DosDeviceName, &DeviceName); // // 设置设备对象属性 // gpDeviceObject->Flags &= ~DO_DEVICE_INITIALIZING; gpDeviceObject->Flags |= DO_BUFFERED_IO; // // 设置设备上下文 // pDE = (PEXTENSION)gpDeviceObject->DeviceExtension; pDE->Adapter = Adapter; pDE->Signature = SIGNATURE; return TRUE; } ///////////////////////////////////////////////////////////////////////////// // // 函数名称: DeviceDestroy // 功能描述: 销毁设备对象及其符号链接 // 输入参数: // Adapter: 设备上下文 // 返回结果: 成功返回TRUE,否则FALSE // BOOLEAN DeviceDestroy( IN PADAPTER Adapter ) { PEXTENSION pDE ; TRACE_IN (\"DeviceDestroy\"); // // 如果设备对象已经销毁…… // if( gpDeviceObject == NULL ) return FALSE; pDE = ( PEXTENSION )gpDeviceObject->DeviceExtension; if( pDE->Adapter != Adapter ) return FALSE; // // 销毁设备对象的符号链接名 // IoDeleteSymbolicLink(&DosDeviceName); //将设备自设备栈中删除 //wIoDetachDevice(pDE->Adapter->StackDeviceObject); //释放Adapter 内存 NdisFreeMemory(Adapter, sizeof(ADAPTER), 0); // // 销毁设备对象 // IoDeleteDevice(gpDeviceObject); gpDeviceObject = NULL; TRACE_OUT(\"\",TRUE); return TRUE; } #endif // NDIS_WDM /* end of device.c */ // main.c // ///////////////////////////////////////////////////////////////////////////// #define TRACE_ON 1 #include \"main.h\" #include \"miniport.h\" #include \"device.h\" UNICODE_STRING RegistryName; static VOID ( *gpUnloadFunc )( IN PDRIVER_OBJECT ); extern NDIS_STATUS DriverEntry( IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath ); VOID DriverUnload( IN PDRIVER_OBJECT DriverObject ); #pragma hdrstop #pragma NDIS_INIT_FUNCTION(DriverEntry) NDIS_STATUS DriverEntry( IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath ) { NDIS_STATUS Status; NDIS_HANDLE WrapperHandle; NDIS_MINIPORT_CHARACTERISTICS MiniportChars; ANSI_STRING AnsiName; NTSTATUS ntStatus; TRACE_IN(\"DriverEntry\"); NdisMInitializeWrapper(&WrapperHandle, DriverObject, RegistryPath, NULL); NdisZeroMemory(&MiniportChars, sizeof MiniportChars); MiniportChars.MajorNdisVersion = MAJOR_NDIS_VERSION; MiniportChars.MinorNdisVersion = MINOR_NDIS_VERSION; MiniportChars.CheckForHangHandler = MiniportCheckForHang; // MiniportChars.DisableInterruptHandler = MiniportDisableInterrupt; // MiniportChars.EnableInterruptHandler = MiniportEnableInterrupt; MiniportChars.HaltHandler = MiniportHalt; // MiniportChars.HandleInterruptHandler = MiniportHandleInterrupt; MiniportChars.InitializeHandler = MiniportInitialize; // MiniportChars.ISRHandler = MiniportIsr; MiniportChars.QueryInformationHandler = MiniportQueryInformation; // MiniportChars.ReconfigureHandler = MiniportReconfigure; MiniportChars.ResetHandler = MiniportReset; MiniportChars.SendHandler = MiniportSend; MiniportChars.SetInformationHandler = MiniportSetInformation; // MiniportChars.TransferDataHandler = MiniportTransferData; #if(MAJOR_NDIS_VERSION >= 4) MiniportChars.ReturnPacketHandler = MiniportReturnPacket; // MiniportChars.SendPacketsHandler = MiniportSendPackets; // MiniportChars.AllocateCompleteHandler = MiniportAllocateComplete; #endif Status = NdisMRegisterMiniport( WrapperHandle, &MiniportChars, sizeof MiniportChars ); if( !NT_SUCCESS(Status) ) { TRACE_OUT(\"Fail to register miniport\", Status); NdisTerminateWrapper(WrapperHandle, NULL); return NDIS_STATUS_FAILURE; } if( !DeviceInitialize(DriverObject) ) { TRACE_OUT(\"Fail to initialize device\", NDIS_STATUS_FAILURE); NdisTerminateWrapper(WrapperHandle, NULL); return NDIS_STATUS_FAILURE; } // 做退出时的释放内存工作 gpUnloadFunc = DriverObject->DriverUnload; DriverObject->DriverUnload = DriverUnload; // 保存当前的注册表位置 ntStatus = RtlUnicodeStringToAnsiString( &AnsiName, RegistryPath, TRUE ); if( NT_SUCCESS( ntStatus ) ) { ntStatus = RtlAnsiStringToUnicodeString( &RegistryName, &AnsiName, TRUE ); RtlFreeAnsiString( &AnsiName ); } // 如果名称转换失败则置初值 if( !NT_SUCCESS( ntStatus ) ) { RegistryName.Buffer = NULL; RegistryName.Length = 0; } TRACE_OUT(\"Success\", NDIS_STATUS_SUCCESS); return NDIS_STATUS_SUCCESS; } VOID DriverUnload( IN PDRIVER_OBJECT DriverObject ) { TRACE_IN( \"DriverUnload\" ); RtlFreeUnicodeString( &RegistryName ); if( gpUnloadFunc ) ( *gpUnloadFunc )( DriverObject ); TRACE_OUT( \"\", NO_RETURN); } #if DBG VOID HexView( IN PUCHAR Buffer, IN UINT Length ) { INT i; ULONG Frag = 0; #if defined( HEX_ALIGN_WORD ) #define HEX_FORMAT \"%04x \" #define HEX_SIZE 2 #define HEX_ALIGN 12 #elif defined( HEX_ALIGN_DWORD ) #define HEX_FORMAT \"%08lx \" #define HEX_SIZE 4 #define HEX_ALIGN 8 #else #define HEX_FORMAT \"%02x \" #define HEX_SIZE 1 #define HEX_ALIGN 16 #endif while( Length > 0 ) { for( i = HEX_ALIGN; ( i > 0 ) && ( Length > 0 ); i-- ) { if( Length < HEX_SIZE ) // fragment { Frag = 0; NdisMoveMemory( &Frag, Buffer, Length ); Length = 0; } else // normal { NdisMoveMemory( &Frag, Buffer, HEX_SIZE ); Buffer += HEX_SIZE; Length -= HEX_SIZE; } DEBUGTRACE1( HEX_FORMAT, Frag ); } DEBUGTRACE0( \"\\r\\n\" ); } DEBUGTRACE0( \"\\r\\n\" ); } #ifdef DBG VOID OutputValue(IN PVOID pBuffer, IN ULONG dwSize) { ULONG i; PUCHAR pStr; pStr = (PUCHAR)pBuffer; DbgPrint(\"The data in address %x is \\r\\n\",pStr); for(i = 0; i < dwSize; i++) { DbgPrint(\"%02x \",pStr); } DbgPrint(\"\\r\\n\"); return; } #endif #endif /* end of main.c */ |
|
板凳#
发布于:2003-08-13 13:30
以上代码已经包括了同应用层通讯的所有代码,其它你自己扩充吧
|
|