C代码实现控制Win 7系统音量
  imGfSpebnSJ1 2023年11月02日 52 0


之前的代码基本上是控制Windows XP的代码。

可是XP的音量控制代码在Win7上不能生效。

查看了MSDN的相关内容,Win7的代码控制只能通过COM的方式来实现。

如下为详细代码:

本代码在VC++ 2010(这个工具貌似集成了Win7 SDK)中开发。

#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;
}



【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  4WdcduV19eWs   2023年11月02日   54   0   0 ios#includeios#include
  4WdcduV19eWs   2023年11月02日   44   0   0 ios#includeios#include
  4WdcduV19eWs   2023年11月02日   67   0   0 ios#includeios#include
  4WdcduV19eWs   2023年11月02日   90   0   0 ios#includeios#include
  4WdcduV19eWs   2023年11月02日   51   0   0 ios#includeios#include
imGfSpebnSJ1