Refer to a file by its object name
来源:互联网 发布:网络培训好吗 编辑:程序博客网 时间:2024/05/18 15:55
Refer to a file by its object name
Kernel-mode device drivers refer to a file by its object name. This name is \DosDevices together with the full path of the file. For example, the object name of the C:\Windows\Example.txt file is \DosDevices\C:\Windows\Example.txt. Then the object name is encapsulated into an OBJECT_ATTRIBUTES structure by calling the InitializeObjectAttributes function.Note If the device driver is loaded early, the \DosDevices namespace may not yet exist. Therefore, the\DosDevices namespace is inaccessible to the device driver because no drive letter is exposed. The only part of the file system that is guaranteed to be available is the\SystemRoot namespace. The \SystemRoot namespace is mapped to the folder where the operation system is installed. For example, this folder may be C:\Windows or D:\Winnt.
The following code example illustrates how to refer to a file by its object name.
UNICODE_STRING uniName; OBJECT_ATTRIBUTES objAttr; RtlInitUnicodeString(&uniName, L"\\DosDevices\\C:\\WINDOWS\\example.txt"); // or L"\\SystemRoot\\example.txt" InitializeObjectAttributes(&objAttr, &uniName, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL);
Obtain a file handle
To obtain a file handle, you can pass an OBJECT_ATTRIBUTES structure to theZwCreateFile function. The DesiredAccess parameter can be set to eitherGENERIC_READ, GENERIC_WRITE, or GENERIC_ALL, depending on what you want to do. If you set theCreateOptions parameter to FILE_SYNCHRONOUS_IO_NONALERT orFILE_SYNCHRONOUS_IO_ALERT, the file system keeps track of the current file-position offset. Therefore, you can sequentially read or write to the file later. Additionally, you can access the file at a random location.The following code example illustrates how to obtain a file handle.
HANDLE handle; NTSTATUS ntstatus; IO_STATUS_BLOCK ioStatusBlock; // Do not try to perform any file operations at higher IRQL levels. // Instead, you may use a work item or a system worker thread to perform file operations. if(KeGetCurrentIrql() != PASSIVE_LEVEL) return STATUS_INVALID_DEVICE_STATE; ntstatus = ZwCreateFile(&handle, GENERIC_WRITE, &objAttr, &ioStatusBlock, NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
Read from or write to a file
You can now call the ZwReadFile function or the ZwWriteFile function. When you have finished modifying the file, close the handle by using theZwClose function.The following code example illustrates how to write to a file.
#define BUFFER_SIZE 30 CHAR buffer[BUFFER_SIZE]; size_t cb; if(NT_SUCCESS(ntstatus)) { ntstatus = RtlStringCbPrintfA(buffer, sizeof(buffer), "This is %d test\r\n", 0x0); if(NT_SUCCESS(ntstatus)) { ntstatus = RtlStringCbLengthA(buffer, sizeof(buffer), &cb); if(NT_SUCCESS(ntstatus)) { ntstatus = ZwWriteFile(handle, NULL, NULL, NULL, &ioStatusBlock, buffer, cb, NULL, NULL); } } ZwClose(handle); }
LARGE_INTEGER byteOffset; ntstatus = ZwCreateFile(&handle, GENERIC_READ, &objAttr, &ioStatusBlock, NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0); if(NT_SUCCESS(ntstatus)) { byteOffset.LowPart = byteOffset.HighPart = 0; ntstatus = ZwReadFile(handle, NULL, NULL, NULL, &ioStatusBlock, buffer, BUFFER_SIZE, &byteOffset, NULL); if(NT_SUCCESS(ntstatus)) { buffer[BUFFER_SIZE-1] = '\0'; DbgPrint("%s\n", buffer); } ZwClose(handle); }
- Refer to a file by its object name
- Attempt to refer to a unregistered pool by its alias '**'
- Attempt to refer to a unregistered pool by its alias 'xxx'
- paip.proxool连接池 :Attempt to refer to a unregistered pool by its alias 'xx'
- Attempt to refer to a unregistered pool by its alias 'xxx'
- hibernate4+spring3.1+proxool0.9.1 出现 Attempt to refer to a unregistered pool by its alias
- java--proxool.xml异常Attempt to refer to a unregistered pool by its alias
- Attempt to refer to a unregistered pool by its alias 'dbpool'
- org.logicalcobwebs.proxool.ProxoolException: Attempt to refer to a unregistered pool by its alias
- spring 结合 proxool连接池 启动tomcat报错:Attempt to refer to a unregistered pool by its alias
- proxool连接池问题解决方案:Attempt to refer to a unregistered pool by its alias 'test'(test是连接池的别名)
- Spring中使用proxool连接池的配置方式与出现Attempt to refer to a unregistered pool by its alias 'ums'异常的解决办法
- java程序在开发工具上运行正常,打包之后本地运行报错Attempt to refer to a unregistered pool by its alias '**'
- Can a DOS batch file determine its own file name?
- Index and length must refer to a location within the string. Parameter name: length (mscorlib)
- Project property "XFire services.xml path" does not refer to a valid file解决办法
- Project property "XFire services.xml path" does not refer to a valid file问题解决
- linux下found a swap file by the name
- Android PendingIntent详解
- 日志
- WinDbg定位内存泄露
- 关于android中PendingIntent.getBroadcase的注册广播
- 提取特定字符的值
- Refer to a file by its object name
- IBM服务器数据恢复案例- VMWare ESX 数据恢复成功
- STK卫星对地成像观测覆盖分析
- AlarmManager类的应用(实现闹钟功能)
- 支持Android与iOS,Qt 5.1 Alpha全新亮相
- 关于C++处理1+2+......+n的新思路
- 操作系统内存管理——分区、页式、段式管理
- MyEclipse快捷键
- Simple Packet Sniffer using Java