Refactor to use Microsoft::WRL::ComPtr (#1599)
Some checks failed
Generate translation template / generate-pot (push) Failing after 2m59s
Build check / build (push) Has been cancelled

This commit is contained in:
oltolm 2025-06-16 23:25:06 +02:00 committed by GitHub
parent da98aa4176
commit 2f02fda9ea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 56 additions and 119 deletions

View file

@ -385,8 +385,6 @@ template <typename T1, typename T2>
constexpr bool HAS_FLAG(T1 flags, T2 test_flag) { return (flags & (T1)test_flag) == (T1)test_flag; }
template <typename T1, typename T2>
constexpr bool HAS_BIT(T1 value, T2 index) { return (value & ((T1)1 << index)) != 0; }
template <typename T>
constexpr void SAFE_RELEASE(T& p) { if (p) { p->Release(); p = nullptr; } }
template <typename T>
constexpr uint32_t ppcsizeof() { return (uint32_t) sizeof(T); }

View file

@ -1,9 +1,8 @@
#include "DirectSoundAPI.h"
#include "gui/wxgui.h"
#include "util/helpers/helpers.h"
#include "gui/guiWrapper.h"
#include <wrl/client.h>
#pragma comment(lib, "Dsound.lib")
@ -15,12 +14,9 @@ std::wstring DirectSoundAPI::DirectSoundDeviceDescription::GetIdentifier() const
DirectSoundAPI::DirectSoundAPI(GUID* guid, sint32 samplerate, sint32 channels, sint32 samples_per_block, sint32 bits_per_sample)
: IAudioAPI(samplerate, channels, samples_per_block, bits_per_sample)
{
LPDIRECTSOUND8 direct_sound;
if (DirectSoundCreate8(guid, &direct_sound, nullptr) != DS_OK)
if (DirectSoundCreate8(guid, &m_direct_sound, nullptr) != DS_OK)
throw std::runtime_error("can't create directsound device");
m_direct_sound = decltype(m_direct_sound)(direct_sound);
if (FAILED(m_direct_sound->SetCooperativeLevel(gui_getWindowInfo().window_main.hwnd, DSSCL_PRIORITY)))
throw std::runtime_error("can't set directsound priority");
@ -30,7 +26,7 @@ DirectSoundAPI::DirectSoundAPI(GUID* guid, sint32 samplerate, sint32 channels, s
bd.dwBufferBytes = kBufferCount * m_bytesPerBlock; // kBlockCount * (samples_per_block * channels * (bits_per_sample / 8));
bd.lpwfxFormat = (LPWAVEFORMATEX)&m_wfx;
LPDIRECTSOUNDBUFFER sound_buffer;
Microsoft::WRL::ComPtr<IDirectSoundBuffer> sound_buffer;
if (FAILED(m_direct_sound->CreateSoundBuffer(&bd, &sound_buffer, nullptr)))
throw std::runtime_error("can't create directsound soundbuffer");
@ -41,27 +37,17 @@ DirectSoundAPI::DirectSoundAPI(GUID* guid, sint32 samplerate, sint32 channels, s
m_sound_buffer_size = caps.dwBufferBytes;
LPDIRECTSOUNDBUFFER8 sound_buffer8;
LPDIRECTSOUNDNOTIFY8 notify8;
sound_buffer->QueryInterface(IID_IDirectSoundBuffer8, (void**)&sound_buffer8);
Microsoft::WRL::ComPtr<IDirectSoundNotify8> notify8;
if (!sound_buffer8)
if (FAILED(sound_buffer->QueryInterface(IID_IDirectSoundBuffer8, &m_sound_buffer)))
{
sound_buffer->Release();
throw std::runtime_error("can't get directsound buffer interface");
}
m_sound_buffer = decltype(m_sound_buffer)(sound_buffer8);
sound_buffer->QueryInterface(IID_IDirectSoundNotify8, (void**)&notify8);
if (!notify8)
if (FAILED(sound_buffer->QueryInterface(IID_IDirectSoundNotify8, &m_notify)))
{
sound_buffer->Release();
throw std::runtime_error("can't get directsound notify interface");
}
m_notify = decltype(m_notify)(notify8);
sound_buffer->Release();
{ // initialize sound buffer
void *ptr1, *ptr2;
@ -155,10 +141,6 @@ DirectSoundAPI::~DirectSoundAPI()
if(m_thread.joinable())
m_thread.join();
m_notify.reset();
m_sound_buffer.reset();
m_direct_sound.reset();
for(auto entry : m_notify_event)
{
if (entry)
@ -186,7 +168,7 @@ bool DirectSoundAPI::Stop()
bool DirectSoundAPI::FeedBlock(sint16* data)
{
std::unique_lock lock(m_mutex);
std::lock_guard lock(m_mutex);
if (m_buffer.size() > kBlockCount)
{
cemuLog_logDebug(LogType::Force, "dropped direct sound block since too many buffers are queued");

View file

@ -2,8 +2,8 @@
#define DIRECTSOUND_VERSION 0x0800
#include <mmsystem.h>
//#include <mmreg.h>
#include <dsound.h>
#include <wrl/client.h>
#include "IAudioAPI.h"
@ -41,15 +41,10 @@ public:
static std::vector<DeviceDescriptionPtr> GetInputDevices();
private:
struct DirectSoundDeleter
{
void operator()(IUnknown* ptr) const { if (ptr) ptr->Release(); }
};
std::unique_ptr<IDirectSound8, DirectSoundDeleter> m_direct_sound;
//std::unique_ptr<IDirectSoundCapture8, DirectSoundDeleter> m_direct_sound_capture;
std::unique_ptr<IDirectSoundBuffer8, DirectSoundDeleter> m_sound_buffer;
std::unique_ptr<IDirectSoundNotify8, DirectSoundDeleter> m_notify;
Microsoft::WRL::ComPtr<IDirectSound8> m_direct_sound;
//Microsoft::WRL::ComPtr<IDirectSoundCapture8> m_direct_sound_capture;
Microsoft::WRL::ComPtr<IDirectSoundBuffer8> m_sound_buffer;
Microsoft::WRL::ComPtr<IDirectSoundNotify8> m_notify;
DWORD m_sound_buffer_size = 0;
uint32_t m_offset = 0;

View file

@ -2,6 +2,7 @@
//#if (_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/)
#include <wrl/client.h>
#include <xaudio2.h>
#ifndef XAUDIO2_DLL
@ -33,17 +34,15 @@ XAudio2API::XAudio2API(std::wstring device_id, uint32 samplerate, uint32 channel
throw std::runtime_error("can't find XAudio2Create import");
HRESULT hres;
IXAudio2* xaudio;
if (FAILED((hres = _XAudio2Create(&xaudio, 0, XAUDIO2_DEFAULT_PROCESSOR))))
if (FAILED((hres = _XAudio2Create(&m_xaudio, 0, XAUDIO2_DEFAULT_PROCESSOR))))
throw std::runtime_error(fmt::format("can't create xaudio device (hres: {:#x})", hres));
m_xaudio = decltype(m_xaudio)(xaudio);
IXAudio2MasteringVoice* mastering_voice;
if (FAILED((hres = m_xaudio->CreateMasteringVoice(&mastering_voice, channels, samplerate, 0, m_device_id.empty() ? nullptr : m_device_id.c_str()))))
throw std::runtime_error(fmt::format("can't create xaudio mastering voice (hres: {:#x})", hres));
m_mastering_voice = decltype(m_mastering_voice)(mastering_voice);
m_mastering_voice.reset(mastering_voice);
m_wfx.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
m_wfx.Format.nChannels = channels;
@ -88,12 +87,6 @@ XAudio2API::XAudio2API(std::wstring device_id, uint32 samplerate, uint32 channel
m_xaudio->StartEngine();
}
void XAudio2API::XAudioDeleter::operator()(IXAudio2* ptr) const
{
if (ptr)
ptr->Release();
}
void XAudio2API::VoiceDeleter::operator()(IXAudio2Voice* ptr) const
{
if (ptr)
@ -106,10 +99,6 @@ XAudio2API::~XAudio2API()
m_xaudio->StopEngine();
XAudio2API::Stop();
m_source_voice.reset();
m_mastering_voice.reset();
m_xaudio.reset();
}
void XAudio2API::SetVolume(sint32 volume)
@ -179,10 +168,10 @@ const std::vector<XAudio2API::DeviceDescriptionPtr>& XAudio2API::RefreshDevices(
try
{
struct IWbemLocator *wbem_locator = nullptr;
Microsoft::WRL::ComPtr<IWbemLocator> wbem_locator;
HRESULT hres = CoCreateInstance(__uuidof(WbemLocator), nullptr, CLSCTX_INPROC_SERVER, __uuidof(IWbemLocator), (LPVOID*)&wbem_locator);
if (FAILED(hres) || !wbem_locator)
HRESULT hres = CoCreateInstance(__uuidof(WbemLocator), nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&wbem_locator));
if (FAILED(hres))
throw std::system_error(hres, std::system_category());
std::shared_ptr<OLECHAR> path(SysAllocString(LR"(\\.\root\cimv2)"), SysFreeString);
@ -191,20 +180,19 @@ const std::vector<XAudio2API::DeviceDescriptionPtr>& XAudio2API::RefreshDevices(
std::shared_ptr<OLECHAR> name_row(SysAllocString(L"Name"), SysFreeString);
std::shared_ptr<OLECHAR> device_id_row(SysAllocString(L"DeviceID"), SysFreeString);
IWbemServices *wbem_services = nullptr;
Microsoft::WRL::ComPtr<IWbemServices> wbem_services;
hres = wbem_locator->ConnectServer(path.get(), nullptr, nullptr, nullptr, 0, nullptr, nullptr, &wbem_services);
wbem_locator->Release(); // Free memory resources.
if (FAILED(hres) || !wbem_services)
throw std::system_error(hres, std::system_category());
hres = CoSetProxyBlanket(wbem_services, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, nullptr, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, nullptr, EOAC_NONE);
if (FAILED(hres))
throw std::system_error(hres, std::system_category());
IEnumWbemClassObject* wbem_enum = nullptr;
hres = CoSetProxyBlanket(wbem_services.Get(), RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, nullptr, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, nullptr, EOAC_NONE);
if (FAILED(hres))
throw std::system_error(hres, std::system_category());
Microsoft::WRL::ComPtr<IEnumWbemClassObject> wbem_enum;
hres = wbem_services->ExecQuery(language.get(), query.get(), WBEM_FLAG_RETURN_WBEM_COMPLETE | WBEM_FLAG_FORWARD_ONLY, nullptr, &wbem_enum);
if (FAILED(hres) || !wbem_enum)
if (FAILED(hres))
throw std::system_error(hres, std::system_category());
ULONG returned;
@ -250,11 +238,6 @@ const std::vector<XAudio2API::DeviceDescriptionPtr>& XAudio2API::RefreshDevices(
auto default_device = std::make_shared<XAudio2DeviceDescription>(L"Primary Sound Driver", L"");
s_devices.insert(s_devices.begin(), default_device);
}
wbem_enum->Release();
// Clean up
wbem_services->Release();
}
catch (const std::system_error& ex)
{

View file

@ -4,6 +4,7 @@
#include <mmsystem.h>
#include <mmreg.h>
#include <dsound.h>
#include <wrl/client.h>
#include "IAudioAPI.h"
@ -50,11 +51,6 @@ private:
static const std::vector<DeviceDescriptionPtr>& RefreshDevices();
struct XAudioDeleter
{
void operator()(IXAudio2* ptr) const;
};
struct VoiceDeleter
{
void operator()(IXAudio2Voice* ptr) const;
@ -63,7 +59,7 @@ private:
static HMODULE s_xaudio_dll;
static std::vector<DeviceDescriptionPtr> s_devices;
std::unique_ptr<IXAudio2, XAudioDeleter> m_xaudio;
Microsoft::WRL::ComPtr<IXAudio2> m_xaudio;
std::wstring m_device_id;
std::unique_ptr<IXAudio2MasteringVoice, VoiceDeleter> m_mastering_voice;
std::unique_ptr<IXAudio2SourceVoice, VoiceDeleter> m_source_voice;

View file

@ -45,6 +45,7 @@
#include <objidl.h>
#include <shlguid.h>
#include <shlobj.h>
#include <wrl/client.h>
#endif
// public events
@ -1630,8 +1631,8 @@ void wxGameList::CreateShortcut(GameInfo2& gameInfo)
}
}
IShellLinkW* shellLink;
HRESULT hres = CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER, IID_IShellLink, reinterpret_cast<LPVOID*>(&shellLink));
Microsoft::WRL::ComPtr<IShellLinkW> shellLink;
HRESULT hres = CoCreateInstance(__uuidof(ShellLink), nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&shellLink));
if (SUCCEEDED(hres))
{
const auto description = wxString::Format("Play %s on Cemu", titleName);
@ -1647,15 +1648,13 @@ void wxGameList::CreateShortcut(GameInfo2& gameInfo)
else
shellLink->SetIconLocation(exePath.wstring().c_str(), 0);
IPersistFile* shellLinkFile;
Microsoft::WRL::ComPtr<IPersistFile> shellLinkFile;
// save the shortcut
hres = shellLink->QueryInterface(IID_IPersistFile, reinterpret_cast<LPVOID*>(&shellLinkFile));
hres = shellLink.As(&shellLinkFile);
if (SUCCEEDED(hres))
{
hres = shellLinkFile->Save(outputPath.wc_str(), TRUE);
shellLinkFile->Release();
}
shellLink->Release();
}
if (!SUCCEEDED(hres)) {
auto errorMsg = formatWxString(_("Failed to save shortcut to {}"), outputPath);

View file

@ -15,9 +15,6 @@ DirectInputController::DirectInputController(const GUID& guid, std::string_view
DirectInputController::~DirectInputController()
{
if (m_effect)
m_effect->Release();
if (m_device)
{
m_device->Unacquire();
@ -39,8 +36,8 @@ DirectInputController::~DirectInputController()
if (kGameCubeController == m_product_guid)
should_release_device = false;
if (should_release_device)
m_device->Release();
if (!should_release_device)
m_device.Detach();
}
}
@ -104,7 +101,6 @@ bool DirectInputController::connect()
// set data format
if (FAILED(m_device->SetDataFormat(m_provider->get_data_format())))
{
SAFE_RELEASE(m_device);
return false;
}
@ -115,7 +111,6 @@ bool DirectInputController::connect()
{
if (FAILED(m_device->SetCooperativeLevel(hwndMainWindow, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)))
{
SAFE_RELEASE(m_device);
return false;
}
// rumble can only be used with exclusive access

View file

@ -2,6 +2,7 @@
#include "input/api/DirectInput/DirectInputControllerProvider.h"
#include "input/api/Controller.h"
#include <wrl/client.h>
class DirectInputController : public Controller<DirectInputControllerProvider>
{
@ -41,8 +42,8 @@ private:
GUID m_product_guid{};
std::shared_mutex m_mutex;
LPDIRECTINPUTDEVICE8 m_device = nullptr;
LPDIRECTINPUTEFFECT m_effect = nullptr;
Microsoft::WRL::ComPtr<IDirectInputDevice8> m_device;
Microsoft::WRL::ComPtr<IDirectInputEffect> m_effect;
std::array<LONG, 6> m_min_axis{};
std::array<LONG, 6> m_max_axis{};

View file

@ -19,7 +19,7 @@ DirectInputControllerProvider::DirectInputControllerProvider()
const auto r = DirectInput8Create(GetModuleHandle(nullptr), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&m_dinput8, nullptr);
if (FAILED(r) || !m_dinput8)
if (FAILED(r))
{
const auto error = GetLastError();
//FreeLibrary(m_module);
@ -29,9 +29,6 @@ DirectInputControllerProvider::DirectInputControllerProvider()
DirectInputControllerProvider::~DirectInputControllerProvider()
{
if (m_dinput8)
m_dinput8->Release();
/*if (m_module)
FreeLibrary(m_module);
*/

View file

@ -4,6 +4,7 @@
#define DIRECTINPUT_VERSION 0x0800
#include <dinput.h>
#include <wrl/client.h>
#include "input/api/ControllerProvider.h"
@ -22,7 +23,7 @@ public:
std::vector<std::shared_ptr<ControllerBase>> get_controllers() override;
IDirectInput8* get_dinput() const { return m_dinput8; }
IDirectInput8* get_dinput() const { return m_dinput8.Get(); }
LPCDIDATAFORMAT get_data_format() const;
private:
@ -31,7 +32,7 @@ private:
decltype(&DirectInput8Create) m_DirectInput8Create;
decltype(&GetdfDIJoystick) m_GetdfDIJoystick = nullptr;
IDirectInput8* m_dinput8 = nullptr;
Microsoft::WRL::ComPtr<IDirectInput8> m_dinput8;
};
#endif

View file

@ -1,7 +1,7 @@
#pragma once
#include <dxgi1_4.h>
//#include <atlbase.h>
#include <wrl/client.h>
class DXGIWrapper
{
@ -23,34 +23,28 @@ public:
throw std::runtime_error("can't find CreateDXGIFactory1 in dxgi module");
}
IDXGIFactory1* dxgiFactory = nullptr;
Microsoft::WRL::ComPtr<IDXGIFactory1> dxgiFactory;
pCreateDXGIFactory1(IID_PPV_ARGS(&dxgiFactory));
IDXGIAdapter1* tmpDxgiAdapter = nullptr;
Microsoft::WRL::ComPtr<IDXGIAdapter1> dxgiAdapter;
UINT adapterIndex = 0;
while (dxgiFactory->EnumAdapters1(adapterIndex, &tmpDxgiAdapter) != DXGI_ERROR_NOT_FOUND)
while (dxgiFactory->EnumAdapters1(adapterIndex, &dxgiAdapter) != DXGI_ERROR_NOT_FOUND)
{
DXGI_ADAPTER_DESC1 desc;
tmpDxgiAdapter->GetDesc1(&desc);
dxgiAdapter->GetDesc1(&desc);
if (deviceLUID == nullptr || memcmp(&desc.AdapterLuid, deviceLUID, sizeof(LUID)) == 0)
{
tmpDxgiAdapter->QueryInterface(IID_PPV_ARGS(&m_dxgiAdapter));
tmpDxgiAdapter->Release();
if (FAILED(dxgiAdapter.As(&m_dxgiAdapter)))
{
Cleanup();
throw std::runtime_error("can't create dxgi adapter");
}
break;
}
tmpDxgiAdapter->Release();
++adapterIndex;
}
dxgiFactory->Release();
if (!m_dxgiAdapter)
{
Cleanup();
throw std::runtime_error("can't create dxgi adapter");
}
}
~DXGIWrapper()
@ -65,15 +59,11 @@ public:
private:
HMODULE m_moduleHandle = nullptr;
IDXGIAdapter3* m_dxgiAdapter = nullptr;
Microsoft::WRL::ComPtr<IDXGIAdapter3> m_dxgiAdapter;
void Cleanup()
{
if (m_dxgiAdapter)
{
m_dxgiAdapter->Release();
m_dxgiAdapter = nullptr;
}
m_dxgiAdapter.Reset();
if (m_moduleHandle)
{