- 1 year ago
How to check whether the user is using NVIDIA or AMD in C++
Comment
1 / 1
To check whether the user's system has an NVIDIA or AMD GPU in C++, you can use the Windows Management Instrumentation (WMI) to query information about the installed GPUs. Here's how you can do it:
- Include the necessary headers:
cpp#include <iostream>
#include <comdef.h>
#include <Wbemidl.h>
#pragma comment(lib, "wbemuuid.lib")
- Initialize COM:
cppHRESULT hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
std::cout << "Failed to initialize COM library. Error code: " << hres << std::endl;
return 1;
}
- Connect to WMI:
cppIWbemLocator* pLoc = nullptr;
hres = CoCreateInstance(CLSID_WbemLocator, nullptr, CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID*)&pLoc);
if (FAILED(hres))
{
std::cout << "Failed to create IWbemLocator object. Error code: " << hres << std::endl;
CoUninitialize();
return 1;
}
IWbemServices* pSvc = nullptr;
hres = pLoc->ConnectServer(_bstr_t(L"ROOT\\CIMV2"), nullptr, nullptr, 0, nullptr, 0, 0, &pSvc);
if (FAILED(hres))
{
std::cout << "Could not connect to WMI. Error code: " << hres << std::endl;
pLoc->Release();
CoUninitialize();
return 1;
}
- Set the security levels on WMI:
cpphres = CoSetProxyBlanket(pSvc, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, nullptr,
RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, nullptr, EOAC_NONE);
if (FAILED(hres))
{
std::cout << "Could not set proxy blanket on WMI. Error code: " << hres << std::endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}
- Query the WMI for GPU information:
cppIEnumWbemClassObject* pEnumerator = nullptr;
hres = pSvc->ExecQuery(bstr_t("WQL"), bstr_t("SELECT * FROM Win32_VideoController"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, nullptr, &pEnumerator);
if (FAILED(hres))
{
std::cout << "Query for GPU information failed. Error code: " << hres << std::endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}
IWbemClassObject* pclsObj = nullptr;
ULONG uReturn = 0;
while (pEnumerator)
{
hres = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if (uReturn == 0)
break;
VARIANT vtProp;
hres = pclsObj->Get(L"Caption", 0, &vtProp, 0, 0);
if (FAILED(hres))
{
std::cout << "Failed to get GPU information. Error code: " << hres << std::endl;
pclsObj->Release();
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}
std::wstring gpuCaption = vtProp.bstrVal;
VariantClear(&vtProp);
// Check if the GPU is from NVIDIA or AMD
if (gpuCaption.find(L"NVIDIA") != std::wstring::npos)
{
std::cout << "NVIDIA GPU found!" << std::endl;
}
else if (gpuCaption.find(L"AMD") != std::wstring::npos)
{
std::cout << "AMD GPU found!" << std::endl;
}
pclsObj->Release();
}
- Release COM objects and uninitialize COM:
cpppSvc->Release();
pLoc->Release();
pEnumerator->Release();
CoUninitialize();
Remember to include appropriate error handling and cleanup as needed. This code uses the WMI to query information about the installed video controllers (GPUs) and checks for NVIDIA or AMD in the "Caption" property to determine the GPU type.