VC控制Win7系统音量

#include <Windows.h>  
#include <Endpointvolume.h>  
#include <Mmdeviceapi.h>  
#include <conio.h>  
#pragma comment(lib, "Winmm.lib")  
 
IAudioEndpointVolume *m_pEndptVolCtrl;
IMMDeviceEnumerator *m_pEnumerator;
IMMDevice *m_pDevice;
 
int _tmain(int argc, _TCHAR* argv[])
{
    HRESULT hr = S_OK;
    int ret = 0;
 
    // Initialize COM component  
    hr = CoInitialize(NULL);
 
    if (FAILED(hr)) {
        printf("CoInitialize failed");
        return 1;
    }
 
    // Create instance for MMDevices...  
    hr = CoCreateInstance(__uuidof(MMDeviceEnumerator),
        NULL, CLSCTX_INPROC_SERVER,
        __uuidof(IMMDeviceEnumerator),
        (void**)&m_pEnumerator);
    if (FAILED(hr)) {
        printf("CoCreateInstance failed");
        return 1;
    }
 
    // Get default Andio Endpoint  
    hr = m_pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &m_pDevice);
    if (FAILED(hr)) {
        printf("GetDefaultAudioEndpoint failed");
        return 1;
    }
 
    // Activate devices...  
    hr = m_pDevice->Activate(__uuidof(IAudioEndpointVolume),
        CLSCTX_ALL, NULL,
        (void**)&m_pEndptVolCtrl);
    if (FAILED(hr)) {
        printf("Activate failed");
        return 1;
    }
 
/*
    // Now you can operate it....  
    printf("SetMute...\n");
    hr = m_pEndptVolCtrl->SetMute(1, &GUID_NULL);
    getchar();
    printf("UnSetMute...\n");
    hr = m_pEndptVolCtrl->SetMute(0, &GUID_NULL);
    getchar();
*/
    UINT chan;
    hr = m_pEndptVolCtrl->GetChannelCount(&chan);
    if (FAILED(hr)) {
        printf("GetChannelCount fail\n");
        ret = 1;
    }
    printf("Set volume to Max...\nChannels: %d\n", chan);
    float vol;
    float vol1;
    float vol2;
 
    hr = m_pEndptVolCtrl->GetMasterVolumeLevelScalar(&vol);
    if (FAILED(hr)) {
        printf("GetMasterVolumeLevel fail\n");
        ret = 1;
    }
    hr = m_pEndptVolCtrl->GetChannelVolumeLevelScalar(0, &vol1);
    if (FAILED(hr)) {
        printf("GetChannelVolumeLevel fail\n");
        ret = 1;
    }
    hr = m_pEndptVolCtrl->GetChannelVolumeLevelScalar(1, &vol2);
    if (FAILED(hr)) {
        printf("GetChannelVolumeLevel fail\n");
        ret = 1;
    }
    printf("Before Master : %1.2f Left %1.2f  Right: %1.2f\n", vol, vol1, vol2);
 
    vol  = 1.0;
    vol1 = 1.0;
    vol2 = 1.0;
 
    printf("Set UnMute... ");
    hr = m_pEndptVolCtrl->SetMute(0, &GUID_NULL);
    if (FAILED(hr)) {
        printf("fail\n");
        ret = 1;
    }
    else {
        printf("ok\n");
    }
    
    printf("Set Master Volume Level... ");
    hr = m_pEndptVolCtrl->SetMasterVolumeLevelScalar(vol, &GUID_NULL);
    if (FAILED(hr)) {
        printf("fail\n");
        ret = 1;
    }
    else {
        printf("ok\n");
    }
 
    printf("Set Right  Volume Level... ");
    hr = m_pEndptVolCtrl->SetChannelVolumeLevelScalar(0, vol1, &GUID_NULL);
    if (FAILED(hr)) {
        printf("fail\n");
        ret = 1;
    }
    else {
        printf("ok\n");
    }
    
    printf("Set Left   Volume Level... ");
    hr = m_pEndptVolCtrl->SetChannelVolumeLevelScalar(1, vol2, &GUID_NULL);
    if (FAILED(hr)) {
        printf("fail\n");
        ret = 1;
    }
    else {
        printf("ok\n");
    }
 
    hr = m_pEndptVolCtrl->GetMasterVolumeLevelScalar(&vol);
    if (FAILED(hr)) {
        printf("GetMasterVolumeLevel fail\n");
        ret = 1;
    }
    hr = m_pEndptVolCtrl->GetChannelVolumeLevelScalar(0, &vol1);
    if (FAILED(hr)) {
        printf("GetChannelVolumeLevel fail\n");
        ret = 1;
    }
    hr = m_pEndptVolCtrl->GetChannelVolumeLevelScalar(1, &vol2);
    if (FAILED(hr)) {
        printf("GetChannelVolumeLevel fail\n");
        ret = 1;
    }
    printf("After Master : %1.2f Left %1.2f  Right: %1.2f\n", vol, vol1, vol2);
 
    CoUninitialize();
    return ret;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值