我正在模仿
conhost.exe
和 condrv.sys
驱动程序之间的连接。因此,我将 conhost.exe 中的代码复制到一个简单的 C 文件中并进行编译。但NtOpenFile()
总是显示0xc0000005
错误。这是代码片段。
RtlInitUnicodeString(&DestinationString, L"\\Device\\ConDrv\\Server");
ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
ObjectAttributes.RootDirectory = 0;
ObjectAttributes.Attributes = OBJ_CASE_INSENSITIVE;
ObjectAttributes.ObjectName = &DestinationString;
ObjectAttributes.SecurityDescriptor = 0;
status = NtOpenFile(&Handle, GENERIC_ALL, &ObjectAttributes, &IoStatusBlock, 0, 0);
如何修改该代码才能正常工作?我做错了什么吗?
感谢@RbMm 的建议。
OBJECT_ATTRIBUTES
结构体定义为:
typedef struct _OBJECT_ATTRIBUTES {
ULONG Length;
HANDLE RootDirectory;
PUNICODE_STRING ObjectName;
ULONG Attributes;
PVOID SecurityDescriptor;
PVOID SecurityQualityOfService;
} OBJECT_ATTRIBUTES;
typedef OBJECT_ATTRIBUTES *POBJECT_ATTRIBUTES;
显示错误是因为我忘记将
SecurityQualityOfService
设置为零。因此 NtOpenFile()
从内存中剩余的内容中获取 SecurityQualityOfService
值。它显示 0xC0000005
又名。内存访问冲突。我添加 ObjectAttributes.SecurityQualityOfService = 0;
并且它有效。
InitializeObjectAttributes()
来初始化此结构。