Skip to content

Commit

Permalink
More memory voodoo
Browse files Browse the repository at this point in the history
  • Loading branch information
nefarius committed Aug 7, 2024
1 parent e7c7042 commit f559183
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
18 changes: 7 additions & 11 deletions src/Devcon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
#include "LibraryHelper.hpp"

//
// Hooking
// Packages
//
#include <detours/detours.h>

#include <scope_guard.hpp>
#include <wil/resource.h>

#include "MultiStringArray.hpp"

Expand Down Expand Up @@ -107,7 +107,7 @@ inline std::vector<wchar_t> BuildMultiString(const std::vector<std::wstring>& da
return multiString;
}

static std::expected<PBYTE, Win32Error> GetDeviceRegistryProperty(
static std::expected<wil::unique_hlocal_ptr<PBYTE>, Win32Error> GetDeviceRegistryProperty(
_In_ HDEVINFO DeviceInfoSet,
_In_ PSP_DEVINFO_DATA DeviceInfoData,
_In_ DWORD Property,
Expand Down Expand Up @@ -163,7 +163,7 @@ static std::expected<PBYTE, Win32Error> GetDeviceRegistryProperty(
if (BufferSize)
*BufferSize = sizeRequired;

return buffer;
return wil::make_unique_hlocal<PBYTE>(buffer);
}

std::expected<void, Win32Error> devcon::create(const std::wstring& className, const GUID* classGuid,
Expand Down Expand Up @@ -310,7 +310,7 @@ std::expected<void, Win32Error> devcon::restart_bth_usb_device(int instance)
return std::unexpected(enumeratorProperty.error());
}

const LPTSTR buffer = (LPTSTR)enumeratorProperty.value();
const LPTSTR buffer = (LPTSTR)enumeratorProperty.value().get();

// find device with enumerator name "USB"
for (LPTSTR p = buffer; p && *p && (p < &buffer[bufferSize]); p += lstrlen(p) + sizeof(TCHAR))
Expand All @@ -322,8 +322,6 @@ std::expected<void, Win32Error> devcon::restart_bth_usb_device(int instance)
}
}

LocalFree(buffer);

// if device found restart
if (found)
{
Expand Down Expand Up @@ -382,7 +380,7 @@ std::expected<void, nefarius::util::Win32Error> devcon::enable_disable_bth_usb_d
return std::unexpected(enumeratorProperty.error());
}

const LPTSTR buffer = (LPTSTR)enumeratorProperty.value();
const LPTSTR buffer = (LPTSTR)enumeratorProperty.value().get();

// find device with enumerator name "USB"
for (LPTSTR p = buffer; p && *p && (p < &buffer[bufferSize]); p += lstrlen(p) + sizeof(TCHAR))
Expand All @@ -394,8 +392,6 @@ std::expected<void, nefarius::util::Win32Error> devcon::enable_disable_bth_usb_d
}
}

LocalFree(buffer);

// if device found change it's state
if (found)
{
Expand Down Expand Up @@ -914,7 +910,7 @@ std::vector<std::expected<void, Win32Error>> devcon::uninstall_device_and_driver
continue;
}

LPWSTR buffer = (LPWSTR)hwIdBuffer.value();
LPWSTR buffer = (LPWSTR)hwIdBuffer.value().get();

//
// find device matching hardware ID
Expand Down
3 changes: 2 additions & 1 deletion vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"argh",
"easyloggingpp",
"detours",
"scope-guard"
"scope-guard",
"wil"
]
}

0 comments on commit f559183

Please sign in to comment.