简体   繁体   中英

How to get Windows path using Qt/C++

I am trying to get the windows path using Qt and C++. The below code compiles, but not gettting the windows folder path in Qt. The same code works in 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 :

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:

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

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

I don't think there is a specific Qt function to do this.

The nearest is QSysinfo which tells you the windows version. However SHGetFolderPath() shoudl work in Qt just as well as any other win API call.

ps In Windows vista-> this is replaced with 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.

If your application is not Terminal Services aware, you may get a different directory under TS environment. Found this out myself today, not that I've ever been bit by %windir% or %SystemRoot% or using the ShGetKnownFolderPath or GetWindowsDirectory APIs.

I've opted for using GetSystemWindowsDirectory which exists Windows 2000 and upward. Microsoft's page for the function is here.

Further explanation by Raymond Chen is 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. Note: this code is ANSI (...single byte chars in 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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