NTSTATUS status;
UNICODE_STRING strFileSrc = RTL_CONSTANT_STRING(L"\\??\\C:\\网络调试工具.exe");//几十kb
UNICODE_STRING strFileDest = RTL_CONSTANT_STRING(L"\\??\\C:\\网络调试工具1.exe");
UNICODE_STRING strSectionName = RTL_CONSTANT_STRING(L"\\MySection");//可以是符号连接的方法,也可以用\\MySection,如果不加\\会报错
HANDLE hFileSrc = NULL;
HANDLE hFileDest = NULL;
OBJECT_ATTRIBUTES oaSrc;
OBJECT_ATTRIBUTES oaDest;
OBJECT_ATTRIBUTES oaSection;
IO_STATUS_BLOCK iosb;
FILE_NETWORK_OPEN_INFORMATION fnoi; HANDLE hSection = NULL;
PVOID pAddress = NULL;
SIZE_T viewsize = 0;
LARGE_INTEGER li_Section;
LARGE_INTEGER li_Offset;
LARGE_INTEGER li_WriteOffset; InitializeObjectAttributes(&oaSrc, &strFileSrc, OBJ_KERNEL_HANDLE, NULL, NULL);
InitializeObjectAttributes(&oaDest, &strFileDest, OBJ_KERNEL_HANDLE, NULL, NULL);
InitializeObjectAttributes(&oaSection, &strSectionName, OBJ_KERNEL_HANDLE, NULL, NULL); status = ZwCreateFile(&hFileSrc, GENERIC_READ, &oaSrc, &iosb, NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN, FILE_NON_DIRECTORY_FILE, NULL, 0);
if (!NT_SUCCESS(status)){
ZwClose(hFileSrc);
KdPrint(("hFileSrc: ZwCreateFile failed with error %I32X", status));
return status;
} status = ZwQueryFullAttributesFile(&oaSrc, &fnoi);
if (!NT_SUCCESS(status)){
ZwClose(hFileSrc);
ZwClose(hSection);
KdPrint(("ZwQueryFullAttributesFile failed with error %I32X", status));
return status;
}
KdPrint(("文件大小:%I64d", fnoi.AllocationSize.QuadPart));
li_Section.QuadPart = fnoi.AllocationSize.QuadPart;
status = ZwCreateSection(&hSection, SECTION_MAP_READ | SECTION_MAP_WRITE, &oaSection, &li_Section, PAGE_READWRITE, SEC_RESERVE, hFileSrc);//只分配在虚拟内存中,不提交到物理内存.参考VirtualAlloc
if (!NT_SUCCESS(status)){
ZwClose(hFileSrc);
ZwClose(hSection);
KdPrint(("ZwCreateSection failed with error %I32X", status));
return status;
} li_Offset.QuadPart = 0;
status = ZwMapViewOfSection(hSection,
ZwCurrentProcess(),
&pAddress,//BaseAddress 如果不为NULL,则分配在指定的位置,有可能失败
0,//为NULL,表示可以分配在虚拟内存的任意位置,请参考windows核心编程里面的VirtualAlloc
(ULONG)li_Section.LowPart,//文件大小
&li_Offset,//从文件的那里开始映射,从其实位置开始映射,则li_Offset.QuadPart = 0;
&viewsize,//对于小文件,SectionOffset 和 ViewSize 都为0
ViewUnmap,
MEM_RESERVE | MEM_LARGE_PAGES ,
PAGE_READWRITE);
if (!NT_SUCCESS(status)){
ZwClose(hFileSrc);
ZwClose(hSection);
ZwClose(hFileDest);
ZwUnmapViewOfSection(hSection, pAddress);
KdPrint(("ZwMapViewOfSection failed with error %I32X", status));
return status;
} status = ZwCreateFile(&hFileDest,
GENERIC_READ | GENERIC_WRITE,
&oaDest,
&iosb,
&li_Section,//要创建的文件大小
FILE_ATTRIBUTE_NORMAL,
0,
FILE_CREATE,
FILE_NON_DIRECTORY_FILE,
NULL,
0);
if (!NT_SUCCESS(status)){
ZwClose(hFileSrc);
ZwClose(hSection);
ZwClose(hFileDest);
KdPrint(("hFileDest: ZwCreateFile failed with error %I32X", status));
return status;
} li_WriteOffset.QuadPart = 0;
ZwWriteFile(hFileDest, NULL, NULL, NULL, &iosb, pAddress, viewsize, &li_WriteOffset, NULL);//参数ByteOffset为0表示从其实地址开始写数据,适用于小文件
ZwUnmapViewOfSection(ZwCurrentProcess(), pAddress); ZwClose(hSection);
ZwClose(hFileDest);
ZwClose(hFileSrc);
return STATUS_SUCCESS;

版权声明:本文为博主原创文章,未经博主允许不得转载。

最新文章

  1. windows 安装 mongodb
  2. MVC实用架构设计(三)——EF-Code First(3):使用T4模板生成相似代码
  3. TCP/IP详解学习笔记(6)-- IP选路
  4. [转][IIS]发布网站,提示用户 'IIS APPPOOL\***' 登录失败。
  5. C++:this指针
  6. HTML5 <Canvas>文字粒子化
  7. SharePoint DataFormWebPart 通过Caml和xslt聚合内容
  8. 更好列表页中一个航班.先unset删除数组中一个键值对,再追加,最后按键排序
  9. linux命令学习-4-lsof
  10. 对象克隆(clone)实例详解
  11. NYOJ116 士兵杀敌(二)
  12. struts2标签库----数据标签详解
  13. EDK II之Secure Boot简述
  14. JDBC原理
  15. crm--分页
  16. TTS
  17. Go语言之讲解GOROOT、GOPATH、GOBIN
  18. .NET跨平台实践:再谈用C#开发Linux守护进程 — 完整篇
  19. android学习---Gallery画廊视图
  20. C语言之Bit-wise Operation和Logical Operation

热门文章

  1. 在linux中安装redis
  2. JDBC--DBUtils的使用
  3. python学习笔记(三)---高级特性
  4. 104、Java中String类之使用indexOf()等功能查找
  5. 四 Hibernate的一级缓存&事务管理及其配置
  6. 在CentOS 7环境下安装 Spark
  7. 第2节 网站点击流项目(下):6、访客visit分析
  8. C#中使用设置(Settings.settings) Properties.Settings.Default .(配置文件相当重要)
  9. 剑指offer 链表中环的入口位置
  10. leetcode1261 Find Elements in a Contaminated Binary Tree