Win7Audio: Remove try/catch blocks

This commit is contained in:
Birunthan Mohanathas 2013-12-24 11:45:25 +00:00
parent da92def9fc
commit 0136f81163

View File

@ -104,29 +104,23 @@ void UnInitCom()
HRESULT RegisterDevice(PCWSTR devID) HRESULT RegisterDevice(PCWSTR devID)
{ {
HRESULT hr = S_FALSE; HRESULT hr = S_FALSE;
try
{
InitCom();
IPolicyConfig *pPolicyConfig;
hr = CoCreateInstance(IID_CPolicyConfigClient, nullptr, InitCom();
CLSCTX_ALL, IID_IPolicyConfig, IPolicyConfig *pPolicyConfig;
(LPVOID *)&pPolicyConfig);
hr = CoCreateInstance(IID_CPolicyConfigClient, nullptr,
CLSCTX_ALL, IID_IPolicyConfig,
(LPVOID *)&pPolicyConfig);
if (hr == S_OK)
{
hr = pPolicyConfig->SetDefaultEndpoint(devID, eConsole);
if (hr == S_OK) if (hr == S_OK)
{ {
hr = pPolicyConfig->SetDefaultEndpoint(devID, eConsole); hr = pPolicyConfig->SetDefaultEndpoint(devID, eCommunications);
if (hr == S_OK)
{
hr = pPolicyConfig->SetDefaultEndpoint(devID, eCommunications);
}
SAFE_RELEASE(pPolicyConfig);
} }
SAFE_RELEASE(pPolicyConfig);
} }
catch (...)
{
RmLog(LOG_WARNING, L"Win7AudioPlugin.dll: RegisterDevice exception");
hr = S_FALSE;
}
UnInitCom(); UnInitCom();
return hr; return hr;
} }
@ -135,23 +129,16 @@ std::wstring GetDefaultID()
{ {
std::wstring id_default; std::wstring id_default;
IMMDevice * pEndpoint = 0; IMMDevice * pEndpoint = 0;
try if (pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pEndpoint) == S_OK)
{ {
if (pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pEndpoint) == S_OK) LPWSTR pwszID = 0;
if (pEndpoint->GetId(&pwszID) == S_OK)
{ {
LPWSTR pwszID = 0; id_default = pwszID;
if (pEndpoint->GetId(&pwszID) == S_OK)
{
id_default = pwszID;
}
CoTaskMemFree(pwszID);
} }
CoTaskMemFree(pwszID);
} }
catch (...)
{
RmLog(LOG_WARNING, L"Win7AudioPlugin.dll: GetDefaultID exception");
id_default = L"Exception";
}
SAFE_RELEASE(pEndpoint) SAFE_RELEASE(pEndpoint)
return id_default; return id_default;
} }
@ -162,32 +149,26 @@ bool GetWin7AudioState(const VolumeAction action)
IAudioEndpointVolume * pEndptVol = 0; IAudioEndpointVolume * pEndptVol = 0;
bool success = false; bool success = false;
try if (InitCom())
{ {
if (InitCom()) if (pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pEndpoint) == S_OK)
{ {
if (pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pEndpoint) == S_OK) if (pEndpoint->Activate(IID_IAudioEndpointVolume, CLSCTX_ALL, 0, (void**)&pEndptVol) == S_OK)
{ {
if (pEndpoint->Activate(IID_IAudioEndpointVolume, CLSCTX_ALL, 0, (void**)&pEndptVol) == S_OK) if (pEndptVol->GetMute(&is_mute) == S_OK && action == TOGGLE_MUTE)
{ {
if (pEndptVol->GetMute(&is_mute) == S_OK && action == TOGGLE_MUTE) success = pEndptVol->SetMute(is_mute == TRUE ? FALSE : TRUE, 0) == S_OK;
{ }
success = pEndptVol->SetMute(is_mute == TRUE ? FALSE : TRUE, 0) == S_OK; // get current volume
} float vol = 0.0f;
// get current volume if (action != TOGGLE_MUTE && pEndptVol->GetMasterVolumeLevelScalar(&vol) == S_OK)
float vol = 0.0f; {
if (action != TOGGLE_MUTE && pEndptVol->GetMasterVolumeLevelScalar(&vol) == S_OK) master_volume = vol;
{
master_volume = vol;
}
} }
} }
} }
} }
catch (...)
{
RmLog(LOG_WARNING, L"Win7AudioPlugin.dll: ToggleMute exception");
}
SAFE_RELEASE(pEndptVol) SAFE_RELEASE(pEndptVol)
SAFE_RELEASE(pEndpoint) SAFE_RELEASE(pEndpoint)
UnInitCom(); UnInitCom();
@ -213,39 +194,32 @@ bool SetWin7Volume(UINT volume, int offset = 0)
IAudioEndpointVolume * pEndptVol = 0; IAudioEndpointVolume * pEndptVol = 0;
bool success = false; bool success = false;
try if (InitCom())
{ {
if (InitCom()) if (pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pEndpoint) == S_OK)
{ {
if (pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pEndpoint) == S_OK) if (pEndpoint->Activate(IID_IAudioEndpointVolume, CLSCTX_ALL, 0, (void**)&pEndptVol) == S_OK)
{ {
if (pEndpoint->Activate(IID_IAudioEndpointVolume, CLSCTX_ALL, 0, (void**)&pEndptVol) == S_OK) pEndptVol->SetMute(FALSE, 0);
float vol = 0.0f;
if (offset != 0) // change master volume + offset
{ {
pEndptVol->SetMute(FALSE, 0); float off = static_cast<float>(offset) / 100.0f;
float vol = 0.0f; vol = master_volume + off;
if (offset != 0) // change master volume + offset vol = (vol < 0.0f) ? 0.0f : ((vol > 1.0f) ? 1.0f : vol);
{
float off = static_cast<float>(offset) / 100.0f;
vol = master_volume + off;
vol = (vol < 0.0f) ? 0.0f : ((vol > 1.0f) ? 1.0f : vol);
}
else
{
vol = (float)volume / 100.0f;
}
// set to volume
success = pEndptVol->SetMasterVolumeLevelScalar(vol, 0) == S_OK;
if (success) success = pEndptVol->GetMasterVolumeLevelScalar(&vol) == S_OK;
if (success) master_volume = vol;
} }
else
{
vol = (float)volume / 100.0f;
}
// set to volume
success = pEndptVol->SetMasterVolumeLevelScalar(vol, 0) == S_OK;
if (success) success = pEndptVol->GetMasterVolumeLevelScalar(&vol) == S_OK;
if (success) master_volume = vol;
} }
} }
}
}
catch (...)
{
RmLog(LOG_WARNING, L"Win7AudioPlugin.dll: SetVolume exception");
}
SAFE_RELEASE(pEndptVol) SAFE_RELEASE(pEndptVol)
SAFE_RELEASE(pEndpoint) SAFE_RELEASE(pEndpoint)
UnInitCom(); UnInitCom();
@ -305,56 +279,51 @@ PLUGIN_EXPORT LPCWSTR GetString(void* data)
{ {
static WCHAR result[256]; static WCHAR result[256];
wsprintf(result, L"ERROR"); wsprintf(result, L"ERROR");
try { if (!InitCom() || !pEnumerator)
if (!InitCom() || !pEnumerator) {
{ UnInitCom();
UnInitCom(); wsprintf(result, L"ERROR - Initializing COM");
wsprintf(result, L"ERROR - Initializing COM"); return result;
return result; }
}
IMMDevice * pEndpoint = 0; IMMDevice * pEndpoint = 0;
if (pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pEndpoint) == S_OK) if (pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pEndpoint) == S_OK)
{
IPropertyStore * pProps = 0;
if (pEndpoint->OpenPropertyStore(STGM_READ, &pProps) == S_OK)
{ {
IPropertyStore * pProps = 0; PROPVARIANT varName;
if (pEndpoint->OpenPropertyStore(STGM_READ, &pProps) == S_OK) PropVariantInit(&varName);
if (pProps->GetValue(PKEY_Device_DeviceDesc, &varName) == S_OK)
{ {
PROPVARIANT varName; wcsncpy(result, varName.pwszVal, 255);
PropVariantInit(&varName); PropVariantClear(&varName);
if (pProps->GetValue(PKEY_Device_DeviceDesc, &varName) == S_OK) SAFE_RELEASE(pProps)
{ SAFE_RELEASE(pEndpoint)
wcsncpy(result, varName.pwszVal, 255); UnInitCom();
PropVariantClear(&varName); return result;
SAFE_RELEASE(pProps)
SAFE_RELEASE(pEndpoint)
UnInitCom();
return result;
}
else
{
PropVariantClear(&varName);
SAFE_RELEASE(pProps)
SAFE_RELEASE(pEndpoint)
wsprintf(result, L"ERROR - Getting Device Description");
}
} }
else else
{ {
PropVariantClear(&varName);
SAFE_RELEASE(pProps) SAFE_RELEASE(pProps)
SAFE_RELEASE(pEndpoint) SAFE_RELEASE(pEndpoint)
wsprintf(result, L"ERROR - Getting Property"); wsprintf(result, L"ERROR - Getting Device Description");
} }
} }
else else
{ {
SAFE_RELEASE(pProps)
SAFE_RELEASE(pEndpoint) SAFE_RELEASE(pEndpoint)
wsprintf(result, L"ERROR - Getting Default Device"); wsprintf(result, L"ERROR - Getting Property");
} }
} catch (...)
{
RmLog(LOG_WARNING, L"Win7AudioPlugin.dll: GetString exception");
wsprintf(result, L"Exception");
} }
else
{
SAFE_RELEASE(pEndpoint)
wsprintf(result, L"ERROR - Getting Default Device");
}
UnInitCom(); UnInitCom();
return result; return result;
} }