Get system information UWP
-
Hi! I want to develop the
Universal Windows Application
which will display the system information such asRAM
capacity,GPU
information,Drives
and so on. I used theWMI
forWin32 (Desktop)
app. From the officialUWP
documentation I can't use theWMI
. So how to get such information? Thanks. -
@Cobra91151 Hi, friend. Welcome.
QSysinfo could give you some help.
-
It's not gonna be that easy. UWP is by design a sandbox so you don't get to poke inside the system or other processes too deep, but you might be able to gather some bits and pieces.
For the filesystem info you can check out the Windows.Devices.Enumeration namespace. Some of it may be coveredQFileSystemModel
, but I haven't checked.
For the RAM info you might get some info via Windows.System.MemoryManager.
For GPU I'd imagine you'd have to go through the DirectX layer, but since Qt uses ANGLE wrapper for UWP you would probably get the ANGLE info, not the gpu under it, unless of course you bypass Qt and go to the DirectX directly. -
I will check it. Thank you.
-
Any documentation how to use
UWP API
withQt
? InVisual Studio
I included:using namespace Windows::Devices::Enumeration;
and it works. When adding/including namespaces inQt
I get errors.For example:
.h #include <windows.devices.enumeration.h> .cpp DeviceAccessInformation deviceInfo;
I get error:
C2065: 'DeviceAccessInformation': undeclared identifier
Thanks.
-
There are couple of ways to access UWP components with C++. Apart from the hacky c++/cx language extensions Microsoft created there's now a nice header-only library wrapper in native C++ called C++/WinRT. The great thing is it's included directly in the Windows SDK now.
When you create a Visual Studio project it has a preoperty that selects the Windows SDK version and sets up the needed include and lib paths.
If you're using qmake for your project you need to set up these paths yourself. Since C++/WinRT is a header-only library all you need is the right include path set up. Depending on which version of Windows SDK you're using and your installation path it might look something like this in your .pro file:INCLUDEPATH += "C:/Program Files (x86)/Windows Kits/10/Include/10.0.16299.0"
After that re-run qmake (in Qt Creator menu Build -> Run qmake) and you can do:
#include "winrt/windows.devices.enumeration.h"
For examples on how to use it you can browse through the various samples on the github page of the project.
-
I have added the
Win SDK
path (INCLUDEPATH += "C:/Program Files (x86)/Windows Kits/10/Include/10.0.16299.0"
) to the.pro
file and it is the same as your include path. Also added the#include "winrt/windows.devices.enumeration.h"
to the header file but it still can't find theDeviceAccessInformation deviceInfo;
, throws error: C2065:DeviceAccessInformation
: undeclared identifier.Also I have checked the samples, and add the namespace.
using namespace winrt;
and it displays the error:
C2871: 'winrt': a namespace with this name does not exist
Also I have noticed the warning: :
-1: warning: winrt_manifest_install.path is not defined: install target not created
I think the problem is with the wrong includes or I missing something.
-
I have included the
WindowsApp
library but the issue still exists.Code:
.procontains(QMAKE_TARGET.arch, x86_64) { LIBS += -L"C:/Program Files (x86)/Windows Kits/10/Lib/10.0.16299.0/um/x64" -lWindowsApp } else { LIBS += -L"C:/Program Files (x86)/Windows Kits/10/Lib/10.0.16299.0/um/x86" -lWindowsApp }
-
Sorry, it seems I jumped the gun on the info about C++/WinRT being part of Windows SDK already. According to the author it's currently in the preview versions and will be part of the future official releases. I got confused because there's a
winrt
directory with all the headers in the SDK, but that's for C++/cx. C++/WinRT will be placed incppwinrt
directory.So for now you need to download C++/WinRT separately from https://github.com/Microsoft/cppwinrt.
Apparently it's also not entirely header-only so you do need to addwindowsapp
to the linker.
Last thing is that the library uses C++17 so you'll need to enable that in the compiler.So here's an example I got to work that lists all the devices on your pc.
In your .pro file add these:INCLUDEPATH += <wherever you downloaded the winrt library from github> QMAKE_CXXFLAGS += /std:c++17 LIBS += -lwindowsapp
And the
main.cpp
looks like this:#include <QString> #include <QDebug> #include "winrt/Windows.Devices.Enumeration.h" using namespace winrt; using namespace winrt::Windows::Devices::Enumeration; int main() { init_apartment(); //this initializes com DeviceInformationCollection infos = DeviceInformation::FindAllAsync().get(); for (const auto& info : infos) qDebug() << QString::fromWCharArray(info.Name().c_str()); }
UWP libraries are highly asynchronous so if you'd like to use some of those async features you will also have to add the
/await
compiler flag to enable coroutine support in the compiler. -
@Chris-Kawa
What do you mean: INCLUDEPATH += <wherever you downloaded the winrt library from github>? There are only header files (in the.../cppwinrt-master/10.0.16299.0/winrt
) directory. I have downloaded it from the link:
https://github.com/Microsoft/cppwinrt . Thanks..pro:
QMAKE_CXXFLAGS += /std:c++17 INCLUDEPATH += "C:/Users/cobra/Downloads/cppwinrt-master/10.0.16299.0" LIBS += -lwindowsapp
.h
#include "winrt/windows.devices.enumeration.h" using namespace winrt; using namespace winrt::Windows::Devices::Enumeration;
.cpp
init_apartment(); //this initializes com DeviceInformationCollection infos = DeviceInformation::FindAllAsync().get(); for (const auto &info : infos) { qDebug() << QString::fromWCharArray(info.Name().c_str()); }
It throws a lot of errors:
-
I have checked it and all errors are from
cppwinrt
directory where theheader
files located.I think something are overriding the
cppwinrt
header files or I use the wrong library. ButQt
highlights the code so I am on the right direction. Thank you. -
I have found why these errors occurs. The problem is with the
Qt Kits
. I have 3Qt UWP
kits installed:- Qt 5.9.3 for
UWP x32
- Qt 5.9.3 for
UWP x64
- Qt 5.9.3 for
UWP armv7
So when I'm trying to build for
x32
orx64
UWP
binaries, it throws the errors. But when I setarmv7
kit as build target then no errors found and build succeeds. So the question is how to targetx32/x64
UWP
? Thanks. - Qt 5.9.3 for
-
Can anyone confirm that
Qt 5.9.3 UWP x32/x64
kits work withUWP API
? Thanks in advance.