-1
#include <iostream>
#include <windows.h>
#include <Audioclient.h>

int main(){
    ShellExecute(NULL, "open", "https://www.youtube.com/watch?v=zf2VYAtqRe0", NULL, NULL, SW_SHOWNORMAL);
    HRESULT SetMasterVolume(1.0, NULL);
    return();
}

Okay so I'm trying to code this program, that opens a YouTube song, and turns up the volume at the same time. I don´t understand the error I get.

ERROR : C2440 ´initializing´: cannot convert from ´initializer list´ to ´HRESULT´

So therefore my question is: how do I initialize HRESULT so SetMasterVolume works? Or, how to setup SetMasterVolume? And please, if possible, explain why I cant just write

SetMasterVolume(1.0,NULL);

When I have included audioclient.h

2 Answers2

3

ISimpleAudioVolume::SetMasterVolume is a COM method, it is not a regular WinAPI. You get a compile error when you just type in the function. Adding HRESULT in front of it will cause a different C++ error.

Use this code instead, with SetMasterVolumeLevelScalar

Based on code from:
Change Master Volume in Visual C++

#include <Windows.h>
#include <Mmdeviceapi.h>
#include <Endpointvolume.h>

BOOL ChangeVolume(float nVolume)
{
    HRESULT hr = NULL;
    IMMDeviceEnumerator *deviceEnumerator = NULL;
    hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER,
        __uuidof(IMMDeviceEnumerator), (LPVOID *)&deviceEnumerator);
    if(FAILED(hr))
        return FALSE;

    IMMDevice *defaultDevice = NULL;
    hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
    deviceEnumerator->Release();
    if(FAILED(hr))
        return FALSE;

    IAudioEndpointVolume *endpointVolume = NULL;
    hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume),
        CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
    defaultDevice->Release();
    if(FAILED(hr))
        return FALSE;

    hr = endpointVolume->SetMasterVolumeLevelScalar(nVolume, NULL);
    endpointVolume->Release();

    return SUCCEEDED(hr);
}

int main()
{
    CoInitialize(NULL);
    ChangeVolume(0.5);
    CoUninitialize();
    return 0;
}
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
-1

You need to give it a name and assign to it.

HRESULT hResult = SetMasterVolume(1.0, NULL);
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • So now you get: `error C3861: 'SetMasterVolume': identifier not found`. Not entirely helpful. While it answers the question that was asked, it fails to acknowledge, that `SetMasterVolume` is not a free function. This answer is not helpful, sorry. Maybe it was a bad idea to answer one of those questions that contain the phrase *"I'm new to C++"*. – IInspectable Feb 17 '18 at 09:54