首页 > 解决方案 > 带有 ConDrv 设备的 NtOpenFile() 显示 0xC0000005 错误

问题描述

我在模仿conhost.execondrv.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);

如何修改该代码以正常工作?我做错什么了吗?

标签: winapiconsole

解决方案


感谢@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;并且它有效。


推荐阅读