16 |
pfowler |
1 |
// dllmain.cpp : Defines the entry point for the DLL application.
|
|
|
2 |
#include "stdafx.h"
|
|
|
3 |
#include <stdio.h>
|
|
|
4 |
#include <objbase.h>
|
|
|
5 |
#include <Windows.h>
|
|
|
6 |
#include <mmdeviceapi.h>
|
|
|
7 |
#include <endpointvolume.h>
|
|
|
8 |
#include <functiondiscoverykeys_devpkey.h>
|
|
|
9 |
|
|
|
10 |
|
|
|
11 |
unsigned int devCount = 0;
|
|
|
12 |
|
|
|
13 |
const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
|
|
|
14 |
const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
|
|
|
15 |
|
|
|
16 |
HRESULT hr = S_OK;
|
|
|
17 |
IMMDeviceEnumerator *pEnum = NULL;
|
|
|
18 |
IMMDeviceCollection *pColl = NULL;
|
|
|
19 |
|
|
|
20 |
void initDevices();
|
|
|
21 |
|
|
|
22 |
#define SAFE_RELEASE(punk) \
|
|
|
23 |
if ((punk) != NULL) \
|
|
|
24 |
{ (punk)->Release(); (punk) = NULL; }
|
|
|
25 |
|
|
|
26 |
BOOL APIENTRY DllMain( HMODULE hModule,
|
|
|
27 |
DWORD ul_reason_for_call,
|
|
|
28 |
LPVOID lpReserved
|
|
|
29 |
) {
|
|
|
30 |
|
|
|
31 |
switch (ul_reason_for_call) {
|
|
|
32 |
case DLL_PROCESS_ATTACH:
|
|
|
33 |
initDevices();
|
|
|
34 |
break;
|
|
|
35 |
case DLL_THREAD_ATTACH:
|
|
|
36 |
// Thread-specific init
|
|
|
37 |
|
|
|
38 |
case DLL_THREAD_DETACH:
|
|
|
39 |
// Thread-specific cleanup
|
|
|
40 |
case DLL_PROCESS_DETACH:
|
|
|
41 |
//Perform any necessary cleanip
|
|
|
42 |
break;
|
|
|
43 |
}
|
|
|
44 |
return TRUE;
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
extern "C" __declspec(dllexport) int _stdcall _getDevCount(unsigned int *deviceCount) {
|
|
|
48 |
*deviceCount = devCount;
|
|
|
49 |
return 1;
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
|
|
|
53 |
extern "C" __declspec(dllexport) LPWSTR _stdcall _getDevName(unsigned int dev) {
|
|
|
54 |
IMMDevice *pEndpoint = NULL;
|
|
|
55 |
IPropertyStore *pProps = NULL;
|
|
|
56 |
LPWSTR pwszID = NULL;
|
|
|
57 |
hr = pColl->Item(dev, &pEndpoint);
|
|
|
58 |
hr = pEndpoint->GetId(&pwszID);
|
|
|
59 |
hr = pEndpoint->OpenPropertyStore(STGM_READ, &pProps);
|
|
|
60 |
|
|
|
61 |
PROPVARIANT varName;
|
|
|
62 |
PropVariantInit(&varName);
|
|
|
63 |
|
|
|
64 |
hr = pProps->GetValue(PKEY_Device_FriendlyName, &varName);
|
|
|
65 |
|
|
|
66 |
LPWSTR name = varName.pwszVal;
|
|
|
67 |
|
|
|
68 |
CoTaskMemFree(pwszID);
|
|
|
69 |
pwszID = NULL;
|
|
|
70 |
//PropVariantClear(&varName);
|
|
|
71 |
SAFE_RELEASE(pEndpoint);
|
|
|
72 |
SAFE_RELEASE(pProps);
|
|
|
73 |
|
|
|
74 |
return name;
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
extern "C" __declspec(dllexport) unsigned int _stdcall _getDevVolume(unsigned int dev, float *deviceVolume) {
|
|
|
78 |
IMMDevice *pEndpoint = NULL;
|
|
|
79 |
hr = pColl->Item(dev, &pEndpoint);
|
|
|
80 |
IAudioEndpointVolume *endpointVolume = NULL;
|
|
|
81 |
pEndpoint->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
|
|
|
82 |
|
|
|
83 |
float volume = 0;
|
|
|
84 |
endpointVolume->GetMasterVolumeLevelScalar(&volume);
|
|
|
85 |
|
|
|
86 |
*deviceVolume = volume;
|
|
|
87 |
|
|
|
88 |
SAFE_RELEASE(pEndpoint);
|
|
|
89 |
SAFE_RELEASE(endpointVolume);
|
|
|
90 |
|
|
|
91 |
return 1;
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
extern "C" __declspec(dllexport) unsigned int _stdcall _setDevVolume(unsigned int dev, float deviceVolume) {
|
|
|
95 |
IMMDevice *pEndpoint = NULL;
|
|
|
96 |
hr = pColl->Item(dev, &pEndpoint);
|
|
|
97 |
IAudioEndpointVolume *endpointVolume = NULL;
|
|
|
98 |
pEndpoint->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
|
|
|
99 |
|
|
|
100 |
endpointVolume->SetMasterVolumeLevelScalar(deviceVolume, NULL);
|
|
|
101 |
|
|
|
102 |
SAFE_RELEASE(pEndpoint);
|
|
|
103 |
SAFE_RELEASE(endpointVolume);
|
|
|
104 |
|
|
|
105 |
return 1;
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
|
|
|
109 |
|
|
|
110 |
void initDevices() {
|
|
|
111 |
hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL,
|
|
|
112 |
CLSCTX_ALL, IID_IMMDeviceEnumerator,
|
|
|
113 |
(void**)&pEnum);
|
|
|
114 |
hr = pEnum->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &pColl);
|
|
|
115 |
|
|
|
116 |
hr = pColl->GetCount(&devCount);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
|