I’ve been trying to get a program working which uses the Windows audio APIs.
The end result will be a programs that checks for volume changes in other audio process.
I’ve looked at some of the examples on the Microsoft site and have an undersatanding of what I should be doing.
However when I attempt to implement this I get some errors. I’m starting by trying to get a list, well actually just a count at the minute, of all the audio process running.
The code I’m using is based on the code on the Windows site however I get errors when I try to build it. The two main ones are:
- ‘IAudioSessionManager2’ : no GUID has been associated with this object
- use of undefined type ‘IAudioSessionManager2’
There is also a similar one for the 2nd one but relating to IAudioSessionEnumerator.
I’m quite new to C++ programming, more used to Java, so I’m thinking that it’s just some silly little mistake to do with header files or something, I’ve included the .h code and .cpp code below.
If anyone would be able to help me it would be much appreciated.
GetProcess.h
#ifndef GETPROCESS_H
#define GETPROCESS_H
#define SAFE_RELEASE(p) { if ( (p) ) { (p)->Release(); (p) = 0; } }
#include<Audiopolicy.h>
#include<Mmdeviceapi.h>
#include <stdio.h>
#include <iostream>
using namespace std;
struct IAudioSessionManager2;
struct IAudioSessionEnumerator;
struct IAudioSessionControl;
struct IMMDevice;
struct IMMDeviceEnumerator;
ref class GetProcess
{
public:
GetProcess();
};
#endif
GetProcess.cpp
#include "StdAfx.h"
#include "GetProcess.h"
#include<Audiopolicy.h>
#include<Mmdeviceapi.h>
#include <stdio.h>
#include <iostream>
#include <windows.h>
GetProcess::GetProcess()
{
IMMDeviceEnumerator* deviceEnumerator = NULL;
IMMDevice* device = NULL;
IAudioSessionManager2* sessionManager = NULL;
IAudioSessionEnumerator* sessionEnumerator = NULL;
IAudioSessionControl* session = NULL;
int numberOfProcesses = 0;
int numberOfActiveProcesses = 0;
AudioSessionState state;
//create device enumerator
CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&deviceEnumerator);
// get default device
deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &device);
// activate session manager
device->Activate(__uuidof(IAudioSessionManager2), CLSCTX_ALL, NULL, (void**)&sessionManager);
//// make enum
sessionManager->GetSessionEnumerator(&sessionEnumerator);
sessionEnumerator ->GetCount(&numberOfProcesses);
for (int i = 0; i < numberOfProcesses; i++)
{
sessionEnumerator->GetSession(i, &session);
session->GetState(&state);
if (state == 1)
{
numberOfActiveProcesses++;
}
SAFE_RELEASE(session);
}
//CLEANUP
SAFE_RELEASE(deviceEnumerator);
SAFE_RELEASE(device);
SAFE_RELEASE(sessionEnumerator);
SAFE_RELEASE(sessionManager);
}
You have version 6.0. You’ll need to download and install Windows SDK version 7.1. IAudioSessionManager2 is only available on Windows 7.