From e89d4e36a694b51b6aaa4fab01343dc9415d897b Mon Sep 17 00:00:00 2001 From: xenolightning Date: Wed, 2 Mar 2016 00:08:32 +1300 Subject: [PATCH] net35 hooks, fixed invalid device hook hooks now use .net 3.5. this is because 4.0 will run load 3.5 dll's, but certainly not the other way around. hooking dll retargeted 3.5 because of this. fixed the hook, if an invalid device id is passed and a device is not found then resort to the actual default. --- .../AudioSwitcher.AudioApi.Hooking.csproj | 19 +++++++------- .../ComObjects/Role.cs | 25 +++---------------- AudioSwitcher.AudioApi.Hooking/EntryPoint.cs | 9 ++++--- .../RemoteInterface.cs | 16 ++++-------- Samples/HookingSample/MainWindow.xaml.cs | 9 ++++++- 5 files changed, 31 insertions(+), 47 deletions(-) diff --git a/AudioSwitcher.AudioApi.Hooking/AudioSwitcher.AudioApi.Hooking.csproj b/AudioSwitcher.AudioApi.Hooking/AudioSwitcher.AudioApi.Hooking.csproj index 3f75324..84accef 100644 --- a/AudioSwitcher.AudioApi.Hooking/AudioSwitcher.AudioApi.Hooking.csproj +++ b/AudioSwitcher.AudioApi.Hooking/AudioSwitcher.AudioApi.Hooking.csproj @@ -9,7 +9,7 @@ Properties AudioSwitcher.AudioApi.Hooking AudioSwitcher.AudioApi.Hooking - v4.5.2 + v3.5 512 @@ -43,16 +43,15 @@ AudioSwitcherApi.pfx - + False - ..\lib\EasyHook\NetFX4.0\EasyHook.dll + ..\lib\EasyHook\NetFX3.5\EasyHook.dll - @@ -75,27 +74,27 @@ - + EasyHook32.dll PreserveNewest - + EasyHook32Svc.exe PreserveNewest - + EasyHook64.dll PreserveNewest - + EasyHook64Svc.exe PreserveNewest - + EasyLoad32.dll PreserveNewest - + EasyLoad64.dll PreserveNewest diff --git a/AudioSwitcher.AudioApi.Hooking/ComObjects/Role.cs b/AudioSwitcher.AudioApi.Hooking/ComObjects/Role.cs index b07569a..7171652 100644 --- a/AudioSwitcher.AudioApi.Hooking/ComObjects/Role.cs +++ b/AudioSwitcher.AudioApi.Hooking/ComObjects/Role.cs @@ -1,29 +1,12 @@ using System; -using System.Runtime.InteropServices; namespace AudioSwitcher.AudioApi.Hooking.ComObjects { - /// - /// The ERole enumeration defines constants that indicate the role - /// that the system has assigned to an audio endpoint device - /// [Flags] - [ComVisible(true)] - public enum Role + public enum Role : uint { - /// - /// Games, system notification sounds, and voice commands. - /// - Console, - - /// - /// Music, movies, narration, and live music recording - /// - Multimedia, - - /// - /// Voice communications (talking to another person). - /// - Communications, + Console = 0, + Multimedia = 1, + Communications = 2 } } \ No newline at end of file diff --git a/AudioSwitcher.AudioApi.Hooking/EntryPoint.cs b/AudioSwitcher.AudioApi.Hooking/EntryPoint.cs index 6bc5715..f05f61a 100644 --- a/AudioSwitcher.AudioApi.Hooking/EntryPoint.cs +++ b/AudioSwitcher.AudioApi.Hooking/EntryPoint.cs @@ -1,5 +1,4 @@ using System; -using System.Linq; using System.Runtime.InteropServices; using System.Threading; using AudioSwitcher.AudioApi.Hooking.ComObjects; @@ -62,8 +61,7 @@ public void Run(RemoteHooking.IContext inContext, string inChannelName) } - private static int GetDefaultAudioEndpoint(IMultimediaDeviceEnumerator self, DataFlow dataflow, Role role, - out IntPtr ppendpoint) + private static int GetDefaultAudioEndpoint(IMultimediaDeviceEnumerator self, DataFlow dataflow, Role role, out IntPtr ppendpoint) { var entryPoint = HookRuntimeInfo.Callback as EntryPoint; @@ -75,7 +73,10 @@ private static int GetDefaultAudioEndpoint(IMultimediaDeviceEnumerator self, Dat try { var devId = remoteInterface.GetDefaultDevice(dataflow, role); - return self.GetDevice(devId, out ppendpoint); + + var result = self.GetDevice(devId, out ppendpoint); + if (ppendpoint != IntPtr.Zero) + return result; } catch (Exception ex) { diff --git a/AudioSwitcher.AudioApi.Hooking/RemoteInterface.cs b/AudioSwitcher.AudioApi.Hooking/RemoteInterface.cs index 38de408..fe68ebf 100644 --- a/AudioSwitcher.AudioApi.Hooking/RemoteInterface.cs +++ b/AudioSwitcher.AudioApi.Hooking/RemoteInterface.cs @@ -1,6 +1,5 @@ using System; using System.Threading; -using System.Threading.Tasks; using AudioSwitcher.AudioApi.Hooking.ComObjects; namespace AudioSwitcher.AudioApi.Hooking @@ -58,21 +57,16 @@ public bool HookInstalled() public void HookUninstalled(int processId) { Interlocked.Increment(ref _messageCount); - Task.Run(() => - { - if (_hookUninstalled != null) - _hookUninstalled(processId); - }).ConfigureAwait(false); + + if (_hookUninstalled != null) + _hookUninstalled(processId); } public void ReportError(int processId, Exception e) { Interlocked.Increment(ref _messageCount); - Task.Run(() => - { - if (_errorHandler != null) - _errorHandler(processId, e); - }).ConfigureAwait(false); + if (_errorHandler != null) + _errorHandler(processId, e); } public string GetDefaultDevice(DataFlow dataFlow, Role role) diff --git a/Samples/HookingSample/MainWindow.xaml.cs b/Samples/HookingSample/MainWindow.xaml.cs index ef30bc9..f6586ed 100644 --- a/Samples/HookingSample/MainWindow.xaml.cs +++ b/Samples/HookingSample/MainWindow.xaml.cs @@ -8,6 +8,7 @@ using AudioSwitcher.AudioApi; using AudioSwitcher.AudioApi.CoreAudio; using AudioSwitcher.AudioApi.Hooking; +using Role = AudioSwitcher.AudioApi.Hooking.ComObjects.Role; namespace HookingSample { @@ -183,7 +184,13 @@ private void HookProcess(object sender, RoutedEventArgs e) var sId = SelectedAudioDevice.RealId; - Hook = new DefaultDeviceHook((dataFlow, role) => sId); + Hook = new DefaultDeviceHook((dataFlow, role) => + { + if (role != Role.Communications) + return sId; + + return null; + }); if (Hook.Hook(SelectedProcess.Id)) {