Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ NtOpenFile返回状态\对象\名称\无效_C++_Winapi - Fatal编程技术网

C++ NtOpenFile返回状态\对象\名称\无效

C++ NtOpenFile返回状态\对象\名称\无效,c++,winapi,C++,Winapi,以下代码尝试使用NtOpenFile生成文件句柄: HANDLE spawnFileHandle(){ HANDLE ret; IO_STATUS_BLOCK IoStatusBlock; OBJECT_ATTRIBUTES ObjectAttributes; ObjectAttributes.SecurityDescriptor=0; ObjectAttributes.SecurityQualityOfService=0; ObjectAttributes.RootDirectory=0; O

以下代码尝试使用NtOpenFile生成文件句柄:

HANDLE spawnFileHandle(){
HANDLE ret;
IO_STATUS_BLOCK IoStatusBlock;
OBJECT_ATTRIBUTES ObjectAttributes;
ObjectAttributes.SecurityDescriptor=0;
ObjectAttributes.SecurityQualityOfService=0;
ObjectAttributes.RootDirectory=0;
ObjectAttributes.Attributes=0;
ObjectAttributes.Length=sizeof(OBJECT_ATTRIBUTES);

WCHAR stringBuffer[5048];
UNICODE_STRING  string;
string.Buffer = stringBuffer;
lstrcpyW(stringBuffer, L"\\??\\");
lstrcatW(stringBuffer, EXEPath);
string.Length = lstrlenW(stringBuffer)*2; // Edit after comment.
string.MaximumLength = 5048;
ObjectAttributes.ObjectName=&string;
NTSTATUS error=origZwOpenFile(&ret, FILE_READ_DATA, &ObjectAttributes, &IoStatusBlock, FILE_SHARE_READ, 0);
printf("huh %ls %x", stringBuffer, error);
return ret;
}
但它总是返回状态\对象\名称\无效,例如:

编辑:[HBIP]-隐藏,因为我是偏执狂--

EXE path : C:\Users\n00b\Desktop\[HBIP]\Debug\[HBIP].exe
huh \??\C:\Users\n00b\Desktop\[HBIP]\Debug\[HBIP].exe c0000033
Spawned Handle : cccccccc

原因可能是什么?

UNICODE\u字符串结构要求
长度
最大长度
都以字节为单位。请注意,这些值将始终为偶数


您将获得
状态\u对象\u名称\u无效
,因为您的
长度
是一个奇数,因此无效。

嗯,星号不是有效的文件名字符。另外,
UNICODE\u STRING::Length
以字节为单位,而不是以字符为单位。此外,通常应在对象属性::属性中设置
OBJ\u不区分大小写
。星号用于屏蔽路径;)但是第二个我只是假设它是字符串长度o0,但是将它更改为字节不会改变任何东西,仍然是相同的错误。我认为这不是问题所在,因为它会返回“未找到文件”而不是“名称无效”…更正,第二个是答案:)我乘以4而不是2谢谢,请作为答案发布。我猜
EXEPath
中有无效字符。如果它不是星号,那么它是
\n
或其他东西(忘记的反斜杠?)。用一个文本替换
EXEPath
。啊,一个奇数长度的字符串,嗯?:)还要注意,sizeof()无法获取字节长度,因为它包含空终止符,而长度成员不需要它。好:
lstrlenW(path)*sizeof(wchar\u t)
坏:
sizeof(path)
UNICODE\u字符串详细信息
RtlInitUnicodeString()
实际上是为您初始化此结构而设计的: