• 标准MDL方法修改Page、NonPage内存的属性


    1. typedef struct _REPROTECT_CONTEXT  
    2. {  
    3.     PMDL   Mdl;  
    4.     PUCHAR LockedVa;  
    5. } REPROTECT_CONTEXT, * PREPROTECT_CONTEXT;  
    6.    
    7. NTSTATUS  
    8. MmLockVaForWrite(  
    9.     __in PVOID Va,  
    10.     __in ULONG Length,  
    11.     __out PREPROTECT_CONTEXT ReprotectContext  
    12.     )  
    13. {  
    14.     NTSTATUS Status;  
    15.   
    16.     Status = STATUS_SUCCESS;  
    17.   
    18.     ReprotectContext->Mdl      = 0;  
    19.     ReprotectContext->LockedVa = 0;  
    20.   
    21.     ReprotectContext->Mdl = IoAllocateMdl(  
    22.         Va,  
    23.         Length,  
    24.         FALSE,  
    25.         FALSE,  
    26.         0  
    27.         );  
    28.   
    29.     if (!ReprotectContext->Mdl)  
    30.     {  
    31.         return STATUS_INSUFFICIENT_RESOURCES;  
    32.     }  
    33.   
    34.     //  
    35.     // Retrieve a locked VA mapping.  
    36.     //  
    37.   
    38.     __try  
    39.     {  
    40.         MmProbeAndLockPages(  
    41.             ReprotectContext->Mdl,  
    42.             KernelMode,  
    43.             IoModifyAccess  
    44.             );  
    45.     }  
    46.     __except (EXCEPTION_EXECUTE_HANDLER)  
    47.     {  
    48.        return GetExceptionCode();  
    49.     }  
    50.   
    51.     ReprotectContext->LockedVa = (PUCHAR)MmMapLockedPagesSpecifyCache(  
    52.         ReprotectContext->Mdl,  
    53.         KernelMode,  
    54.         MmCached,  
    55.         0,  
    56.         FALSE,  
    57.         NormalPagePriority  
    58.         );  
    59.   
    60.     if (!ReprotectContext->LockedVa)  
    61.     {  
    62.          
    63.   
    64.         IoFreeMdl(  
    65.             ReprotectContext->Mdl  
    66.             );  
    67.   
    68.         ReprotectContext->Mdl = 0;  
    69.   
    70.         return STATUS_ACCESS_VIOLATION;  
    71.     }  
    72.   
    73.     //  
    74.     // Reprotect.  
    75.     //  
    76.   
    77.     Status = MmProtectMdlSystemAddress(  
    78.         ReprotectContext->Mdl,  
    79.         PAGE_EXECUTE_READWRITE  
    80.         );  
    81.   
    82.     if (!NT_SUCCESS(Status))  
    83.     {  
    84.   
    85.   
    86.         MmUnmapLockedPages(  
    87.             ReprotectContext->LockedVa,  
    88.             ReprotectContext->Mdl  
    89.             );  
    90.         MmUnlockPages(  
    91.             ReprotectContext->Mdl  
    92.             );  
    93.         IoFreeMdl(  
    94.             ReprotectContext->Mdl  
    95.             );  
    96.   
    97.         ReprotectContext->LockedVa = 0;  
    98.         ReprotectContext->Mdl      = 0;  
    99.     }  
    100.   
    101.     return Status;  
    102. }  
    103.   
    104. NTSTATUS  
    105. MmUnlockVaForWrite(  
    106.     __in PREPROTECT_CONTEXT ReprotectContext  
    107.     )  
    108. {  
    109.     if (ReprotectContext->LockedVa)  
    110.     {  
    111.         MmUnmapLockedPages(  
    112.             ReprotectContext->LockedVa,  
    113.             ReprotectContext->Mdl  
    114.             );  
    115.         MmUnlockPages(  
    116.             ReprotectContext->Mdl  
    117.             );  
    118.         IoFreeMdl(  
    119.             ReprotectContext->Mdl  
    120.             );  
    121.   
    122.         ReprotectContext->LockedVa = 0;  
    123.         ReprotectContext->Mdl      = 0;  
    124.     }  
    125.   
    126.     return STATUS_SUCCESS;  
    127. }  
  • 相关阅读:
    Data Visualisation Cheet Sheet
    数据预处理
    算法题目
    集成方法
    Mysql数据库重要知识点(知了堂学习心得)
    Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 创建复杂数据模型
    Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 排序、筛选、分页以及分组
    Working with Data » 使用Visual Studio开发ASP.NET Core MVC and Entity Framework Core初学者教程
    ASP.NET Core文档中Work with Data章节的翻译目录
    webpages框架中使用Html.TextArea()在前台显示多行信息时,如何进行大小、样式的设置
  • 原文地址:https://www.cnblogs.com/kuangke/p/5524443.html
Copyright © 2020-2023  润新知