gpt4 book ai didi

c++ - LNK2019 未解析的外部符号 NtOpenFile

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:49:46 32 4
gpt4 key购买 nike

我的代码面临链接器错误。我正在尝试在 Win-7 X64 位 m/c 中使用 Visual Studio 命令提示符(2010)进行编译。
我看到的错误如下。

dust2.obj

dust2.obj : error LNK2019: unresolved external symbol _NtOpenFile@24 referenced in function _main

dust2.obj : error LNK2019: unresolved external symbol _RtlAnsiStringToUnicodeStr ing@12 referenced in function _main

dust2.obj : error LNK2019: unresolved external symbol _RtlInitAnsiString@8 refer enced in function _main

dust2.exe : fatal error LNK1120: 3 unresolved externals



我的代码的简化版本是这样的:
   #include <windows.h>
#include <iostream>
#include <Winternl.h>

using namespace std;

int main()
{
NTSTATUS Status;
OBJECT_ATTRIBUTES Obja;
HANDLE SourceFile;
PUNICODE_STRING PathName=0;
PANSI_STRING p_path=0;
const char* ccp_path = "D:\\txt.txt";
RtlInitAnsiString( p_path,ccp_path );
RtlAnsiStringToUnicodeString( PathName, p_path, true );
IO_STATUS_BLOCK IoStatusBlock;
wprintf(L"%s", PathName->Buffer);
InitializeObjectAttributes(
&Obja,
PathName,
OBJ_CASE_INSENSITIVE,
NULL,
NULL
);
Status = NtOpenFile(
&SourceFile,
FILE_LIST_DIRECTORY | FILE_READ_EA | FILE_READ_ATTRIBUTES,
&Obja,
&IoStatusBlock,
FILE_SHARE_READ | FILE_SHARE_WRITE,
FILE_DIRECTORY_FILE | FILE_OPEN_FOR_BACKUP_INTENT | FILE_OPEN_REPARSE_POINT
);
if(SourceFile == INVALID_HANDLE_VALUE){
printf("\nError: Could not open file\n");
return 0;
}
cout<<endl<<endl;
system("pause");
return 0;

}

在本论坛的另一篇文章中提到此类问题的解决方案包括 #pragma .

我通过像这样添加 #pragma 来尝试这个解决方案
#pragma comment(lib, "ntdll")

但在编译时,我看到另一个错误,显示“链接:致命错误 LNK1104:无法打开文件 'ntdll.lib'”。

我将非常感谢您帮助解决此问题。谢谢..

最佳答案

不能让这个问题像这样没有答案。因为尽管 Mladen 的评论在很大程度上适用于这个特定的原生 API,但整个主题值得深入讨论。

初步警告

在前面我应该注意到,在很多情况下它是 neither desirable nor necessary to use one of the native API functions on Windows .但是,在少数情况下,Win32 API 不提供查询信息甚至操作数据等的方法。其中一种情况是 NtQueryInformationFile / ZwQueryInformationFile 可用的几个信息类。 .

一个很好的例子是枚举文件和目录上的备用数据流,这可以使用 Win32 API 来完成,特别是使用备份 API,但在这种情况下需要特殊权限。如果您使用 native API,则情况并非如此。在 Windows 2000 之前,硬链接(hard link)也是如此,它引入了 CreateHardLink 到 Win32 API。尽管在这种特殊情况下,如果您知道自己的方式,您可以使用 MoveFileEx MOVEFILE_CREATE_HARDLINK自从它被引入以来(尽管在撰写本文时,微软仍将其标记为保留以备将来使用……嗯)。

关于 native API 的规范书籍是以下两本:

  • Windows NT/2000 native API 引用,Gary Nebbett
  • 未记录的 Windows 2000 secret :程序员的食谱,Sven B. Schreiber ( free version from the author's website here )

  • ... 还有更多,包括讨论 NT 4 并且在 Nebbett 的书之前的一个。但是 Nebbett 的书曾经开始围绕原生 API 进行炒作,就像 Hoglund 的书开始围绕 Windows Rootkit 进行炒作一样。不是关于 native API 主题的引用,但仍然很好:
  • Windows Internals,Mark Russinovich 等。阿尔。

  • 查看此网站以获取“记录”的大量 native API 函数:
  • http://undocumented.ntinternals.net/

  • 所以请记住:使用这些函数的固有风险是它们会在 future 的 Windows 版本中消失或它们的语义发生变化,恕不另行通知。因此,如果您使用它们,请在使用它们时小心。

    走向荣耀...

    如何调用原生 API 函数

    实际上有两种方法可以调用这些函数。几年前,微软在一场反垄断诉讼中被迫披露了一些原生 API 功能。这些被插入 winternl.h SDK的。微软是这样表达的:

    The NtOpenFile documentation is provided for the sake of full API coverage. NtOpenFile is equivalent to the ZwOpenFile function documented in the DDK. For more information on the ZwOpenFile and related functions, go to http://msdn.microsoft.com/library. In the left-hand pane, click Windows Development, then click Driver Development Kit.



    然而,没有伴随 ntdll.lib SDK 中的文件。 Microsoft 建议您动态链接这些功能(下面的第二个选项)。

    您有多种选择:
  • 最常见的是照你做的做。但是ntdll.lib导入库只是 WDK 的一部分,而不是 DDK。
  • 使用 GetProcAddress找到函数指针并调用它。 GetModuleHandle对于 Win32 子系统来说就足够了,因为每个 Win32 程序都保证已加载 ntdll.dll .

  • 方法一: ntdll.lib
    如果您有 DDK/WDK - 用于驱动程序开发套件和 Windows Driver Kit分别 - 你得到全套 ntdll.lib文件已经。在我的系统上(Windows 7 WDK 7600.16385.1):
    C:\WINDDK\7600.16385.1\lib\win7\amd64\ntdll.lib
    C:\WINDDK\7600.16385.1\lib\win7\i386\ntdll.lib
    C:\WINDDK\7600.16385.1\lib\win7\ia64\ntdll.lib
    C:\WINDDK\7600.16385.1\lib\wlh\amd64\ntdll.lib
    C:\WINDDK\7600.16385.1\lib\wlh\i386\ntdll.lib
    C:\WINDDK\7600.16385.1\lib\wlh\ia64\ntdll.lib
    C:\WINDDK\7600.16385.1\lib\wnet\amd64\ntdll.lib
    C:\WINDDK\7600.16385.1\lib\wnet\i386\ntdll.lib
    C:\WINDDK\7600.16385.1\lib\wnet\ia64\ntdll.lib
    C:\WINDDK\7600.16385.1\lib\wxp\i386\ntdll.lib

    创建您自己的临时工具 ntdll.lib
    否则你必须生成 ntdll.lib您自己来自 dumpbin 的输出(或通过其他方式允许解析 DLL 的导出)然后您可以将其输出到模块定义文件中,您可以从中构建导出 .lib .听起来很复杂?没有那么多,让我们看看;)

    使用 Ero Carrera 的 pefile Python module , 我们可以完成这个:
    import os, re, sys
    from os.path import basename, dirname, join, realpath
    try:
    import pefile
    except ImportError:
    try:
    sys.path.append(join(realpath(dirname(__file__)), "pefile"))
    import pefile
    except:
    raise

    def main(pename):
    from pefile import PE
    print "Parsing %s" % pename
    pe = PE(pename)
    if not getattr(pe, "DIRECTORY_ENTRY_EXPORT", None):
    return "ERROR: given file has no exports."
    modname = basename(pename)
    libname = re.sub(r"(?i)^.*?([^\\/]+)\.(?:dll|exe|sys|ocx)$", r"\1.lib", modname)
    defname = libname.replace(".lib", ".def")
    print "Writing module definition file %s for %s" % (defname, modname)
    with open(defname, "w") as f: # want it to throw, no sophisticated error handling here
    print >>f, "LIBRARY %s\n" % (modname)
    print >>f, "EXPORTS"
    numexp = 0
    for exp in [x for x in pe.DIRECTORY_ENTRY_EXPORT.symbols if x.name]:
    numexp += 1
    print >>f, "\t%s" % (exp.name)
    print "Wrote %s with %d exports" % (defname, numexp)
    print "\n\nUse this to create the export lib:\n\tlib /def:%s /out:%s" % (defname, libname)

    if __name__ == '__main__':
    if len(sys.argv) != 2:
    sys.exit("ERROR:\n\tSyntax: fakelib <dllfile>\n")
    sys.exit(main(sys.argv[1]))

    运行此脚本的示例输出(命名为 fakelib.py 时)将是:
    > fakelib.py ntdll.dll
    Parsing ntdll.dll
    Writing module definition file ntdll.def for ntdll.dll
    Wrote ntdll.def with 1984 exports


    Use this to create the export lib:
    lib /def:ntdll.def /out:ntdll.lib

    然后我们运行最后一行给出的命令。最好给 /machine:参数,当然。这留给读者作为“锻炼”(*咳嗽* *咳嗽*)。 VS 2012 的输出将是:
    > lib /def:ntdll.def /out:ntdll.lib
    Microsoft (R) Library Manager Version 11.00.51106.1
    Copyright (C) Microsoft Corporation. All rights reserved.

    LINK : warning LNK4068: /MACHINE not specified; defaulting to X86
    Creating library ntdll.lib and object ntdll.exp

    恭喜。您现在可以使用 ntdll.lib由微软自己创建 lib.exentdll.dll 静态导入,即使没有“真实”(原创) .lib在你的系统上。

    根据您的需要和喜好调整路径和文件名。

    使用 MinGW 时

    Damon在评论中指出 MinGW 包含的工具链包含一个工具 gendef可以完成上述 Python 脚本的工作,并且可以将输出提供给 dlltool .

    问题

    当以 x64(64 位)为目标时,上述方法非常有效,但对于 x86(32 位),我有时会遇到链接器错误。

    问题是 __stdcall 的名称修饰x64 和 x86 之间有所不同。前者并没有真正使用相同的 __stdcall作为 x86,因此只在前面加上一个下划线。但是,后者还附加了参数的数量乘以 sizeof(void*) (即 4)。因此,对于一个参数,函数的修饰函数名称 int __stdcall foo(int);变成 _foo@4 .

    This KB article from Microsoft概述了解决该问题的方法。

    方法二:动态导入,使用 GetProcAddress
    MSDN 中的文档状态(对于 NtOpenFile):

    Note that the DDK header file Ntdef.h is necessary for many constant definitions as well as the InitializeObjectAttributes macro. The associated import library, Ntdll.lib is also available in the DDK. You can also use the LoadLibrary and GetProcAddress functions to dynamically link to Ntdll.dll.



    声明一个函数类型,例如在这里我们声明类型 TFNNtOpenFile适合您的情况:
    typedef NTSTATUS (NTAPI  *TFNNtOpenFile)(
    OUT PHANDLE FileHandle,
    IN ACCESS_MASK DesiredAccess,
    IN POBJECT_ATTRIBUTES ObjectAttributes,
    OUT PIO_STATUS_BLOCK IoStatusBlock,
    IN ULONG ShareAccess,
    IN ULONG OpenOptions
    );

    ...然后检索函数指针并调用它:
    TFNNtOpenFile pfnNtOpenFile = (TFNNtOpenFile)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtOpenFile");
    status = pfnNtOpenFile(...); // can't be bothered to type out all parameters ;)

    检索函数指针的另一种方法可能是这样的:
    static NTSTATUS (NTAPI  *NtOpenFile)(
    OUT PHANDLE,
    IN ACCESS_MASK,
    IN POBJECT_ATTRIBUTES,
    OUT PIO_STATUS_BLOCK,
    IN ULONG,
    IN ULONG
    );
    (FARPROC)&NtOpenFile = GetProcAddress(GetModuleHandle("ntdll.dll"), "NtOpenFile");

    可以通过使用预处理器字符串化运算符 ( # ) 进一步压缩。这是你的选择。

    关于c++ - LNK2019 未解析的外部符号 NtOpenFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6774967/

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