Skip to content

Commit

Permalink
Merge pull request #168 from XScorpion2/set_default_device_api
Browse files Browse the repository at this point in the history
Implemented IPolicyConfig interface for set default device
  • Loading branch information
t3knomanzer authored Sep 6, 2020
2 parents 7f1586d + a64c452 commit 82b2424
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
1 change: 1 addition & 0 deletions Desktop/Application/MaxMix/MaxMix.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
<Compile Include="Services\Communication\Message\MessageRemoveSession.cs" />
<Compile Include="Services\Communication\Message\MessageSettings.cs" />
<Compile Include="Services\Communication\Message\MessageUpdateVolumeSession.cs" />
<Compile Include="Services\Audio\PolicyConfig.cs" />
<Compile Include="ViewModels\BaseViewModel.cs" />
<Compile Include="ViewModels\MainViewModel.cs" />
<Compile Include="ViewModels\SettingsViewModel.cs" />
Expand Down
9 changes: 8 additions & 1 deletion Desktop/Application/MaxMix/Services/Audio/AudioExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using CSCore.CoreAudioAPI;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
Expand Down Expand Up @@ -33,5 +34,11 @@ public static string GetProductName(this Process process)
var versionInfo = FileVersionInfo.GetVersionInfo(fileName);
return versionInfo.ProductName;
}

public static void SetDefaultEndpoint(string deviceID, Role role)
{
var policyConfig = new PolicyConfig();
policyConfig.SetDefaultEndpoint(deviceID, role);
}
}
}
55 changes: 55 additions & 0 deletions Desktop/Application/MaxMix/Services/Audio/PolicyConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using CSCore.CoreAudioAPI;
using CSCore.Win32;
using System;
using System.Runtime.InteropServices;

namespace MaxMix.Services.Audio
{
[Guid("8F9FB2AA-1C0B-4D54-B6BB-B2F2A10CE03C")]
class PolicyConfig : ComObject
{
public PolicyConfig() : base(CreatePolicyConfig()) { }

private static IntPtr CreatePolicyConfig()
{
var obj = new PolicyConfigObject();
if (obj is IPolicyConfig c)
return Marshal.GetComInterfaceForObject(c, typeof(IPolicyConfig));
throw new NotSupportedException("Unable to create PolicyConfig on this OS.");
}

public void SetDefaultEndpoint(string id, Role role)
{
var obj = Marshal.GetObjectForIUnknown(BasePtr);
int result = unchecked((int)0x80090011); // Object not found error
if (obj is IPolicyConfig c)
result = c.SetDefaultEndpoint(id, role);
Marshal.ThrowExceptionForHR(result);
}

[ComImport]
[Guid("870AF99C-171D-4F9E-AF0D-E63DF40C2BC9")]
private class PolicyConfigObject
{
}
}

[ComImport]
[Guid("F8679F50-850A-41CF-9C72-430F290290C8")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IPolicyConfig : IUnknown
{
void Unused1();
void Unused2();
void Unused3();
void Unused4();
void Unused5();
void Unused6();
void Unused7();
void Unused8();
void Unused9();
void Unused10();
int SetDefaultEndpoint([In, MarshalAs(UnmanagedType.LPWStr)] string id, Role role);
void Unused12();
}
}

0 comments on commit 82b2424

Please sign in to comment.