Skip to content

Commit

Permalink
Use new function Bstr::fromString
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamer-dead committed Apr 25, 2023
1 parent d138f59 commit dbd6e5b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
5 changes: 5 additions & 0 deletions osquery/core/windows/bstr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ namespace {
static_assert(sizeof(Bstr) == sizeof(BSTR), "BstrSize");
}

// static
Bstr Bstr::fromString(std::wstring_view s) {
return Bstr(SysAllocString(s.c_str()));
}

Bstr::~Bstr() {
// SysFreeString handles nullptr gracefully.
::SysFreeString(bstr_);
Expand Down
6 changes: 5 additions & 1 deletion osquery/core/windows/bstr.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include <osquery/utils/system/system.h>

#include <string>

#include <wtypes.h>

#include <osquery/utils/only_movable.h>
Expand All @@ -17,7 +19,9 @@ namespace osquery {

class Bstr : private only_movable {
public:
Bstr() = default;
static Bstr fromString()

Bstr() = default;
~Bstr();

explicit Bstr(BSTR bs);
Expand Down
18 changes: 9 additions & 9 deletions osquery/core/windows/wmi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,8 @@ Expected<WmiRequest, WmiError> WmiRequest::CreateWmiRequest(
wmi_request.locator_.reset(locator);

IWbemServices* services = nullptr;
Bstr nspace_str(SysAllocString(nspace.c_str()));
if (nullptr == nspace_str.get()) {
Bstr nspace_str = Bstr::fromString(nspace);
if (!nspace_str) {
return createError(WmiError::ConstructionError)
<< "WmiRequest creation failed in nspace_str allocation";
}
Expand Down Expand Up @@ -455,14 +455,14 @@ Expected<WmiRequest, WmiError> WmiRequest::CreateWmiRequest(

IEnumWbemClassObject* wbem_enum = nullptr;

Bstr language_str(SysAllocString(L"WQL"));
if (nullptr == language_str.get()) {
Bstr language_str = Bstr::fromString(L"WQL");
if (!language_str) {
return createError(WmiError::ConstructionError)
<< "WmiRequest creation failed in language_str allocation";
}

Bstr wql_str(SysAllocString(wql.c_str()));
if (nullptr == wql_str.get()) {
Bstr wql_str = Bstr::fromString(wql);
if (!wql_str) {
return createError(WmiError::ConstructionError)
<< "WmiRequest creation failed in wql_str allocation";
}
Expand Down Expand Up @@ -559,7 +559,7 @@ Status WmiRequest::ExecMethod(const WmiResultItem& object,
// and method name
IWbemClassObject* out_params = nullptr;

Bstr wmi_meth_name(SysAllocString(property_name.c_str()));
Bstr wmi_meth_name = Bstr::fromString(property_name);
if (!wmi_meth_name) {
return Status::failure("Out of memory");
}
Expand All @@ -571,8 +571,8 @@ Status WmiRequest::ExecMethod(const WmiResultItem& object,

// Execute the WMI method, the return value and out-params all exist in
// out_params
hr = services_->ExecMethod(wmi_obj_path,
wmi_meth_name,
hr = services_->ExecMethod(wmi_obj_path.get(),
wmi_meth_name.get(),
0,
nullptr,
args_inst.get(),
Expand Down

0 comments on commit dbd6e5b

Please sign in to comment.