Windows API函数不可用,但它应该可用

Windows API function not available while it should be

本文关键字:API Windows 函数      更新时间:2023-10-16

我正在尝试使用Windows的API创建符号链接,但是CreateSymbolicLink函数似乎不存在,尽管我包含了windows.h

我正在使用Qt Creator。我可以使用API中除此之外的其他函数。

有人能解释一下原因吗?

我使用的是Windows 8.1。

编辑:

我的代码:

#include "windows.h"
bool createSymbolicLink(const int &type, const QString &linkPath, const QString &targetPath) {
    DWORD windows_Type = type;
    wchar_t* windows_LinkPath = new wchar_t[linkPath.length()];
    linkPath.toWCharArray(windows_LinkPath);
    wchar_t* windows_TargetPath = new wchar_t[targetPath.length()];
    targetPath.toWCharArray(windows_TargetPath);
    return CreateSymbolicLinkW(windows_LinkPath, windows_TargetPath, windows_Type) != 0;
}

错误:未在此作用域中声明"CreateSymbolicLinkW"

CreateSymbolicLink对Vista有最低要求,如MSDN中所述。

这意味着您需要通过条件定义的方式指定您的目标Vista及以上版本。您可以在这里找到更多文档:

  • 使用Windows标头
  • 修改WINVER和_WIN32_WINNT

因此,在包含windows头文件之前,可以先包含这些定义。

#define WINVER 0x0600
#define _WIN32_WINNT 0x0600

如果这没有帮助,那么您的SDK已经过时,需要切换到更新的SDK。