对于设备驱动程序通知应用程序的措施.doc_第1页
对于设备驱动程序通知应用程序的措施.doc_第2页
对于设备驱动程序通知应用程序的措施.doc_第3页
对于设备驱动程序通知应用程序的措施.doc_第4页
对于设备驱动程序通知应用程序的措施.doc_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

对于设备驱动程序通知应用程序的措施 论文题目:对于设备驱动程序通知应用程序的几种方法 外文翻译ThedevicedrivernotifiestheapplicationofseveralmethodsInthecurrentpopularWindowsoperatingsystem,devicedriversisthelowestlevelofmanipulationofhardwaresoftwareinterface.Tosharethedevicedriverdesignprocessexperience,giventhedevicedrivernotifiestheapplicationofthefivekindsofways,eachmethodspecificallydescribedtheprincipleandimplementationprocess,hopingtogivethedesignerofthedevicedrivertoprovidesomehelp.Operatingsysteminordertoensurepeaceandstability,andapplicationportability,Windowsoperatingsystemdoesnotallowapplicationsdirectaesstothesystemshardwareresources,butmustresorttotheappropriatedevicedriver.Devicedriverscandirectlymanipulatethehardware,ifapplicationsanddevicedriverstoachieveatwo-waymunicationbetween,alsoreachedtheunderlyinghardwaredeviceapplicationcontrolpurposes.Communicationbetweenthem,includingtwoaspectsAbstract:Ontheonehandistheapplicationsenttothedevicedriverdata;handisthedevicedriversendsamessagetotheapplication.TheformertoachievemoreeasilythroughCreateFile()functiontoobtainahandletothedevicedriver,youcanusetheWin32functionssuchasDeviceIoControl(),ReadFile()orWriteFile(),etc.toachieveapplicationanddevicedrivermunication.Implementationofthelatterthantheformerplexandthearticledescribesthesituationinthisrespectless.Thisdoesnotmeanthatitisnotimportant,onthecontrary,itisinsomeapplicationsplayan importantfunction.Devicedriverspletethedatacollectionwork,theneedtoimmediatelynotifytheapplicationsothattheapplicationcanbepromptlyremovedandthedataisprocessed.Incaseslikethese,andsoforth.Inviewofthedevicedrivernotifiestheapplicationoftheimportance,Ibinedsomeexperience,itwassummarizedsummarizedfivemethodsAbstract:asynchronousprocedurecall(APC),eventmode(VxD),messaging,asynchronousI/Omodesandeventmode(WDM).Belowweretheprinciplesofthisinseveralways,andgivenofsomeofthesourcecode.AnAsynchronousProcedureCall(APC)Win32applicationsusingCreateFile()functiontodynamicallyloadthedevicedriver,andthendefineacallbackfunctionbackFunc(),andtheaddressofthecallbackfunction%26amp;backFunc()asaparameter,throughDeviceIoControl()senttothedevicedriver.Devicedriverstogettheaddressofacallbackfunction,itwillsaveitinaglobalvariable(egcallback),thesimultaneouscallsGet_Cur_Thread_Handle()functiontogetitsapplicationthreadhandle,andthehandleisstoredinaglobalvariable(egappthread)in.Whenconditionsareripe,thedevicedrivercalls_VWIN32_QueueUserApc()functiontosendmessagestotheWin32application.ThisfunctiontakesthreeargumentsAbstract:Thefirstparameteristheaddressofthecallbackfunction(alreadyregistered);secondparameterispassedtothecallbackmessage;thirdparametertothecallersthreadhandle(alreadyregistered).Win32applicationreceivesamessage,theautomaticcallbackfunction(actuallycalledbythedevicedriver).Callbackfunctionoftheinputparametersarepopulatedbythedevicedriver,thecallbackfunctionhereismainlytohandlethemessage.2EventMode(VxD) First,Win32applicationstocreateaneventhandler,calledRing3 handle.BecausethevirtualdevicedriverusingeventsRing0handle,soyouneedtocreateRing0handle.UsingLoadLibrary()functiontoloadthedynamiclinklibraryundisclosedKernel32.dll,getdynamiclinklibraryhandle.Then,callGetProcAddress(),findthefunctionOpenVxDHandle()inthedynamiclinklibrarylocation.Then,OpenVxDHandle()functionwillRing3eventhandlersintoRing0eventhandler.Win32applicationsusingCreateFile()functiontoloadthedevicedriver.Iftheloadissuessful,calltheDeviceIoControl()functionwillbepassedtotheeventhandlerRing0VxD;whilewaitingforthesignaltocreateaworkerthreadbeessignaledstateitselfcanshopandotherthings.Whenconditionsareripe,VxDsetRing0eventtosignaledstate(called_VWIN32_SetWin32Event()function),whichcorrespondstoRing3immediatelytriggereventtosignaledstate.OnceRing3eventhandlerforthesignaledstate,Win32applicationsworkerthreadonthisnewsbehandledaordingly.3MessageModeWin32applicationcallsCreateFile()functiontodynamicallyloadthevirtualdevicedriver.Loadedsuessfully,bycallingDeviceIoControl()functionhandlestheformsenttoVxD,VxDusethishandletoformamessage.Whentheconditionsaremet,VxDcallsSHELL_PostMessage()functiontosendmessagestoWin32applications.Tomakeuseofthisfunctionsuessfully,youmustuse#definetocustomizeamessage,andevensoitisdefinedintheapplication;alsousedinthemessageloopON_MESSAGE()todefinethemessageprocessingfunctioncorrespondingtothemessagesothatthemessageisgenerated,cancallthemessagehandler.SHELL_PostMessage()functionsfirstargumentforWin32windowhandle,thesecondparameteristhemessageIDnumber,andthethird,fourparametersaresenttothemessageprocessing functionparameters,fifth,sixparametersforthecallbackfunctionandpasstoits parameters.Win32applicationreceivesamessage,themessageisprocessed.4AsynchronousI/OModeWin32applicationfirstcallstheCreateFile()functiontoloadthe devicedriver.Beforecallingthisfunction,thepenultimatetwoparameterissettoFILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,sayingthathecanfileoverlappedI/Ooperations.Whenthedevicedriverfileissuessfullycreated,createaninitialstateisnosignal,youneedtomanuallyresettheevent,andwillpassthiseventtypeisOVERLAPPEDdatastructure(egOverlapped).Then,OverlappedasaparameterpassedtoDeviceIoControl()function.DevicedriverputtheI/Orequestpacket(IRP)issettopendingstatus,andsetacancelroutine.IfthecurrentIRPqueueisempty,thentheIRPsenttoStartIo()routine;Otherwise,putitIRPqueue.Devicedriversafterpletingsuchwork,endingtheDeviceIoControl()processing,sodonotwaitforWin32applicationsmayIRPprocessed,fromDeviceIoControl()calltoreturn.Bydeterminingthereturnvalue,gettheIRPhandling.IfthecurrentIRPispending,thenthemainprogramdosomeotherwork,thencallWaitForSingleObject()orWaitForMultipleObject()functionwaitsfortheeventbeeOverlappedsignalstate.DevicedriversattheappropriatetimetohandlethequeuedIRP,processingisplete,callIoCompleteRequest()function.ThisfunctionOverlappedtheeventissettothesignaledstate.Win32applicationstorespondtothisincidentimmediatelyexitthewaitstate,andnosignalwillberesettothestateoftheevent,andthencallGetOverlappedResultFunctiontogettheresultoftheIRPprocess.5EventMode(WDM)Win32applicationfirstcreatesanevent,andthenpassedtotheeventhandlerdevicedrivers,thencreateasecondarythread,waitingforaneventsignaledthathewillgoonotherthings.Devicedriversaesstheeventhandle,canbeusedto convertittoaneventpointerregisterandputitupforlateruse.Whentheconditionsaremet,thedevicedriverwillsettheeventtosignaledstate,sothattheapplicationoftheworkerthreadimmediatelyknowthenews,thenmaketheappropriatetreatment.Whenthedevicedrivernolongerusesthisevent,theeventshouldbeliftedpointer.6ConclusionInthecurrentpopularWindowsoperatingsystem,devicedriversisthelowestlevelofmanipulationofhardwaresoftwareinterface.Itoffersupauserinterfaceandhardware-independent,downdirectlyI/O,hardwareinterrupts,DMAandmemoryaessandotheroperations.Itapplicationsandhardwaredetailsshields,sodonotrelyonthehardwareandsoftwareavailableinportablebetweendifferentplatforms.Thisarticledescribesthefivekindsofdevicedrivernotifiestheapplication,inwhichthefirstthreemethodsaremainlyusedinVxD,thelattertwomethodsismainlyusedforWDM.This5waystohavebeentheactualtest.Testresultsshowthattheyareabletoreachthedevicedrivernotifiestheapplicationpurposes.ReferencesAbstract: 1EuropeanYouthLi,XUJian-bo,LIFang-min,etc.virtualdevicedriverVxDinquiryanddevelopment.ComputerEngineering,xx 2(America)ChrisCant.WindowsWDMDeviceDriverDevelopmentGuide.SunYi,MaLibo,thecountryXuefei,M.,BeijingAbstract:MechanicalIndustryPress,20003LiHepingDSP-basedimagereconstructionsystemtoexploreICT,BeijingAbstract:BeijingUniversityofAeronauticsandAstronauticsMechanicalEngineeringandAutomation,xx 对于设备驱动程序通知应用程序的几种方法 在目前流行的Windows操作系统中,设备驱动程序是操纵硬件 摘要:在目前流行的Windows操作系统中,设备驱动程序是操纵硬件的最底层软件接口。为了共享在设备驱动程序设计过程中的经验,给出设备驱动程序通知应用程序的5种方法,详细说明每种方法的原理和实现过程,希望能够给设备驱动程序的设计者提供一些帮助。 摘要:在目前流行的Windows操作系统中,设备驱动程序是操纵硬件的最底层软件接口。为了共享在设备驱动程序设计过程中的经验,给出设备驱动程序通知应用程序的5种方法,详细说明每种方法的原理和实现过程,希望能够给设备驱动程序的设计者提供一些帮助。 关键词:设备驱动程序异步I/OVirtualDeviceDriver(VxD)WindowsDriverModel(WDM) 为了保证操作系统的安全性和稳定性以及应用程序的可移植性,Windows操作系统不允许应用程序直接访问系统的硬件资源,而是必须借助于相应的设备驱动程序。设备驱动程序可以直接操作硬件,如果应用程序和设备驱动程序之间实现了双向通信,也就达到了应用程序控制底层硬件设备的目的。它们之间的通信包括两个方面:一方面是应用程序传送给设备驱动程序的数据;另一方面是设备驱动程序发送给应用程序的消息。前者的实现较容易,通过CreateFile()函数获取设备驱动程序的句柄后,就可以使用Win32函数,如DeviceIoControl()、 ReadFile()或WriteFile()等实现应用程序与设备驱动程序之间的通信。后者的实现远比前者复杂,同时介绍这方面情况的文章较少。这不等于说它不重要,相反,它在有些应用场合发挥着重要的作用。设备驱动程序完成数据的采集工作后,需要马上通知应用程序,以便应用程序能够及时将数据取走并进行处理。诸如此类情况,不一而足。 鉴于设备驱动程序通知应用程序的重要性,本人结合一些经验,对它进行了总结,归纳出5种方法:异步过程调用(APC)、事件方式(VxD)、消息方式、异步I/O方式和事件方式(WDM)。下面分别说明这几种方式的原理,并给出实现的部分源代码。 1异步过程调用(APC) Win32应用程序使用CreateFile()函数动态加载设备驱动程序,然后定义一个回调函数backFunc(),并且将回调函数的地址&backFunc()作为参数,通过 DeviceIoControl()传送给设备驱动程序。设备驱动程序获得回调函数的地址后,将它保存在一个全局变量(如callback)中,同时调用Get_Cur_Thread_Handle()函数获取它的应用程序线程的句柄,并且将该句柄保存在一个全局变量(如appthread)中。当条件成熟时,设备驱动程序调用_VWIN32_QueueUserApc()函数,向Win32应用程序发送消息。这个函数带有三个参数:第一个参数为回调函数的地址(已经注册);第二个参数为传递给回调函数的消息;第三个参数为调用者的线程句柄(已经注册)。Win32应用程序收到消息后,自动调用回调函数(实际是由设备驱动程序调用)。回调函数的输入参数是由设备驱动程序填入的,回调函数在这里主要是对消息进行处理。 2事件方式(VxD) 首先,Win32应用程序创建一个事件的句柄,称其为Ring3句柄。由于虚拟设备驱动程序使用事件的Ring0句柄,因此,需要创建Ring0句柄。用LoadLibrary()函数加载未公开的动态链接库Kernel32.dll,获得动态链接库的句柄。然后,调用GetProcAddress(),找到函数OpenVxDHandle()在动态链接库中的位置。接 着,用OpenVxDHandle()函数将Ring3事件句柄转化为Ring0事件句柄。Win32应用程序用CreateFile()函数加载设备驱动程序。如果加载成功,则调用 DeviceIoControl()函数将Ring0事件句柄传给VxD;同时,创建一个辅助线程等待信号变成有信号状态,本身则可去干其它的事情。当条件成熟时,VxD置Ring0事件为有信号状态(调用_VWIN32_SetWin32Event()函数),这马上触发对应的Ring3事件为有信号状态。一旦Ring3事件句柄为有信号状态,Win32应用程序的辅助线程就对这个消息进行相应的处理。 3消息方式 Win32应用程序调用CreateFile()函数动态加载虚拟设备驱动程序。加载成功后,通过调用DeviceIoControl()函数将窗体句柄传送给VxD,VxD利用这个句柄向窗体发消息。当条件满足时,VxD调用SHELL_PostMessage()函数向Win32应用程序发送消息。要让该函数使用成功,必须用#define定义一个消息,并且也要照样在应用程序中定义它;还要在消息循环中使用ON_MESSAGE()来定义消息对应的消息处理函数,以便消息产生时,能够调用消息处理函数。SHELL_PostMessage()函数的第一个参数为Win32窗体句柄,第二个参数为消息ID号,第三、四个参数为发送给消息处理函数的参数,第五、六个参数为回调函数和传给它的参数。Win32应用程序收到消息后,对消息进行处理。4异步I/O方式 Win32应用程序首先调用CreateFile()函数加载设备驱动程序。在调用该函数时,将倒数第2个参数设置为 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,表示以后可以对文件进行重叠I/O操作。当设备驱动程序文件创建成功后,创建一个初始态为无信号、需要手动复位的事件,并且将这个事件传给类型为OVERLAPPED的数据结构(如Overlapped)。然后,将Overlapped作为一个参数,传给 DeviceIoControl

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论