WASAPI GetDisplayName returns blank

1.2k views Asked by At

I am trying to write a program that interfaces with Windows Core Audio and WASAPI. I am having difficulty with the following couple lines.

CComHeapPtr<WCHAR> name;
hr = pAudioSessionControl->GetDisplayName(&name);
if (FAILED(hr)) {
    LOG(L"IAudioSessionControl::GetDisplayName() failed: hr = 0x%08x", hr);
    return -__LINE__; }
_tprintf(_T("Session Index %d, \"%s\"\n"), i, CString(name));

Outputs:

Session Index 0, "@%SystemRoot%\System32\AudioSrv.Dll,-202"
Session Index 1, ""
Session Index 2, ""
Session Index 3, ""
Press any key to continue . . .

This is with 3 programs all active and making noise. It seems i can see the system sounds program but nothing else.

Thanks for the help.

2

There are 2 answers

1
Roman Ryltsov On

IAudioSessionControl::GetDisplayName is proper API and it might return non-empty strings, however you might also see inactive sessions for which the strings are indeed empty. In your case you might hit inactive sessions, error code which you did not provide or otherwise incorrect API use.

This code snippet/application enumerates sessions and polls for volume changes - it prints non-empty strings.

CComHeapPtr<WCHAR> pszDisplayName;
ATLENSURE_SUCCEEDED(pSessionControl->GetDisplayName(&pszDisplayName));
_tprintf(_T("nSessionIndex %d, pszDisplayName \"%s\"\n"), 
    nSessionIndex, CString(pszDisplayName));
C:\AudioSessionVolumeNotification\Debug>AudioSessionVolumeNotification.exe
nSessionCount 5
nSessionIndex 0, pszDisplayName "@%SystemRoot%\System32\AudioSrv.Dll,-202"
nSessionIndex 1, pszDisplayName "Mozilla Firefox"
nSessionIndex 2, pszDisplayName "Mozilla Thunderbird"
nSessionIndex 3, pszDisplayName "Mozilla Firefox"
nSessionIndex 4, pszDisplayName ""
0
nekomona On

An idea from another question.

The problem should be about sessions themselves. Most programs never name their sessions, so usually sessions don't have names, and the name shown on audio mixer might be the name of the window title of sessions' owner process.

Using IAudioSessionControl2::GetProcessID and get the window title of the process through other APIs should give a reasonable name similar to the one from audio mixer.