回复 9楼 非虫
嗬嗬,我还真写过一个。写得不好,请多指教。
[ 本帖最后由 东海一鱼 于 2009-8-18 07:39 编辑 ]
嗬嗬,我还真写过一个。写得不好,请多指教。
程序代码:
#include "precomp.h" #pragma hdrstop #pragma NDIS_INIT_FUNCTION(DriverEntry) #define LINKNAME_STRING L"\\DosDevices\\NDIS_INFACE" #define NTDEVICE_STRING L"\\Device\\MYNDIS_001" //我的设备:〉 UINT g_BanDing = 0; NDIS_HANDLE ProtHandle = NULL; NDIS_HANDLE DriverHandle = NULL; NDIS_MEDIUM MediumArray[4] = //支持的物理网络类型 { NdisMedium802_3, // Ethernet NdisMedium802_5, // Token-ring NdisMediumFddi, // Fddi NdisMediumWan // NDISWAN }; NDIS_SPIN_LOCK GlobalLock; PADAPT pAdaptList = NULL; LONG MiniportCount = 0; NDIS_HANDLE NdisWrapperHandle; NDIS_HANDLE NdisDeviceHandle = NULL; PDEVICE_OBJECT ControlDeviceObject = NULL; enum _DEVICE_STATE { PS_DEVICE_STATE_READY = 0, PS_DEVICE_STATE_CREATING, PS_DEVICE_STATE_DELETING } ControlDeviceState = PS_DEVICE_STATE_READY; NTSTATUS DriverEntry( IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath ) { NDIS_STATUS Status; NDIS_PROTOCOL_CHARACTERISTICS PChars; NDIS_MINIPORT_CHARACTERISTICS MChars; PNDIS_CONFIGURATION_PARAMETER Param; NDIS_STRING Name; Status = NDIS_STATUS_SUCCESS; NdisAllocateSpinLock(&GlobalLock); NdisMInitializeWrapper(&NdisWrapperHandle, DriverObject, RegistryPath, NULL); do { NdisZeroMemory(&MChars, sizeof(NDIS_MINIPORT_CHARACTERISTICS)); MChars.MajorNdisVersion = PASSTHRU_MAJOR_NDIS_VERSION; MChars.MinorNdisVersion = PASSTHRU_MINOR_NDIS_VERSION; MChars.InitializeHandler = MPInitialize; MChars.QueryInformationHandler = MPQueryInformation; MChars.SetInformationHandler = MPSetInformation; MChars.ResetHandler = MPReset; MChars.TransferDataHandler = MPTransferData; MChars.HaltHandler = MPHalt; #ifdef NDIS51_MINIPORT MChars.CancelSendPacketsHandler = MPCancelSendPackets; MChars.PnPEventNotifyHandler = MPDevicePnPEvent; MChars.AdapterShutdownHandler = MPAdapterShutdown; #endif // NDIS51_MINIPORT MChars.CheckForHangHandler = NULL; MChars.ReturnPacketHandler = MPReturnPacket; // // 采用多包发送 // MChars.SendHandler = NULL; // MPSend; MChars.SendPacketsHandler = MPSendPackets; Status = NdisIMRegisterLayeredMiniport(NdisWrapperHandle, &MChars, sizeof(MChars), &DriverHandle); if (Status != NDIS_STATUS_SUCCESS) { break; } #ifndef WIN9X NdisMRegisterUnloadHandler(NdisWrapperHandle, PtUnload); #endif // // 注册我的协议 // NdisZeroMemory(&PChars, sizeof(NDIS_PROTOCOL_CHARACTERISTICS)); PChars.MajorNdisVersion = PASSTHRU_PROT_MAJOR_NDIS_VERSION; PChars.MinorNdisVersion = PASSTHRU_PROT_MINOR_NDIS_VERSION; NdisInitUnicodeString(&Name, L"XNDF"); // 我的协议名称,哈哈 PChars.Name = Name; PChars.OpenAdapterCompleteHandler = PtOpenAdapterComplete; PChars.CloseAdapterCompleteHandler = PtCloseAdapterComplete; PChars.SendCompleteHandler = PtSendComplete; PChars.TransferDataCompleteHandler = PtTransferDataComplete; PChars.ResetCompleteHandler = PtResetComplete; PChars.RequestCompleteHandler = PtRequestComplete; PChars.ReceiveHandler = PtReceive; PChars.ReceiveCompleteHandler = PtReceiveComplete; PChars.StatusHandler = PtStatus; PChars.StatusCompleteHandler = PtStatusComplete; PChars.BindAdapterHandler = PtBindAdapter; PChars.UnbindAdapterHandler = PtUnbindAdapter; PChars.UnloadHandler = PtUnloadProtocol; PChars.ReceivePacketHandler = PtReceivePacket; PChars.PnPEventHandler= PtPNPHandler; NdisRegisterProtocol(&Status, &ProtHandle, &PChars, sizeof(NDIS_PROTOCOL_CHARACTERISTICS)); if (Status != NDIS_STATUS_SUCCESS) { NdisIMDeregisterLayeredMiniport(DriverHandle); break; } NdisIMAssociateMiniport(DriverHandle, ProtHandle); } while (FALSE); if (Status != NDIS_STATUS_SUCCESS) { NdisTerminateWrapper(NdisWrapperHandle, NULL); } return(Status); } NDIS_STATUS PtRegisterDevice( VOID ) { NDIS_STATUS Status = NDIS_STATUS_SUCCESS; UNICODE_STRING DeviceName; UNICODE_STRING DeviceLinkUnicodeString; PDRIVER_DISPATCH DispatchTable[IRP_MJ_MAXIMUM_FUNCTION]; UINT i; DBGPRINT(("==>PtRegisterDevice\n")); NdisAcquireSpinLock(&GlobalLock); ++MiniportCount; if (1 == MiniportCount) { ASSERT(ControlDeviceState != PS_DEVICE_STATE_CREATING); while (ControlDeviceState != PS_DEVICE_STATE_READY) { NdisReleaseSpinLock(&GlobalLock); NdisMSleep(1); NdisAcquireSpinLock(&GlobalLock); } ControlDeviceState = PS_DEVICE_STATE_CREATING; NdisReleaseSpinLock(&GlobalLock); for (i = 0; i < IRP_MJ_MAXIMUM_FUNCTION; i++) { DispatchTable[i] = PtDispatch; } NdisInitUnicodeString(&DeviceName, NTDEVICE_STRING); NdisInitUnicodeString(&DeviceLinkUnicodeString, LINKNAME_STRING); // // 创建设备对象并绑定驱动回调函数组 // Status = NdisMRegisterDevice( NdisWrapperHandle, &DeviceName, &DeviceLinkUnicodeString, &DispatchTable[0], &ControlDeviceObject, &NdisDeviceHandle ); NdisAcquireSpinLock(&GlobalLock); ControlDeviceState = PS_DEVICE_STATE_READY; } NdisReleaseSpinLock(&GlobalLock); DBGPRINT(("<==PtRegisterDevice: %x\n", Status)); return (Status); }
[ 本帖最后由 东海一鱼 于 2009-8-18 07:39 编辑 ]
举世而誉之而不加劝,举世而非之而不加沮,定乎内外之分,辩乎荣辱之境,斯已矣。彼其于世未数数然也。