Last active
September 6, 2015 16:58
-
-
Save zenden2k/8f4f98925d2d45027a62 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#include <iostream> | |
#include <mmdeviceapi.h> | |
#include <endpointvolume.h> | |
#include <Audiopolicy.h> | |
#include <comdef.h> | |
#include <comip.h> | |
#define CHECK_HR(hr) \ | |
if(FAILED(hr)) { \ | |
std::cout << "error" << std::endl; \ | |
return 0; \ | |
} | |
_COM_SMARTPTR_TYPEDEF(IMMDevice, __uuidof(IMMDevice)); | |
_COM_SMARTPTR_TYPEDEF(IMMDeviceEnumerator, __uuidof(IMMDeviceEnumerator)); | |
_COM_SMARTPTR_TYPEDEF(IAudioSessionManager2, __uuidof(IAudioSessionManager2)); | |
_COM_SMARTPTR_TYPEDEF(IAudioSessionManager2, __uuidof(IAudioSessionManager2)); | |
_COM_SMARTPTR_TYPEDEF(IAudioSessionEnumerator, __uuidof(IAudioSessionEnumerator)); | |
_COM_SMARTPTR_TYPEDEF(IAudioSessionControl2, __uuidof(IAudioSessionControl2)); | |
_COM_SMARTPTR_TYPEDEF(IAudioSessionControl, __uuidof(IAudioSessionControl)); | |
_COM_SMARTPTR_TYPEDEF(ISimpleAudioVolume, __uuidof(ISimpleAudioVolume)); | |
IAudioSessionManager2Ptr CreateSessionManager() | |
{ | |
HRESULT hr = S_OK; | |
IMMDevicePtr pDevice; | |
IMMDeviceEnumeratorPtr pEnumerator; | |
IAudioSessionManager2Ptr pSessionManager; | |
// Create the device enumerator. | |
CHECK_HR(hr = CoCreateInstance( | |
__uuidof(MMDeviceEnumerator), | |
NULL, CLSCTX_ALL, | |
__uuidof(IMMDeviceEnumerator), | |
(void**)&pEnumerator)); | |
// Get the default audio device. | |
CHECK_HR(hr = pEnumerator->GetDefaultAudioEndpoint( | |
eRender, eConsole, &pDevice)); | |
// Get the session manager. | |
CHECK_HR(hr = pDevice->Activate( | |
__uuidof(IAudioSessionManager2), CLSCTX_ALL, | |
NULL, (void**)&pSessionManager)); | |
return pSessionManager; | |
} | |
bool MuteProcess(DWORD processId) { | |
bool res = false; | |
IAudioSessionManager2Ptr mgr = CreateSessionManager(); | |
if (!mgr) { | |
return false; | |
} | |
IAudioSessionEnumeratorPtr enumerator; | |
if (SUCCEEDED(mgr->GetSessionEnumerator(&enumerator))) { | |
int sessionCount; | |
if (SUCCEEDED(enumerator->GetCount(&sessionCount))) { | |
for (int i = 0; i < sessionCount; i++) { | |
IAudioSessionControlPtr control; | |
if (SUCCEEDED(enumerator->GetSession(i, &control))) { | |
IAudioSessionControl2Ptr control2; | |
if (SUCCEEDED(control->QueryInterface(__uuidof(IAudioSessionControl2), (void**)&control2))) { | |
DWORD foundProcessId; | |
if (SUCCEEDED(control2->GetProcessId(&foundProcessId))) { | |
if (foundProcessId == processId) { | |
ISimpleAudioVolumePtr volume; | |
if (SUCCEEDED(control2->QueryInterface(_uuidof(ISimpleAudioVolume), (void**)&volume))) { | |
if (SUCCEEDED(volume->SetMute(TRUE, 0))) { | |
res = true; | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
return res; | |
} | |
int _tmain(int argc, _TCHAR* argv[]){ | |
CoInitialize(NULL); | |
DWORD processId = 11944; | |
MuteProcess(processId); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment