Skip to content

Commit

Permalink
Use Microsoft::WRL::ComPtr (#16744)
Browse files Browse the repository at this point in the history
  • Loading branch information
oltolm authored Feb 23, 2025
1 parent 796a237 commit b08273b
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 58 deletions.
8 changes: 4 additions & 4 deletions rpcs3/Emu/Audio/XAudio2/XAudio2Backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ XAudio2Backend::XAudio2Backend()
m_com_init_success = true;
}

if (HRESULT hr = XAudio2Create(instance.GetAddressOf(), 0, XAUDIO2_USE_DEFAULT_PROCESSOR); FAILED(hr))
if (HRESULT hr = XAudio2Create(&instance, 0, XAUDIO2_USE_DEFAULT_PROCESSOR); FAILED(hr))
{
XAudio.error("XAudio2Create() failed: %s (0x%08x)", std::system_category().message(hr), static_cast<u32>(hr));
return;
Expand All @@ -78,7 +78,7 @@ XAudio2Backend::XAudio2Backend()
}

// Try to register a listener for device changes
if (HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(enumerator.GetAddressOf())); FAILED(hr))
if (HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&enumerator)); FAILED(hr))
{
XAudio.error("CoCreateInstance() failed: %s (0x%08x)", std::system_category().message(hr), static_cast<u32>(hr));
return;
Expand Down Expand Up @@ -215,7 +215,7 @@ bool XAudio2Backend::Open(std::string_view dev_id, AudioFreq freq, AudioSampleSi
if (use_default_device)
{
Microsoft::WRL::ComPtr<IMMDevice> default_dev{};
if (HRESULT hr = m_device_enumerator->GetDefaultAudioEndpoint(eRender, eConsole, default_dev.GetAddressOf()); FAILED(hr))
if (HRESULT hr = m_device_enumerator->GetDefaultAudioEndpoint(eRender, eConsole, &default_dev); FAILED(hr))
{
XAudio.error("GetDefaultAudioEndpoint() failed: %s (0x%08x)", std::system_category().message(hr), static_cast<u32>(hr));
return false;
Expand Down Expand Up @@ -319,7 +319,7 @@ f64 XAudio2Backend::GetCallbackFrameLen()
Microsoft::WRL::ComPtr<IXAudio2Extension> xaudio_ext{};
f64 min_latency{};

if (HRESULT hr = m_xaudio2_instance->QueryInterface(IID_IXAudio2Extension, std::bit_cast<void**>(xaudio_ext.GetAddressOf())); FAILED(hr))
if (HRESULT hr = m_xaudio2_instance.As(&xaudio_ext); FAILED(hr))
{
XAudio.error("QueryInterface() failed: %s (0x%08x)", std::system_category().message(hr), static_cast<u32>(hr));
}
Expand Down
6 changes: 3 additions & 3 deletions rpcs3/Emu/Audio/XAudio2/xaudio2_enumerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ xaudio2_enumerator::~xaudio2_enumerator()
std::vector<audio_device_enumerator::audio_device> xaudio2_enumerator::get_output_devices()
{
Microsoft::WRL::ComPtr<IMMDeviceEnumerator> devEnum{};
if (HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(devEnum.GetAddressOf())); FAILED(hr))
if (HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&devEnum)); FAILED(hr))
{
xaudio_dev_enum.error("CoCreateInstance() failed: %s (0x%08x)", std::system_category().message(hr), static_cast<u32>(hr));
return {};
Expand Down Expand Up @@ -57,7 +57,7 @@ std::vector<audio_device_enumerator::audio_device> xaudio2_enumerator::get_outpu
for (UINT dev_idx = 0; dev_idx < count; dev_idx++)
{
Microsoft::WRL::ComPtr<IMMDevice> endpoint{};
if (HRESULT hr = devices->Item(dev_idx, endpoint.GetAddressOf()); FAILED(hr))
if (HRESULT hr = devices->Item(dev_idx, &endpoint); FAILED(hr))
{
xaudio_dev_enum.error("devices->Item() failed: %s (0x%08x)", std::system_category().message(hr), static_cast<u32>(hr));
continue;
Expand All @@ -83,7 +83,7 @@ std::vector<audio_device_enumerator::audio_device> xaudio2_enumerator::get_outpu
CoTaskMemFree(id);

Microsoft::WRL::ComPtr<IPropertyStore> props{};
if (HRESULT hr = endpoint->OpenPropertyStore(STGM_READ, props.GetAddressOf()); FAILED(hr))
if (HRESULT hr = endpoint->OpenPropertyStore(STGM_READ, &props); FAILED(hr))
{
xaudio_dev_enum.error("endpoint->OpenPropertyStore() failed: %s (0x%08x)", std::system_category().message(hr), static_cast<u32>(hr));
continue;
Expand Down
5 changes: 0 additions & 5 deletions rpcs3/Emu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -627,11 +627,6 @@ if(TARGET 3rdparty_vulkan)
RSX/VK/VKVertexProgram.cpp
RSX/VK/VKTextureCache.cpp
)
if(MSVC)
set_source_files_properties(RSX/VK/vkutils/shared.cpp PROPERTIES
COMPILE_FLAGS /EHs-
SKIP_PRECOMPILE_HEADERS ON)
endif()
endif()

target_link_libraries(rpcs3_emu
Expand Down
25 changes: 8 additions & 17 deletions rpcs3/Input/xinput_pad_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "stdafx.h"
#include "xinput_pad_handler.h"
#include "Emu/Io/pad_config.h"
#include "util/dyn_lib.hpp"

namespace XINPUT_INFO
{
Expand Down Expand Up @@ -77,8 +78,6 @@ xinput_pad_handler::~xinput_pad_handler()
{
if (library)
{
FreeLibrary(library);
library = nullptr;
xinputGetExtended = nullptr;
xinputGetCustomData = nullptr;
xinputGetState = nullptr;
Expand Down Expand Up @@ -374,39 +373,31 @@ pad_preview_values xinput_pad_handler::get_preview_values(const std::unordered_m
};
}

template<class T>
T getProc(HMODULE hModule, LPCSTR lpProcName)
{
return reinterpret_cast<T>(GetProcAddress(hModule, lpProcName));
}

bool xinput_pad_handler::Init()
{
if (m_is_init)
return true;

for (auto it : XINPUT_INFO::LIBRARY_FILENAMES)
{
library = LoadLibrary(it);
library.load(it);
if (library)
{
xinputGetExtended = getProc<PFN_XINPUTGETEXTENDED>(library, "XInputGetExtended"); // Optional
xinputGetCustomData = getProc<PFN_XINPUTGETCUSTOMDATA>(library, "XInputGetCustomData"); // Optional
xinputGetState = getProc<PFN_XINPUTGETSTATE>(library, reinterpret_cast<LPCSTR>(100));
xinputGetExtended = library.get<PFN_XINPUTGETEXTENDED>("XInputGetExtended"); // Optional
xinputGetCustomData = library.get<PFN_XINPUTGETCUSTOMDATA>("XInputGetCustomData"); // Optional
xinputGetState = library.get<PFN_XINPUTGETSTATE>(reinterpret_cast<LPCSTR>(100));
if (!xinputGetState)
xinputGetState = getProc<PFN_XINPUTGETSTATE>(library, "XInputGetState");
xinputGetState = library.get<PFN_XINPUTGETSTATE>("XInputGetState");

xinputSetState = getProc<PFN_XINPUTSETSTATE>(library, "XInputSetState");
xinputGetBatteryInformation = getProc<PFN_XINPUTGETBATTERYINFORMATION>(library, "XInputGetBatteryInformation");
xinputSetState = library.get<PFN_XINPUTSETSTATE>("XInputSetState");
xinputGetBatteryInformation = library.get<PFN_XINPUTGETBATTERYINFORMATION>("XInputGetBatteryInformation");

if (xinputGetState && xinputSetState && xinputGetBatteryInformation)
{
m_is_init = true;
break;
}

FreeLibrary(library);
library = nullptr;
xinputGetExtended = nullptr;
xinputGetCustomData = nullptr;
xinputGetState = nullptr;
Expand Down
14 changes: 7 additions & 7 deletions rpcs3/Input/xinput_pad_handler.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "Emu/Io/PadHandler.h"
#include "util/dyn_lib.hpp"

#include <unordered_map>

Expand All @@ -9,7 +10,6 @@
#endif
#include <Windows.h>
#include <Xinput.h>
#include <chrono>

// ScpToolkit defined structure for pressure sensitive button query
struct SCP_EXTN
Expand Down Expand Up @@ -116,22 +116,22 @@ class xinput_pad_handler final : public PadHandlerBase
void init_config(cfg_pad* cfg) override;

private:
typedef DWORD (WINAPI * PFN_XINPUTGETEXTENDED)(DWORD, SCP_EXTN *);
typedef DWORD (WINAPI * PFN_XINPUTGETCUSTOMDATA)(DWORD, DWORD, void *);
typedef DWORD (WINAPI * PFN_XINPUTGETSTATE)(DWORD, XINPUT_STATE *);
typedef DWORD (WINAPI * PFN_XINPUTSETSTATE)(DWORD, XINPUT_VIBRATION *);
typedef DWORD (WINAPI * PFN_XINPUTGETBATTERYINFORMATION)(DWORD, BYTE, XINPUT_BATTERY_INFORMATION *);
using PFN_XINPUTGETEXTENDED = DWORD(WINAPI*)(DWORD, SCP_EXTN*);
using PFN_XINPUTGETCUSTOMDATA = DWORD(WINAPI*)(DWORD, DWORD, void*);
using PFN_XINPUTGETSTATE = DWORD(WINAPI*)(DWORD, XINPUT_STATE*);
using PFN_XINPUTSETSTATE = DWORD(WINAPI*)(DWORD, XINPUT_VIBRATION*);
using PFN_XINPUTGETBATTERYINFORMATION = DWORD(WINAPI*)(DWORD, BYTE, XINPUT_BATTERY_INFORMATION*);

int GetDeviceNumber(const std::string& padId);
static PadButtonValues get_button_values_base(const XINPUT_STATE& state, trigger_recognition_mode trigger_mode);
static PadButtonValues get_button_values_scp(const SCP_EXTN& state, trigger_recognition_mode trigger_mode);

HMODULE library{ nullptr };
PFN_XINPUTGETEXTENDED xinputGetExtended{ nullptr };
PFN_XINPUTGETCUSTOMDATA xinputGetCustomData{ nullptr };
PFN_XINPUTGETSTATE xinputGetState{ nullptr };
PFN_XINPUTSETSTATE xinputSetState{ nullptr };
PFN_XINPUTGETBATTERYINFORMATION xinputGetBatteryInformation{ nullptr };
utils::dynamic_library library;

std::shared_ptr<PadDevice> get_device(const std::string& device) override;
bool get_is_left_trigger(const std::shared_ptr<PadDevice>& device, u64 keyCode) override;
Expand Down
11 changes: 5 additions & 6 deletions rpcs3/rpcs3qt/shortcut_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <comdef.h>

#include "Emu/system_utils.hpp"
#include <wrl/client.h>
#else
#include <sys/stat.h>
#include <errno.h>
Expand Down Expand Up @@ -142,19 +143,17 @@ namespace gui::utils
return false;
}

IShellLink* pShellLink = nullptr;
IPersistFile* pPersistFile = nullptr;
Microsoft::WRL::ComPtr<IShellLink> pShellLink;
Microsoft::WRL::ComPtr<IPersistFile> pPersistFile;

const auto cleanup = [&](bool return_value, const std::string& fail_reason) -> bool
{
if (!return_value) sys_log.error("Failed to create shortcut: %s", fail_reason);
if (pPersistFile) pPersistFile->Release();
if (pShellLink) pShellLink->Release();
CoUninitialize();
return return_value;
};

res = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pShellLink));
res = CoCreateInstance(__uuidof(ShellLink), NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pShellLink));
if (FAILED(res))
return cleanup(false, "CoCreateInstance failed");

Expand Down Expand Up @@ -200,7 +199,7 @@ namespace gui::utils
}

// Use the IPersistFile object to save the shell link
res = pShellLink->QueryInterface(IID_PPV_ARGS(&pPersistFile));
res = pShellLink.As(&pPersistFile);
if (FAILED(res))
return cleanup(false, fmt::format("QueryInterface failed (%s)", str_error(res)));

Expand Down
14 changes: 11 additions & 3 deletions rpcs3/util/dyn_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ namespace utils
return loaded();
}

#ifdef _WIN32
bool dynamic_library::load(const std::wstring& path)
{
m_handle = LoadLibraryW(path.c_str());
return loaded();
}
#endif

void dynamic_library::close()
{
#ifdef _WIN32
Expand All @@ -39,12 +47,12 @@ namespace utils
m_handle = nullptr;
}

void* dynamic_library::get_impl(const std::string& name) const
void* dynamic_library::get_impl(const char* name) const
{
#ifdef _WIN32
return reinterpret_cast<void*>(GetProcAddress(reinterpret_cast<HMODULE>(m_handle), name.c_str()));
return reinterpret_cast<void*>(GetProcAddress(reinterpret_cast<HMODULE>(m_handle), name));
#else
return dlsym(m_handle, name.c_str());
return dlsym(m_handle, name);
#endif
}

Expand Down
19 changes: 6 additions & 13 deletions rpcs3/util/dyn_lib.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,19 @@ namespace utils
~dynamic_library();

bool load(const std::string& path);
#ifdef _WIN32
bool load(const std::wstring& path);
#endif
void close();

private:
void* get_impl(const std::string& name) const;
void* get_impl(const char* name) const;

public:
template <typename Type = void>
Type* get(const std::string& name) const
Type get(const char* name) const
{
Type* result;
*reinterpret_cast<void**>(&result) = get_impl(name);
return result;
}

template <typename Type>
bool get(Type*& function, const std::string& name) const
{
*reinterpret_cast<void**>(&function) = get_impl(name);

return function != nullptr;
return reinterpret_cast<Type>(get_impl(name));
}

bool loaded() const;
Expand Down

0 comments on commit b08273b

Please sign in to comment.