Subversion Repositories group.NITPanels

Rev

Blame | Last modification | View Log | RSS feed

// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
#include <stdio.h>
#include <objbase.h>
#include <Windows.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>
#include <functiondiscoverykeys_devpkey.h>


unsigned int devCount = 0;

const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);

HRESULT hr = S_OK;
IMMDeviceEnumerator *pEnum = NULL;
IMMDeviceCollection *pColl = NULL;

void initDevices();

#define SAFE_RELEASE(punk)  \
              if ((punk) != NULL)  \
                { (punk)->Release(); (punk) = NULL; }

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                                         ) {

        switch (ul_reason_for_call)     {
        case DLL_PROCESS_ATTACH:
                initDevices();
                break;
        case DLL_THREAD_ATTACH:
                // Thread-specific init
                
        case DLL_THREAD_DETACH:
                // Thread-specific cleanup
        case DLL_PROCESS_DETACH:
                //Perform any necessary cleanip
                break;
        }
        return TRUE;
}

extern "C" __declspec(dllexport) int _stdcall _getDevCount(unsigned int *deviceCount) {
        *deviceCount = devCount;
        return 1;
}


extern "C" __declspec(dllexport) LPWSTR _stdcall _getDevName(unsigned int dev) {
        IMMDevice *pEndpoint = NULL;
        IPropertyStore *pProps = NULL;
        LPWSTR pwszID = NULL;
        hr = pColl->Item(dev, &pEndpoint);
        hr = pEndpoint->GetId(&pwszID);
        hr = pEndpoint->OpenPropertyStore(STGM_READ, &pProps);
        
        PROPVARIANT varName;
        PropVariantInit(&varName);

        hr = pProps->GetValue(PKEY_Device_FriendlyName, &varName);

        LPWSTR name = varName.pwszVal;

        CoTaskMemFree(pwszID);
        pwszID = NULL;
        //PropVariantClear(&varName);
        SAFE_RELEASE(pEndpoint);
        SAFE_RELEASE(pProps);

        return name;
}

extern "C" __declspec(dllexport) unsigned int _stdcall _getDevVolume(unsigned int dev, float *deviceVolume) {
        IMMDevice *pEndpoint = NULL;
        hr = pColl->Item(dev, &pEndpoint);
        IAudioEndpointVolume *endpointVolume = NULL;
        pEndpoint->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
        
        float volume = 0;
        endpointVolume->GetMasterVolumeLevelScalar(&volume);

        *deviceVolume = volume;
        
        SAFE_RELEASE(pEndpoint);
        SAFE_RELEASE(endpointVolume);

        return 1;
}

extern "C" __declspec(dllexport) unsigned int _stdcall _setDevVolume(unsigned int dev, float deviceVolume) {
        IMMDevice *pEndpoint = NULL;
        hr = pColl->Item(dev, &pEndpoint);
        IAudioEndpointVolume *endpointVolume = NULL;
        pEndpoint->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
        
        endpointVolume->SetMasterVolumeLevelScalar(deviceVolume, NULL);

        SAFE_RELEASE(pEndpoint);
        SAFE_RELEASE(endpointVolume);

        return 1;
}



void initDevices() {
        hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL,
                CLSCTX_ALL, IID_IMMDeviceEnumerator,
                (void**)&pEnum);
        hr = pEnum->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &pColl);

        hr = pColl->GetCount(&devCount);
}