【问题标题】:How to get Windows path using Qt/C++如何使用 Qt/C++ 获取 Windows 路径
【发布时间】:2012-06-05 17:03:35
【问题描述】:

我正在尝试使用 Qt 和 C++ 获取 Windows 路径。下面的代码编译,但没有在 Qt 中获取 windows 文件夹路径。相同的代码适用于 Visual Studio 2010

      wchar_t path[MAX_PATH];
      SHGetFolderPath(NULL, CSIDL_WINDOWS, NULL, 0, path);

以下代码更改似乎有效:

     int const bufferSize = 512;        
     QScopedPointer<WCHAR> dirPath(new WCHAR[bufferSize]);
     ZeroMemory( dirPath.operator ->(), bufferSize);
     SHGetFolderPath(NULL, CSIDL_WINDOWS, NULL, 0, dirPath.operator ->());

【问题讨论】:

  • “在 QT 中”是什么意思? Qt 是一个库,它无法与 Visual Studio 之类的 IDE 相提并论。您可以将 Qt 与多种编译器一起使用,包括 Visual Studio 使用的 Visual C++ 编译器。
  • 我需要使用 QT 4.6.3 使用 windows API 获取 windows 路径
  • 您指的是 Qt Creator IDE 吗?这与 Qt 不同。就目前而言,您的问题与 Qt 无关。它是 Qt,而不是 QT。
  • 我的开发环境是 QT Creator/QT 库,我也在使用 Windows API。
  • This answer 为我工作。

标签: c++ qt


【解决方案1】:

没有一个 Qt 函数可以做到这一点,但是你的要求可以通过读取环境变量 WINDIR 来实现:

QStringList env_list(QProcess::systemEnvironment());

int idx = env_list.indexOf(QRegExp("^WINDIR=.*", Qt::CaseInsensitive));
if (idx > -1)
{
    QStringList windir = env_list[idx].split('=');
    qDebug() << "Var : " << windir[0];
    qDebug() << "Path: " << windir[1];
}

输出:

Var :  "WINDIR"
Path:  "C:\WINDOWS"

【讨论】:

  • 如果 Windows 安装在其他地方(可能吗?),或者在本地化版本上被称为其他名称,则不一定。很多人在他们的应用程序中硬编码“程序文件”并被 64 位咬伤
  • 人们发现他们的应用程序现在位于“Program Files(x86)”中。 “Windows”没有翻译吗?
  • 我重写了答案,值得一试。也许删除你的 cmets 也是合适的,因为它们已经过时了。
【解决方案2】:
QString windowsInstallPath;

#ifdef Q_WS_WIN
QDir d;
if (d.cd("%windir%"))
    windowsInstallPath = d.absolutePath();
#endif

if (!windowsInstallPath.isNull())
    qDebug() << windowsInstallPath;
else
    qDebug() << "Not compiled for Windows";

应该可以。

【讨论】:

    【解决方案3】:

    我认为获取Windows目录的另一种非常合理的方法是从传递给程序的环境中获取它:

    QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
    qDebug() << env.value("windir");
    

    https://doc.qt.io/qt-5/qprocessenvironment.html

    【讨论】:

      【解决方案4】:

      我认为没有特定的 Qt 函数可以做到这一点。

      最近的是QSysinfo,它告诉您Windows 版本。但是 SHGetFolderPath() 应该在 Qt 中和任何其他 win API 调用一样工作。

      ps 在 Windows vista 中-> 替换为 SHGetKnownFolderPath

      【讨论】:

      • 变量类型 wchar_t path[MAX_PATH] 似乎有问题,我认为它与 QT 兼容
      • 可能依赖于 vs2010 中的 UNICODE 设置,该函数可能不包含在宽字符形式中
      【解决方案5】:

      这是一个单行解决方案:

      QString winPath = QString::fromUtf8(qgetenv("windir"));

      这也可以用于任何环境变量。我不确定qgetenv 是否在 Qt4 中可用,但它在 Qt5 中。

      【讨论】:

        【解决方案6】:

        如果您的应用程序不支持终端服务,您可能会在 TS 环境下获得不同的目录。今天我自己发现了这一点,并不是说我曾经被 %windir% 或 %SystemRoot% 或使用过 ShGetKnownFolderPath 或 GetWindowsDirectory API 咬过。

        我选择使用存在于 Windows 2000 及更高版本的 GetSystemWindowsDirectory。 Microsoft's page for the function is here.

        Raymond Chen 进一步解释is here.

        最后,代码...

        它是用 Delphi 6 编写的。很抱歉 :) 这就是我目前正在编写的代码,但如果您有使用您的语言编写的 GetWindowsDirectory 代码,那么只需要一些复制 + 重命名,因为函数签名是完全相同的。注意:此代码是 ANSI(...Delphi 6 中的单字节字符)。

        function GetSystemWindowsDirectoryA(lpBuffer: PAnsiChar; uSize: UINT): UINT; stdcall; external kernel32 name 'GetSystemWindowsDirectoryA';
        
        function GetSystemWindowsDirectory: string;
        var
          buf: array[0..MAX_PATH] of Char;
          resultLength: Cardinal;
        begin
          resultLength := GetSystemWindowsDirectoryA(@buf, SizeOf(buf));
          if resultLength = 0 then
            RaiseLastOSError;
          SetLength(Result, resultLength);
          Move(buf, PChar(Result)^, resultLength);
        end;
        

        【讨论】:

          猜你喜欢
          • 2018-01-05
          • 1970-01-01
          • 2011-01-30
          • 2017-02-20
          • 1970-01-01
          • 1970-01-01
          • 2010-11-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多