Added auto-detection of AMD and Intel SMBus adapters. WMI class by Stavros Avramidis (@purpl3F0x).
This commit is contained in:
parent
6e9cf5e751
commit
d91acc5a52
5 changed files with 310 additions and 0 deletions
Binary file not shown.
|
|
@ -115,10 +115,12 @@
|
|||
<ConformanceMode>true</ConformanceMode>
|
||||
<PrecompiledHeaderFile />
|
||||
<PrecompiledHeaderOutputFile />
|
||||
<AdditionalIncludeDirectories>..\dependencies\inpout32_1501\Win32;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalLibraryDirectories>..\dependencies\inpout32_1501\Win32;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
|
|
@ -156,12 +158,14 @@
|
|||
<ConformanceMode>true</ConformanceMode>
|
||||
<PrecompiledHeaderFile />
|
||||
<PrecompiledHeaderOutputFile />
|
||||
<AdditionalIncludeDirectories>..\dependencies\inpout32_1501\Win32;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalLibraryDirectories>..\dependencies\inpout32_1501\Win32;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
|
|
@ -169,6 +173,7 @@
|
|||
<ClInclude Include="i2c_smbus.h" />
|
||||
<ClInclude Include="i2c_smbus_i801.h" />
|
||||
<ClInclude Include="i2c_smbus_piix4.h" />
|
||||
<ClInclude Include="wmi.h" />
|
||||
<ClInclude Include="OpenAuraSDK.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
|
@ -177,6 +182,7 @@
|
|||
<ClCompile Include="i2c_smbus.cpp" />
|
||||
<ClCompile Include="i2c_smbus_i801.cpp" />
|
||||
<ClCompile Include="i2c_smbus_piix4.cpp" />
|
||||
<ClCompile Include="wmi.cpp" />
|
||||
<ClCompile Include="OpenAuraSDK.cpp" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
|
|
|
|||
|
|
@ -30,6 +30,9 @@
|
|||
<ClInclude Include="i2c_smbus_i801.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="wmi.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="OpenAuraSDK.cpp">
|
||||
|
|
@ -50,5 +53,8 @@
|
|||
<ClCompile Include="i2c_smbus.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="wmi.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
262
OpenAuraSDK/wmi.cpp
Normal file
262
OpenAuraSDK/wmi.cpp
Normal file
|
|
@ -0,0 +1,262 @@
|
|||
#include "wmi.h"
|
||||
|
||||
// Taken from https://stackoverflow.com/questions/215963/
|
||||
// Convert a wide Unicode string to an UTF8 string
|
||||
std::string utf8_encode(const std::wstring& wstr)
|
||||
{
|
||||
if (wstr.empty()) return std::string();
|
||||
|
||||
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int) wstr.size(), nullptr, 0, nullptr, nullptr);
|
||||
std::string strTo(size_needed, 0);
|
||||
WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int) wstr.size(), &strTo[0], size_needed, nullptr, nullptr);
|
||||
|
||||
return strTo;
|
||||
}
|
||||
|
||||
// Convert an UTF8 string to a wide Unicode String
|
||||
std::wstring utf8_decode(const std::string& str)
|
||||
{
|
||||
if (str.empty())
|
||||
{
|
||||
return std::wstring();
|
||||
}
|
||||
|
||||
int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int) str.size(), nullptr, 0);
|
||||
std::wstring wstrTo(size_needed, 0);
|
||||
MultiByteToWideChar(CP_UTF8, 0, &str[0], (int) str.size(), &wstrTo[0], size_needed);
|
||||
return wstrTo;
|
||||
}
|
||||
|
||||
bool isMatch(const std::string& value, const std::regex& re)
|
||||
{
|
||||
return std::regex_match(value, re);
|
||||
}
|
||||
|
||||
Wmi::Wmi() : pLoc(nullptr), pSvc(nullptr)
|
||||
{
|
||||
|
||||
};
|
||||
|
||||
Wmi::~Wmi()
|
||||
{
|
||||
pSvc->Release();
|
||||
pLoc->Release();
|
||||
CoUninitialize();
|
||||
}
|
||||
|
||||
HRESULT Wmi::init()
|
||||
{
|
||||
HRESULT hres;
|
||||
|
||||
// Initialize COM. ------------------------------------------
|
||||
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
|
||||
if (FAILED(hres))
|
||||
{
|
||||
return hres;
|
||||
}
|
||||
|
||||
// Set general COM security levels --------------------------
|
||||
hres = CoInitializeSecurity(
|
||||
nullptr,
|
||||
-1, // COM authentication
|
||||
nullptr, // Authentication services
|
||||
nullptr, // Reserved
|
||||
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
|
||||
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
|
||||
nullptr, // Authentication info
|
||||
EOAC_NONE, // Additional capabilities
|
||||
nullptr // Reserved
|
||||
);
|
||||
|
||||
if (FAILED(hres))
|
||||
{
|
||||
CoUninitialize();
|
||||
return hres;
|
||||
}
|
||||
|
||||
// Obtain the initial locator to WMI -------------------------
|
||||
hres = CoCreateInstance(
|
||||
CLSID_WbemLocator,
|
||||
nullptr,
|
||||
CLSCTX_INPROC_SERVER,
|
||||
IID_IWbemLocator, (LPVOID*) &pLoc
|
||||
);
|
||||
|
||||
if (FAILED(hres))
|
||||
{
|
||||
CoUninitialize();
|
||||
return hres;
|
||||
}
|
||||
|
||||
hres = pLoc->ConnectServer(
|
||||
_bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
|
||||
nullptr, // User name. NULL = current user
|
||||
nullptr, // User password. NULL = current
|
||||
nullptr, // Locale. NULL indicates current
|
||||
0, // Security flags.
|
||||
nullptr, // Authority (for example, Kerberos)
|
||||
nullptr, // Context object
|
||||
&pSvc // pointer to IWbemServices proxy
|
||||
);
|
||||
|
||||
if (FAILED(hres))
|
||||
{
|
||||
pLoc->Release();
|
||||
CoUninitialize();
|
||||
return hres;
|
||||
}
|
||||
|
||||
// Set security levels on the proxy -------------------------
|
||||
hres = CoSetProxyBlanket(
|
||||
pSvc, // Indicates the proxy to set
|
||||
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
|
||||
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
|
||||
nullptr, // Server principal name
|
||||
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
|
||||
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
|
||||
nullptr, // client identity
|
||||
EOAC_NONE // proxy capabilities
|
||||
);
|
||||
|
||||
if (FAILED(hres))
|
||||
{
|
||||
pSvc->Release();
|
||||
pLoc->Release();
|
||||
CoUninitialize();
|
||||
return hres;
|
||||
}
|
||||
|
||||
// Initialised WMI successfully
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
HRESULT Wmi::query(std::string queryStr, std::vector<QueryObj>& queryVectorOut, const AdditionalFilters* filters)
|
||||
{
|
||||
HRESULT hres;
|
||||
int nIdx = 0;
|
||||
IEnumWbemClassObject* pEnumerator = nullptr;
|
||||
|
||||
// Make the WMI query
|
||||
hres = pSvc->ExecQuery(
|
||||
bstr_t("WQL"),
|
||||
bstr_t(queryStr.c_str()),
|
||||
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
|
||||
nullptr,
|
||||
&pEnumerator
|
||||
);
|
||||
|
||||
if (FAILED(hres))
|
||||
{
|
||||
return hres;
|
||||
}
|
||||
|
||||
IWbemClassObject* pclsObj = nullptr;
|
||||
ULONG uReturn = 0;
|
||||
|
||||
while (pEnumerator)
|
||||
{
|
||||
hres = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
|
||||
|
||||
if (0==uReturn)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
VARIANT vtProp;
|
||||
if (filters)
|
||||
{
|
||||
for (auto filter: *filters)
|
||||
{
|
||||
hres = pclsObj->Get(utf8_decode(filter.first).c_str(), 0, &vtProp, nullptr, nullptr);
|
||||
|
||||
if (FAILED(hres))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
auto val = utf8_encode(vtProp.bstrVal);
|
||||
|
||||
if (!std::regex_match(val, filter.second))
|
||||
{
|
||||
goto _NextElement;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SAFEARRAY* sfArray;
|
||||
LONG lstart, lend;
|
||||
|
||||
//Get Wmi objects names
|
||||
hres = pclsObj->GetNames(0, WBEM_FLAG_ALWAYS, nullptr, &sfArray);
|
||||
|
||||
if (FAILED(hres))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Find safe array boundaries
|
||||
SafeArrayGetLBound(sfArray, 1, &lstart);
|
||||
SafeArrayGetUBound(sfArray, 1, &lend);
|
||||
|
||||
BSTR* pbstr;
|
||||
hres = SafeArrayAccessData(sfArray, (void HUGEP**) &pbstr);
|
||||
nIdx = 0;
|
||||
|
||||
if (FAILED(hres))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
{
|
||||
CIMTYPE pType;
|
||||
QueryObj item;
|
||||
|
||||
for (nIdx = lstart; nIdx < lend; nIdx++)
|
||||
{
|
||||
hres = pclsObj->Get(pbstr[nIdx], 0, &vtProp, &pType, 0);
|
||||
|
||||
if (FAILED(hres))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (vtProp.vt == VT_NULL)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if ((pType == CIM_STRING || pType == CIM_REFERENCE) && pType != CIM_EMPTY && pType != CIM_ILLEGAL)
|
||||
{
|
||||
item.emplace(utf8_encode(pbstr[nIdx]), utf8_encode(vtProp.bstrVal));
|
||||
}
|
||||
|
||||
VariantClear(&vtProp);
|
||||
}
|
||||
|
||||
hres = pclsObj->Get(L"Dependent", 0, &vtProp, &pType, nullptr);
|
||||
|
||||
if (pType != CIM_EMPTY && pType != CIM_ILLEGAL && SUCCEEDED(hres))
|
||||
{
|
||||
item.emplace(utf8_encode(pbstr[nIdx]), utf8_encode(vtProp.bstrVal));
|
||||
}
|
||||
|
||||
// Push item to vector
|
||||
queryVectorOut.emplace_back(item);
|
||||
|
||||
// Empty sfArray
|
||||
SafeArrayUnaccessData(sfArray);
|
||||
|
||||
SafeArrayDestroy(sfArray); // Delete sfArray
|
||||
sfArray = nullptr; // Avoid dangling pointers
|
||||
}
|
||||
|
||||
_NextElement:
|
||||
VariantClear(&vtProp); // Clear vtProp
|
||||
pclsObj->Release(); // Release pclsObj
|
||||
}
|
||||
|
||||
pEnumerator->Release();
|
||||
pEnumerator = nullptr;
|
||||
|
||||
return S_OK;
|
||||
}
|
||||
36
OpenAuraSDK/wmi.h
Normal file
36
OpenAuraSDK/wmi.h
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <regex>
|
||||
|
||||
#include <comdef.h>
|
||||
#include <Wbemidl.h>
|
||||
#include <Windows.h>
|
||||
|
||||
#pragma comment(lib, "wbemuuid.lib")
|
||||
|
||||
|
||||
typedef std::map<std::string, std::string> QueryObj;
|
||||
typedef std::map<std::string, std::regex> AdditionalFilters;
|
||||
typedef std::pair<std::string, std::regex> AdditionalFilter;
|
||||
|
||||
// Wmi Base class
|
||||
class Wmi
|
||||
{
|
||||
public:
|
||||
Wmi();
|
||||
~Wmi();
|
||||
|
||||
// Initialises connection to WMI host
|
||||
HRESULT init();
|
||||
|
||||
HRESULT query(std::string queryStr,
|
||||
std::vector<QueryObj>& queryVectorOut,
|
||||
const AdditionalFilters* filters = nullptr);
|
||||
|
||||
private:
|
||||
IWbemLocator *pLoc = nullptr;
|
||||
IWbemServices *pSvc= nullptr;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue