之前的代码基本上是控制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;
}