gpt4 book ai didi

c - 无法在 win32 项目中包含 ntifs.h

转载 作者:太空狗 更新时间:2023-10-29 15:25:10 25 4
gpt4 key购买 nike

我尝试使用名为 NTCreateFile 的函数。当我编译时它给了我一个错误说“未找到 _NTCreateFile 标识符”。我包含了 header winternl.h。所以接下来我尝试使用 ZwCreatFile,根据 MSDN,我包含了 ntifs.h,但我无法包含该 header 。它说“无法打开/找到目录”。我正在使用 V@2008。问题是什么?我错过了什么吗?

编辑 1:

typedef NTSTATUS (*fp_CreatFile)(
OUT PHANDLE FileHandle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_ATTRIBUTES ObjectAttributes,
OUT PIO_STATUS_BLOCK IoStatusBlock,
IN PLARGE_INTEGER AllocationSize OPTIONAL,
IN ULONG FileAttributes,
IN ULONG ShareAccess,
IN ULONG CreateDisposition,
IN ULONG CreateOptions,
IN PVOID EaBuffer OPTIONAL,
IN ULONG EaLength
);
OBJECT_ATTRIBUTES myAttributes;

int _tmain(int argc, _TCHAR* argv[])
{
fp_CreatFile myFunction;
HMODULE module = LoadLibrary(L"ntdll.dll");
if(NULL != module)
{
myFunction = (fp_CreatFile)GetProcAddress(module,"NtCreateFile");
}

UNICODE_STRING string;
IO_STATUS_BLOCK fileStatus;
string.Length = 56;
string.Buffer = L"C:\\user\\kiddo\\Desktop\\7zFM.exe";
string.MaximumLength = 56;

HANDLE fileHandle;
myAttributes.ObjectName = &string;
myAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
long mystatus = myFunction(&fileHandle,FILE_GENERIC_READ,&myAttributes ,&fileStatus,NULL,FILE_ATTRIBUTE_NORMAL,FILE_SHARE_READ,
NULL,NULL,NULL,NULL);
return 0;
}

当它尝试调用它时,它会在消息框中给出以下错误。错误:运行时检查失败 #0 - ESP 的值未在函数调用中正确保存。这通常是用一个调用约定声明的函数调用一个以不同调用约定声明的函数指针的结果。

最佳答案

如果您阅读 MSDN documentation ,第一段说:

Note Before using this function, please read Calling Internal APIs.

其中说:(我突出了重要部分)

The Winternl.h header file exposes prototypes of internal Windows APIs. There is no associated import library, so developers must use run-time dynamic linking to call the functions described in this header file.

The functions and structures in Winternl.h are internal to the operating system and subject to change from one release of Windows to the next, and possibly even between service packs for each release. To maintain the compatibility of your application, you should use the equivalent public functions instead. Further information is available in the header file, Winternl.h, and the documentation for each function.

If you do use these functions, you can access them through run-time dynamic linking using LoadLibrary and GetProcAddress. This gives your code an opportunity to respond gracefully if the function has been changed or removed from the operating system. Signature changes, however, may not be detectable.

因此您必须先从 NtDll.dll 加载您要使用的函数,然后才能使用它们。

这是一个未经测试的示例代码示例:

typedef NTSTATUS (__stdcall *NtCreateFile)(
OUT PHANDLE FileHandle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_ATTRIBUTES ObjectAttributes,
OUT PIO_STATUS_BLOCK IoStatusBlock,
IN PLARGE_INTEGER AllocationSize OPTIONAL,
IN ULONG FileAttributes,
IN ULONG ShareAccess,
IN ULONG CreateDisposition,
IN ULONG CreateOptions,
IN PVOID EaBuffer OPTIONAL,
IN ULONG EaLength
);

NtCreateFile _NtCreateFile = (NtCreateFile)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtCreateFile");

// You can now use the function
_NtCreateFile(/* params */);

// Don't forget the release the resources

关于c - 无法在 win32 项目中包含 ntifs.h,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2964941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com