Skip to content

Commit

Permalink
Change wmi.cpp, use ComPtr class
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamer-dead committed May 7, 2023
1 parent fdc6278 commit a74d7f0
Showing 1 changed file with 10 additions and 26 deletions.
36 changes: 10 additions & 26 deletions osquery/core/windows/wmi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ Status WmiResultItem::GetDateTime(const std::string& name,
return Status::failure("Expected VT_BSTR, got something else.");
}

ISWbemDateTime* dt = nullptr;
ComPtr<ISWbemDateTime> dt;
hr = CoCreateInstance(
CLSID_SWbemDateTime, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&dt));
CLSID_SWbemDateTime, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(dt.GetAddressOf()));
if (!SUCCEEDED(hr)) {
VariantClear(&value);
return Status::failure("Failed to create SWbemDateTime object.");
Expand All @@ -139,14 +139,12 @@ Status WmiResultItem::GetDateTime(const std::string& name,
VariantClear(&value);

if (!SUCCEEDED(hr)) {
dt->Release();
return Status::failure("Failed to set SWbemDateTime value.");
}

BSTR filetime_str = {0};
hr = dt->GetFileTime(is_local ? VARIANT_TRUE : VARIANT_FALSE, &filetime_str);
if (!SUCCEEDED(hr)) {
dt->Release();
return Status::failure("GetFileTime failed.");
}

Expand All @@ -157,7 +155,6 @@ Status WmiResultItem::GetDateTime(const std::string& name,
ft.dwHighDateTime = ui.HighPart;

SysFreeString(filetime_str);
dt->Release();

return Status::success();
}
Expand Down Expand Up @@ -509,47 +506,35 @@ Status WmiRequest::ExecMethod(const WmiResultItem& object,
const WmiMethodArgs& args,
WmiResultItem& out_result) const {
std::wstring property_name = stringToWstring(method);

IWbemClassObject* raw = nullptr;

std::unique_ptr<IWbemClassObject, impl::WmiObjectDeleter> in_def{nullptr};
std::unique_ptr<IWbemClassObject, impl::WmiObjectDeleter> class_obj{nullptr};

BSTR wmi_class_name = WbemClassObjectPropToBSTR(object, "__CLASS");
if (wmi_class_name == nullptr) {
return Status::failure("Class name out of memory");
}

ComPtr<IWbemClassObject> class_obj;
// GetObject obtains a CIM Class definition object
HRESULT hr = services_->GetObject(wmi_class_name, 0, nullptr, &raw, nullptr);
HRESULT hr = services_->GetObject(wmi_class_name, 0, nullptr, class_obj.GetAddressOf(), nullptr);
SysFreeString(wmi_class_name);

if (FAILED(hr)) {
return Status::failure("Failed to GetObject");
}

class_obj.reset(raw);
raw = nullptr;

ComPtr<IWbemClassObject> in_def;
// GetMethod only works on CIM class definition objects. This is why
// we don't use result_
hr = class_obj->GetMethod(property_name.c_str(), 0, &raw, nullptr);
hr = class_obj->GetMethod(property_name.c_str(), 0, in_def.GetAddressOf(), nullptr);
if (FAILED(hr)) {
return Status::failure("Failed to GetMethod");
}

in_def.reset(raw);
raw = nullptr;

std::unique_ptr<IWbemClassObject, impl::WmiObjectDeleter> args_inst{nullptr};

ComPtr<IWbemClassObject> args_inst;
// in_def can be nullptr if the chosen method has no in-parameters
if (in_def != nullptr) {
hr = in_def->SpawnInstance(0, &raw);
if (in_def) {
hr = in_def->SpawnInstance(0, args_inst.GetAddressOf());
if (FAILED(hr)) {
return Status::failure("Failed to SpawnInstance");
}
args_inst.reset(raw);

// Build up the WMI method call arguments
for (const auto& p : args.GetArguments()) {
Expand All @@ -567,8 +552,6 @@ Status WmiRequest::ExecMethod(const WmiResultItem& object,

// In order to execute a WMI method, we need to know the specific object name
// and method name
IWbemClassObject* out_params = nullptr;

auto wmi_meth_name = SysAllocString(property_name.c_str());
if (wmi_meth_name == nullptr) {
return Status::failure("Out of memory");
Expand All @@ -580,6 +563,7 @@ Status WmiRequest::ExecMethod(const WmiResultItem& object,
return Status::failure("Out of memory");
}

IWbemClassObject* out_params = nullptr;
// Execute the WMI method, the return value and out-params all exist in
// out_params
hr = services_->ExecMethod(wmi_obj_path,
Expand Down

0 comments on commit a74d7f0

Please sign in to comment.