Use the application layer's space directly on the drive layer

Source: Internet
Author: User

</pre><p> Requirements: The application layer requests a space and then passes the address to the drive layer, which drives the processed data directly into the space specified by the application layer, similar to the Copy_from_user under Linux. </p><p></p><p> Application Layer Code snippet: </p><p><span style= "White-space:pre" > Pass the requested address to the drive layer </span></p><pre code_snippet_id= "576642" snippet_file_name= "blog_20150110_2_1402064" Name= "code" class= "CPP" >        CHAR    buf[64] = {0};int Iwritelen = 0;char *pwaddr = Null;char *praddr = null;unsigned Long *ptmp = Null;pwaddr = (char*) calloc (1, +);p raddr = (char*) calloc (1, +); if (pwaddr ==null) | | (Praddr==null)) {Puts ("Calloc error"); CloseHandle (hdevice); return 0;}  memset (pwaddr, ' A ', ');p tmp = (unsigned long*) buf;ptmp[0] = (unsigned long) 0;//pwaddr;ptmp[1] = (unsigned long) praddr;if (WriteFile (Hdevice, buf, 8, (Lpdword) &iwritelen, NULL)) {puts ("write Success");} Else{puts ("Write Failed");}

Driver Layer Code snippet:


VOID uaddr2daddr (PVOID pusr, unsigned long iusrlen, BOOLEAN bRead) {int i = 0; Lock_operation opt = iowriteaccess; Puchar pvir = NULL; PMDL MDL, if (! Mmisaddressvalid (PUSR))//If there is no such judgment, the application is an illegal address, will result in the following virtual address, blue screen {kdprint (("Mmisaddressvalid univalid error\n"); return;} MDL = IOALLOCATEMDL (pusr, Iusrlen, FALSE, TRUE, NULL); if (mdl = = NULL) {Kdprint (("Ioallocatemdl error\n"); return;} if (bRead) {opt = ioreadaccess;} Mmprobeandlockpages (MDL, UserMode, opt);p vir = Mmmaplockedpagesspecifycache (Mdl,kernelmode,mmcached,null,false, normalpagepriority)///If Puser is not valid, calling this function causes a blue screen kdprint ("Mmmaplockedpagesspecifycache:% #x \ n", (unsigned long) pvir)) ;p vir = Mmgetsystemaddressformdlsafe (MDL, normalpagepriority); <span style= "font-family:arial, Helvetica, Sans-serif; " >//if Puser is not valid, calling this function causes a blue screen </span>kdprint ("Mmgetsystemaddressformdlsafe:% #x \ n", (unsigned long) pvir)); Pvir = mmgetmdlvirtualaddress (MDL); <span style= "font-family:arial, Helvetica, Sans-serif;" >//if Puser is illegal, calling this function causes a blue screen </span&Gt Kdprint ("mmgetmdlvirtualaddress:% #x \ n", (unsigned long) pvir)), if (pvir = = NULL) {Kdprint (("Ioallocatemdl error\n")); return;} Kdprint ("\ n----------------------DML Data------------------\ n \ nthe")), if (BRead) {for (i=0, i<4; i++) {for (i=0; i< 4; i++) {Pvir[i] = ' B ';}}} Else{for (i=0; i<4; i++) {Kdprint (("%c", Pvir[i]);}} Kdprint ("\ n-------------------End DML Data------------------\ n \ nthe")); Mmunlockpages (MDL); iofreemdl (MDL);}


In the DbgView print driver: The virtual address values obtained by the 3 functions are as follows:

Mmmaplockedpagesspecifycache:0xae44b3b0
Mmgetsystemaddressformdlsafe:0xae44b3b0
Mmgetmdlvirtualaddress:0x7013b0

All of the above three functions can be completed with requirements.

Code Download:



Use the application layer's space directly on the drive layer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.