Skip to content

Commit

Permalink
net35 hooks, fixed invalid device hook
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
xenolightning committed Mar 1, 2016
1 parent c59aca2 commit e89d4e3
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>AudioSwitcher.AudioApi.Hooking</RootNamespace>
<AssemblyName>AudioSwitcher.AudioApi.Hooking</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
Expand Down Expand Up @@ -43,16 +43,15 @@
<AssemblyOriginatorKeyFile>AudioSwitcherApi.pfx</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="EasyHook, Version=2.7.0.0, Culture=neutral, PublicKeyToken=4b580fca19d0b0c5, processorArchitecture=MSIL">
<Reference Include="EasyHook, Version=2.7.5870.0, Culture=neutral, PublicKeyToken=4b580fca19d0b0c5, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\lib\EasyHook\NetFX4.0\EasyHook.dll</HintPath>
<HintPath>..\lib\EasyHook\NetFX3.5\EasyHook.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.Remoting" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
Expand All @@ -75,27 +74,27 @@
<None Include="AudioSwitcherApi.pfx" />
</ItemGroup>
<ItemGroup>
<Content Include="..\lib\EasyHook\NetFX4.0\EasyHook32.dll">
<Content Include="..\lib\EasyHook\NetFX3.5\EasyHook32.dll">
<Link>EasyHook32.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="..\lib\EasyHook\NetFX4.0\EasyHook32Svc.exe">
<Content Include="..\lib\EasyHook\NetFX3.5\EasyHook32Svc.exe">
<Link>EasyHook32Svc.exe</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="..\lib\EasyHook\NetFX4.0\EasyHook64.dll">
<Content Include="..\lib\EasyHook\NetFX3.5\EasyHook64.dll">
<Link>EasyHook64.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="..\lib\EasyHook\NetFX4.0\EasyHook64Svc.exe">
<Content Include="..\lib\EasyHook\NetFX3.5\EasyHook64Svc.exe">
<Link>EasyHook64Svc.exe</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="..\lib\EasyHook\NetFX4.0\EasyLoad32.dll">
<Content Include="..\lib\EasyHook\NetFX3.5\EasyLoad32.dll">
<Link>EasyLoad32.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="..\lib\EasyHook\NetFX4.0\EasyLoad64.dll">
<Content Include="..\lib\EasyHook\NetFX3.5\EasyLoad64.dll">
<Link>EasyLoad64.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down
25 changes: 4 additions & 21 deletions AudioSwitcher.AudioApi.Hooking/ComObjects/Role.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
using System;
using System.Runtime.InteropServices;

namespace AudioSwitcher.AudioApi.Hooking.ComObjects
{
/// <summary>
/// The ERole enumeration defines constants that indicate the role
/// that the system has assigned to an audio endpoint device
/// </summary>
[Flags]
[ComVisible(true)]
public enum Role
public enum Role : uint
{
/// <summary>
/// Games, system notification sounds, and voice commands.
/// </summary>
Console,

/// <summary>
/// Music, movies, narration, and live music recording
/// </summary>
Multimedia,

/// <summary>
/// Voice communications (talking to another person).
/// </summary>
Communications,
Console = 0,
Multimedia = 1,
Communications = 2
}
}
9 changes: 5 additions & 4 deletions AudioSwitcher.AudioApi.Hooking/EntryPoint.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using AudioSwitcher.AudioApi.Hooking.ComObjects;
Expand Down Expand Up @@ -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;

Expand All @@ -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)
{
Expand Down
16 changes: 5 additions & 11 deletions AudioSwitcher.AudioApi.Hooking/RemoteInterface.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using AudioSwitcher.AudioApi.Hooking.ComObjects;

namespace AudioSwitcher.AudioApi.Hooking
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 8 additions & 1 deletion Samples/HookingSample/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using AudioSwitcher.AudioApi;
using AudioSwitcher.AudioApi.CoreAudio;
using AudioSwitcher.AudioApi.Hooking;
using Role = AudioSwitcher.AudioApi.Hooking.ComObjects.Role;

namespace HookingSample
{
Expand Down Expand Up @@ -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))
{
Expand Down

0 comments on commit e89d4e3

Please sign in to comment.