{"id":149,"date":"2009-09-12T20:27:39","date_gmt":"2009-09-13T04:27:39","guid":{"rendered":"http:\/\/www.abin.cn\/?p=149"},"modified":"2009-09-12T20:29:51","modified_gmt":"2009-09-13T04:29:51","slug":"%e4%b8%80%e4%b8%aa%e5%8a%a0%e5%af%86%e7%8b%97%e7%9a%84%e9%a9%b1%e5%8a%a8%e6%ba%90%e7%a0%81","status":"publish","type":"post","link":"https:\/\/www.abin.cn\/?p=149","title":{"rendered":"\u4e00\u4e2a\u52a0\u5bc6\u72d7\u7684\u9a71\u52a8\u6e90\u7801"},"content":{"rendered":"<p>\u4e00\u4e2a\u52a0\u5bc6\u72d7\u7684\u9a71\u52a8\u6e90\u7801<\/p>\n<p><!--more--><\/p>\n<pre lang=\"c\">\r\n#define DONG_NT_DEVICE_NAME L\"\\\\Device\\\\DongLpt\"\r\n#define DONG_NT_PORT_DEVICE_NAME L\"\\\\Device\\\\ParallelPort\"\r\n#define DONG_WIN32_DEVICE_NAME L\"\\\\DosDevices\\\\DongLpt\"\r\n#define DONG_DOS_DEVICES L\"\\\\DosDevices\\\\\"\r\n#define DONG_DRIVER_NAME L\"DongLpt\"\r\n\r\n#define DONG_MAX_NAME_LENGTH 50\r\n\r\n\r\nNTSTATUS\r\nDriverEntry(\r\nIN PDRIVER_OBJECT pDriverObject,\r\nIN PUNICODE_STRING pRegistryPath\r\n)\r\n{\r\nULONG NtDeviceNumber, NumParallelPorts;\r\nNTSTATUS status = STATUS_SUCCESS;\r\n\r\nDongInitializeEventLog(pDriverObject);\r\n\r\n\/\/ Export other driver entry points...\r\npDriverObject->DriverUnload = DongDriverUnload;\r\n\r\npDriverObject->MajorFunction[ IRP_MJ_CREATE ] = DongDispatchOpen;\r\npDriverObject->MajorFunction[ IRP_MJ_CLOSE ] = DongDispatchClose;\r\npDriverObject->MajorFunction[ IRP_MJ_WRITE ] = DongDispatchWrite;\r\npDriverObject->MajorFunction[ IRP_MJ_READ ] = DongDispatchRead;\r\npDriverObject->MajorFunction[ IRP_MJ_CLEANUP ] = DongDispatchCleanup;\r\n\r\n\/\/ Initialize a Device object for each parallel port\r\nNumParallelPorts = IoGetConfigurationInformation()->ParallelCount;\r\n\r\nfor( NtDeviceNumber=0; NtDeviceNumber<NumParallelPorts; NtDeviceNumber++)\r\n{\r\n   status = DongCreateDevice( pDriverObject, NtDeviceNumber);\r\n   if( !NT_SUCCESS(status))\r\nreturn status;\r\n}\r\n\r\n\/\/ Log that we've started\r\n\/\/ ...\r\n\r\nreturn status;\r\n}\r\n\r\nstatic NTSTATUS\r\nDongCreateDevice (\r\nIN PDRIVER_OBJECT pDriverObject,\r\nIN ULONG NtDeviceNumber\r\n)\r\n{\r\nNTSTATUS status;\r\n\r\nPDEVICE_OBJECT pDevObj;\r\nPDEVICE_EXTENSION pDevExt;\r\n\r\nUNICODE_STRING deviceName, portName, linkName, number;\r\nWCHAR deviceNameBuffer[DONG_MAX_NAME_LENGTH];\r\nWCHAR portNameBuffer[DONG_MAX_NAME_LENGTH];\r\nWCHAR linkNameBuffer[DONG_MAX_NAME_LENGTH];\r\nWCHAR numberBuffer[10];\r\n\r\nPFILE_OBJECT        pFileObject;\r\n\r\n\/\/ Initialise strings\r\nnumber.Buffer = numberBuffer;\r\nnumber.MaximumLength = 20;\r\ndeviceName.Buffer = deviceNameBuffer;\r\ndeviceName.MaximumLength = DONG_MAX_NAME_LENGTH*2;\r\nportName.Buffer = portNameBuffer;\r\nportName.MaximumLength = DONG_MAX_NAME_LENGTH*2;\r\nlinkName.Buffer = linkNameBuffer;\r\nlinkName.MaximumLength = DONG_MAX_NAME_LENGTH*2;\r\n\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n   \/\/ Form the base NT device name...\r\n\r\ndeviceName.Length = 0;\r\n   RtlAppendUnicodeToString( &#038;deviceName, DONG_NT_DEVICE_NAME);\r\nnumber.Length = 0;\r\nRtlIntegerToUnicodeString( NtDeviceNumber, 10, &#038;number);\r\nRtlAppendUnicodeStringToString( &#038;deviceName, &#038;number);\r\n\r\n\/\/ Create a Device object for this device...\r\nstatus = IoCreateDevice(\r\npDriverObject,\r\nsizeof( DEVICE_EXTENSION ),\r\n&#038;deviceName,\r\nFILE_DEVICE_PARALLEL_PORT,\r\n0,\r\nTRUE,\r\n&#038;pDevObj);\r\n\r\nif( !NT_SUCCESS(status))\r\n{\r\nDongReportUnexpectedFailure(DONG_ERRORLOG_INIT,DONG_INIT_IoCreateDevice);\r\nreturn status;\r\n}\r\n\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\/\/ Use buffered I\/O\r\n\r\npDevObj->Flags |= DO_BUFFERED_IO;\r\n\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\/\/ Initialize the Device Extension\r\n\r\npDevExt = pDevObj->DeviceExtension;\r\nRtlZeroMemory(pDevExt, sizeof(DEVICE_EXTENSION));\r\n\r\npDevExt->DeviceObject = pDevObj;\r\npDevExt->NtDeviceNumber = NtDeviceNumber;\r\n\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\/\/ Attach to parport device\r\nportName.Length = 0;\r\n   RtlAppendUnicodeToString( &portName, DONG_NT_PORT_DEVICE_NAME);\r\nnumber.Length = 0;\r\nRtlIntegerToUnicodeString( NtDeviceNumber, 10, &number);\r\nRtlAppendUnicodeStringToString( &portName, &number);\r\n\r\nstatus = IoGetDeviceObjectPointer(&portName, FILE_READ_ATTRIBUTES,\r\n&pFileObject,\r\n&pDevExt->PortDeviceObject);\r\nif (!NT_SUCCESS(status))\r\n{\r\nIoDeleteDevice(pDevObj);\r\nDongReportUnexpectedFailure(DONG_ERRORLOG_INIT,DONG_INIT_IoGetDeviceObjectPointer);\r\nreturn status;\r\n}\r\n\r\nObReferenceObjectByPointer( pDevExt->PortDeviceObject,FILE_READ_ATTRIBUTES,\r\nNULL,KernelMode);\r\nObDereferenceObject(pFileObject);\r\n\r\npDevExt->DeviceObject->StackSize = pDevExt->PortDeviceObject->StackSize + 1;\r\n\r\n\/\/ Get the port information from the port device object.\r\nstatus = DongGetPortInfoFromPortDevice(pDevExt);\r\nif (!NT_SUCCESS(status))\r\n{\r\nIoDeleteDevice(pDevObj);\r\nreturn status;\r\n}\r\n\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n   \/\/ Form the Win32 symbolic link name.\r\n\r\nlinkName.Length = 0;\r\nRtlAppendUnicodeToString( &linkName, DONG_WIN32_DEVICE_NAME);\r\nnumber.Length = 0;\r\nRtlIntegerToUnicodeString( NtDeviceNumber + 1, 10, &number);\r\nRtlAppendUnicodeStringToString( &linkName, &number);\r\n\r\n\/\/ Create a symbolic link so our device is visible to Win32...\r\nstatus = IoCreateSymbolicLink( &linkName, &deviceName);\r\nif( !NT_SUCCESS(status))\r\n{\r\nIoDeleteDevice( pDevObj );\r\nDongReportUnexpectedFailure(DONG_ERRORLOG_INIT,DONG_INIT_IoCreateSymbolicLink);\r\nreturn status;\r\n}\r\n\r\nreturn status;\r\n}\r\n\r\nstatic NTSTATUS\r\nDongGetPortInfoFromPortDevice(\r\nIN OUT  PDEVICE_EXTENSION   pDevExt\r\n)\r\n{\r\nKEVENT                      event;\r\nPIRP                        irp;\r\nPARALLEL_PORT_INFORMATION   portInfo;\r\nIO_STATUS_BLOCK             ioStatus;\r\nNTSTATUS                    status;\r\n\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\/\/ Get parallel port information\r\n\r\nKeInitializeEvent(&event, NotificationEvent, FALSE);\r\n\r\nirp = IoBuildDeviceIoControlRequest(\r\nIOCTL_INTERNAL_GET_PARALLEL_PORT_INFO,\r\npDevExt->PortDeviceObject,\r\nNULL, 0, &portInfo,\r\nsizeof(PARALLEL_PORT_INFORMATION),\r\nTRUE, &event, &ioStatus);\r\n\r\nif (!irp)\r\n{\r\nDongReportUnexpectedFailure(DONG_ERRORLOG_INIT,DONG_INIT_IoBuildDeviceIoControlRequest);\r\nreturn STATUS_INSUFFICIENT_RESOURCES;\r\n}\r\n\r\nstatus = IoCallDriver(pDevExt->PortDeviceObject, irp);\r\n\r\nif (!NT_SUCCESS(status))\r\n{\r\nDongReportUnexpectedFailure(DONG_ERRORLOG_INIT,DONG_INIT_IoCallDriver);\r\nreturn status;\r\n}\r\n\r\nstatus = KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, NULL);\r\n\r\nif (!NT_SUCCESS(status))\r\n{\r\nDongReportUnexpectedFailure(DONG_ERRORLOG_INIT,DONG_INIT_KeWaitForSingleObject);\r\nreturn status;\r\n}\r\n\r\npDevExt->OriginalController = portInfo.OriginalController;\r\npDevExt->Controller = portInfo.Controller;\r\npDevExt->SpanOfController = portInfo.SpanOfController;\r\npDevExt->FreePort = portInfo.FreePort;\r\npDevExt->TryAllocatePort = portInfo.TryAllocatePort;\r\npDevExt->PortContext = portInfo.Context;\r\n\r\n\/\/ Check register span\r\nif (pDevExt->SpanOfController < DONG_REGISTER_SPAN)\r\n{\r\nDongReportUnexpectedFailure(DONG_ERRORLOG_INIT,DONG_INIT_RegisterSpan);\r\nreturn STATUS_INSUFFICIENT_RESOURCES;\r\n}\r\n\r\nreturn STATUS_SUCCESS;\r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u4e2a\u52a0\u5bc6\u72d7\u7684\u9a71\u52a8\u6e90\u7801<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[29,32,31,30],"_links":{"self":[{"href":"https:\/\/www.abin.cn\/index.php?rest_route=\/wp\/v2\/posts\/149"}],"collection":[{"href":"https:\/\/www.abin.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.abin.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.abin.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.abin.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=149"}],"version-history":[{"count":0,"href":"https:\/\/www.abin.cn\/index.php?rest_route=\/wp\/v2\/posts\/149\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.abin.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.abin.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.abin.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}