Repository navigation
Expand file tree
/
Copy pathVolTest.cs
More file actions
79 lines (68 loc) · 3.08 KB
/
Copy pathVolTest.cs
File metadata and controls
79 lines (68 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Verifies the output half of MagicVolume: that the volume keystrokes it
// synthesises really do move the Windows master volume.
using System;
using System.Runtime.InteropServices;
using System.Threading;
[ComImport, Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")] class MMDeviceEnumerator { }
[ComImport, Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IMMDeviceEnumerator {
int EnumAudioEndpoints(int dataFlow, int mask, out IntPtr col);
int GetDefaultAudioEndpoint(int dataFlow, int role, out IMMDevice dev);
}
[ComImport, Guid("D666063F-1587-4E43-81F1-B948E807363F"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IMMDevice {
int Activate(ref Guid iid, int ctx, IntPtr param,
[MarshalAs(UnmanagedType.IUnknown)] out object iface);
}
[ComImport, Guid("5CDF2C82-841E-4546-9722-0CF74078229A"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IAudioEndpointVolume {
int RegisterControlChangeNotify(IntPtr n);
int UnregisterControlChangeNotify(IntPtr n);
int GetChannelCount(out uint c);
int SetMasterVolumeLevel(float d, ref Guid ctx);
int SetMasterVolumeLevelScalar(float d, ref Guid ctx);
int GetMasterVolumeLevel(out float d);
int GetMasterVolumeLevelScalar(out float d);
int SetChannelVolumeLevel(uint ch, float d, ref Guid ctx);
int SetChannelVolumeLevelScalar(uint ch, float d, ref Guid ctx);
int GetChannelVolumeLevel(uint ch, out float d);
int GetChannelVolumeLevelScalar(uint ch, out float d);
int SetMute([MarshalAs(UnmanagedType.Bool)] bool m, ref Guid ctx);
int GetMute([MarshalAs(UnmanagedType.Bool)] out bool m);
}
class VolTest {
[DllImport("user32.dll")] static extern void keybd_event(byte vk, byte scan, uint flags, UIntPtr extra);
const byte VK_VOLUME_DOWN = 0xAE, VK_VOLUME_UP = 0xAF;
const uint KEYUP = 0x0002;
static IAudioEndpointVolume Endpoint() {
var en = (IMMDeviceEnumerator)new MMDeviceEnumerator();
IMMDevice dev;
en.GetDefaultAudioEndpoint(0, 1, out dev); // eRender, eMultimedia
Guid iid = typeof(IAudioEndpointVolume).GUID;
object o;
dev.Activate(ref iid, 23, IntPtr.Zero, out o);
return (IAudioEndpointVolume)o;
}
static void Tap(byte vk) {
keybd_event(vk, 0, 0, UIntPtr.Zero);
keybd_event(vk, 0, KEYUP, UIntPtr.Zero);
Thread.Sleep(160);
}
static void Main() {
var vol = Endpoint();
float start; vol.GetMasterVolumeLevelScalar(out start);
Console.WriteLine("volume before : " + Math.Round(start * 100) + "%");
Tap(VK_VOLUME_UP);
float up; vol.GetMasterVolumeLevelScalar(out up);
Console.WriteLine("after VOL_UP : " + Math.Round(up * 100) + "%");
Tap(VK_VOLUME_DOWN);
float back; vol.GetMasterVolumeLevelScalar(out back);
Console.WriteLine("after VOL_DOWN: " + Math.Round(back * 100) + "%");
Console.WriteLine(Math.Abs(up - start) > 0.001f
? "RESULT: volume commands work."
: "RESULT: volume did NOT move.");
}
}