简体   繁体   English

如何使用 Qt/C++ 获取 Windows 路径

[英]How to get Windows path using Qt/C++

I am trying to get the windows path using Qt and C++.我正在尝试使用 Qt 和 C++ 获取 Windows 路径。 The below code compiles, but not gettting the windows folder path in Qt.下面的代码编译,但没有在 Qt 中获取 windows 文件夹路径。 The same code works in Visual Studio 2010相同的代码适用于 Visual Studio 2010

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

The below code change seems working:以下代码更改似乎有效:

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

There isn't a Qt function to do this, but what you are asking could be achieved by reading the environtment variable WINDIR :没有 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];
}

Outputs:输出:

Var :  "WINDIR"
Path:  "C:\WINDOWS"
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";

Should work.应该管用。

I think another very reasonable way to get the Windows directory would be to get it from the environment passed to the program:我认为获取 Windows 目录的另一种非常合理的方法是从传递给程序的环境中获取它:

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

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

I don't think there is a specific Qt function to do this.我认为没有特定的 Qt 函数可以做到这一点。

The nearest is QSysinfo which tells you the windows version.最近的是QSysinfo ,它会告诉您 Windows 版本。 However SHGetFolderPath() shoudl work in Qt just as well as any other win API call.但是 SHGetFolderPath() 应该在 Qt 中与任何其他 win API 调用一样工作。

ps In Windows vista-> this is replaced with SHGetKnownFolderPath ps 在 Windows vista-> 这被替换为SHGetKnownFolderPath

Here is a one line solution:这是一个单行解决方案:

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

This can also be used for any environment variable.这也可以用于任何环境变量。 I am not sure if qgetenv is available in Qt4 but it is in Qt5.我不确定qgetenv在 Qt4 中是否可用,但它在 Qt5 中。

If your application is not Terminal Services aware, you may get a different directory under TS environment.如果您的应用程序不知道终端服务,您可能会在 TS 环境下获得不同的目录。 Found this out myself today, not that I've ever been bit by %windir% or %SystemRoot% or using the ShGetKnownFolderPath or GetWindowsDirectory APIs.今天我自己发现了这一点,并不是说我曾经被 %windir% 或 %SystemRoot% 或使用 ShGetKnownFolderPath 或 GetWindowsDirectory API 所困扰。

I've opted for using GetSystemWindowsDirectory which exists Windows 2000 and upward.我选择使用存在于 Windows 2000 及更高版本的 GetSystemWindowsDirectory。 Microsoft's page for the function is here. Microsoft 的功能页面在这里。

Further explanation by Raymond Chen is here. Raymond Chen 的进一步解释 是here。

Finally, the code...最后,代码...

It's written in Delphi 6. Sorry about that :) It's what I'm coding in at the moment, but if you have code for GetWindowsDirectory in your language, then just a few copy + renames are needed as the function signatures are identical.它是用 Delphi 6 编写的。对此很抱歉 :) 这是我目前正在编写的代码,但是如果您有使用您的语言编写的 GetWindowsDirectory 代码,那么只需要几个副本 + 重命名,因为函数签名是相同的。 Note: this code is ANSI (...single byte chars in Delphi 6).注意:此代码是 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM