2010-06-03 47 views
4

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

EDIT1:

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的值未正确保存在函数调用中。这通常是调用一个调用约定的函数声明的结果,其中函数指针声明的调用约定不同。

+0

你应该逐字地发布编译器输出消息(复制和粘贴会更简单和更准确的说明*);知道这是否是链接器或编译器错误(该消息会告诉我们这一点)非常重要。很明显,找不到的文件是一个处理器错误,但是在你尝试修复原始问题之前*怎么办? – Clifford 2010-06-03 10:19:53

回答

4

如果你读了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 
+0

谢谢你的回复ereOn,我确实喜欢你的建议,但又有一个RunTime错误..请检查我的编辑进一步 – kiddo 2010-06-03 11:24:32

+0

@kiddo:你最后的错误很明显:你尝试调用另一个“调用约定“而不是它编译的那个。如果你在你的函数声明前尝试了其中的一个:'__cdecl','__stdcall'(**可能是这个**)或'__fastcall'?我编辑了我的答案以添加有趣的部分。 – ereOn 2010-06-03 12:06:00

+0

它的工作,但它没有收到任何文件句柄..请帮助我,如果你有关于函数的知识NtCreateFile – kiddo 2010-06-03 12:16:47

1

ZwCreateFile函数是Windows驱动程序工具包,不是Windows SDK的一部分。您需要安装驱动程序工具包。 NTCreateFile使用的一些宏和类型也需要WDK头。这在MSDN的文档中有明确说明。

1

由于错误消息明确指出,您的调用约定错误,因此您放弃了NTAPI。它应该是:

typedef NTSTATUS (__stdcall * fp_CreatFile)(
    // etc.. 
); 

正确地初始化myAttributes通常很重要。我没有看到你做任何可以保证调用未记录的本机API函数的任何东西。尽可能坚持CreateFile()。

+0

是的,它的工作..但它没有做任何事情..对此功能有任何想法..Ncreatic文件 – kiddo 2010-06-03 12:05:06

+0

原生的Windows API是无证的,我不应该知道它的任何事情。当然,它确实有*什么是函数返回值? – 2010-06-03 12:20:12

+0

它的垃圾值..一些大的负值 – kiddo 2010-06-03 12:25:47

1

几种可能性:

  • 你说的错误消息是 “找不到_NTCreateFile标识符”。 API的名称是NtCreateFile()(注意小写't')。有可能你只是使用了错误的名字。

  • ntifs.h和相关的链接库包含在Windows驱动程序工具包(WDK)中,可以从这里下载:http://www.microsoft.com/whdc/devtools/wdk/wdkpkg.mspx。您应该可以使用WDK直接执行您想要的操作,而不是使用动态链接。但是你通常必须购买一个全新的构建系统,或者找出如何将头文件和库集成到当前版本中。可以使用dynamic linking technique outlined by ereOn