Commit cd251597 authored by captainwong's avatar captainwong

wmi simpleSelect

parent cc4290c0
......@@ -7,6 +7,9 @@
namespace jlib
{
/**************************** trim ***************************/
// Taken from https://stackoverflow.com/a/217605/2963736
// Thanks https://stackoverflow.com/users/13430/evan-teran
......@@ -80,4 +83,29 @@ inline std::wstring trim_copy(std::wstring s) {
trim(s); return s;
}
/**************************** join ***************************/
/**
* @brief join 字符串
* @note StringType 可以为std::string或std::wstring
* @note StringContainer 必须为上述StringType的iterable容器类,如vector,list
*/
template <typename StringType, typename StringContainer>
StringType join(const StringContainer& container, const StringType& conjunction = StringType())
{
StringType result;
auto itBegin = container.cbegin();
auto itEnd = container.cend();
if (itBegin != itEnd) {
result = *itBegin;
itBegin++;
}
for (; itBegin != itEnd; itBegin++) {
result += conjunction;
result += *itBegin;
}
return result;
}
} // namespace jlib
......@@ -24,9 +24,9 @@ enum class Branch : int {
InvalidBranch = 0x0FFFFFFF,
};
static constexpr const char* BranchNameTest = "test";
static constexpr const char* BranchNameExperimental = "experimental";
static constexpr const char* BranchNameStable = "stable";
static constexpr auto BranchNameTest = "test";
static constexpr auto BranchNameExperimental = "experimental";
static constexpr auto BranchNameStable = "stable";
inline const char* branchName(Branch branch) {
switch (branch) {
......@@ -60,6 +60,7 @@ struct Version {
: major(major), minor(minor), revision(revision), build(build) {}
Version(const std::string& s) { _fromString(s); }
Version& fromString(const std::string& s) { _fromString(s); return *this; }
Version& operator=(const std::string& s) { _fromString(s); return *this; }
bool valid() const { return !(major == 0 && minor == 0 && revision == 0 && build == 0); }
void reset() { major = minor = revision = build = 0; }
......
......@@ -12,6 +12,9 @@
#include <string>
#include <functional>
#include <assert.h>
#include <vector>
#include <unordered_map>
#include "../util/str_util.h"
#pragma comment (lib, "comsuppw.lib")
#pragma comment (lib, "wbemuuid.lib")
......@@ -43,6 +46,14 @@ namespace wmi
typedef std::function<void(HRESULT, const std::wstring&)> ErrorFunc;
typedef std::function<void(const std::wstring&, const std::wstring&)> OutputFunc;
//struct ResultItem {
// std::wstring key = {};
// std::wstring value = {};
//};
typedef std::unordered_map<std::wstring, std::wstring> ResultItem;
typedef std::vector<ResultItem> Result;
class WmiBase
{
public:
......@@ -59,6 +70,45 @@ public:
CoUninitialize();
}
/*
* @brief 快速查询
* @note 默认namespace 为 root\\CIMV2
* @param keys 要查询的键名,例如 Caption, Desc, ..., 必须有序,否则结果的key:value无法对应
* @param provider 例如 Win32_Process
* @param[in|out] result 结果
* @return 成功或失败
* @note 例:keys=["captian, key"], provider="Win32_Process", 则result结构应为:[ {caption: value, key:value}, {caption: value, key:value}, ...]
*/
static bool simpleSelect(const std::vector<std::wstring>& keys, const std::wstring& provider, Result& result) {
std::vector<std::wstring> values, errors;
auto output = [&values](const std::wstring& key, const std::wstring& value) {
values.push_back(value);
};
auto error = [&errors](HRESULT hr, const std::wstring& msg) {
errors.push_back(msg);
};
wmi::WmiBase wmi(L"ROOT\\CIMV2", output, error);
if (!wmi.prepare()) {
return false;
}
if (!wmi.execute(std::wstring(L"select ") + join(keys, std::wstring(L",")) + L" from " + provider) || !errors.empty()) {
return false;
}
for (size_t i = 0; i < values.size(); i += keys.size()) {
ResultItem item;
for (size_t j = 0; j < keys.size(); j++) {
item.insert({ keys.at(j % keys.size()), values.at(i + j) });
}
result.emplace_back(item);
}
return true;
}
bool prepare() {
do {
HRESULT hr = E_FAIL;
......
......@@ -21,15 +21,24 @@ int main()
wmi.execute(L"SELECT * FROM Win32_Processor");
}*/
Result result;
WmiBase::simpleSelect({ L"AdapterRAM", L"Description" }, L"Win32_VideoController", result);
for (const auto& i : result) {
for (const auto& j : i) {
printf("%ls %ls\n", j.first.data(), j.second.data());
}
}
{
WmiBase wmi(L"root\\CIMV2", out, err);
wmi.prepare();
//WmiBase wmi(L"root\\CIMV2", out, err);
//wmi.prepare();
//wmi.execute(L"SELECT SerialNumber FROM Win32_DiskDrive WHERE Index = 4");
//wmi.execute(L"SELECT * FROM Win32_DiskPartition");
//wmi.execute(L"SELECT Caption FROM Win32_BootConfiguration");
//wmi.execute(L"Select Name from Win32_OperatingSystem");
wmi.execute(L"SELECT * FROM Win32_VideoController");
//wmi.execute(L"SELECT Description,AdapterRAM FROM Win32_VideoController");
//wmi.execute(L"SELECT * FROM Win32_VideoController");
//wmi.execute(L"SELECT PNPDeviceID FROM Win32_LogicalDisk WHERE NAME = 'C:'");
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment