0

I am making a simple console program that checks if headphones are plugged in. There seems to be something wrong with the part where I created an object or instance of Microsoft.DirectX.DirectSound.Speakers called 'soundSpeakers'. I know this because if I take this part away the program doesn't crash.

Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.DirectX.DirectSound;

namespace Headphone_detect
{
    class Program
    {
        static void Main(string[] args)
        {
            bool checkHeadphone = true;
            Speakers soundSpeakers = new Speakers();
            Console.WriteLine("Created object");
            while (checkHeadphone == true)
            {
                Console.WriteLine("While started");
                if (soundSpeakers.Headphone == true)
                {
                    Console.WriteLine("There are headphones plugged in.");
                }
                else
                {
                    Console.WriteLine("There are no headphones plugged in.");
                }
            }
        }
    }
}

I have a bad feeling that I'm doing this completely wrong because when I google how to detect headphones plugged in every single post was seriously complex about how to solve it. Also, I have added a reference to Microsoft.DirectX.DirectSound in Visual Studio.

I forgot to mention that my error kid is as follows:

Unhandled Exception: System.IO.FileLoadException: Mixed mode assembly is built against version 'v1.1.4322' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information. at Headphone_detect.Program.Main(String[] args)

Dom
  • 1,232
  • 1
  • 10
  • 20
  • Stacktrace and error message please. – Orphid Mar 09 '16 at 18:56
  • 1
    Did you search for the error message? https://stackoverflow.com/q/4018924/1925996 – piedar Mar 09 '16 at 19:36
  • @piedar seems to have it - in visual studio, open your app.config file and make the amendments suggested by Reed Copsey and Gustavo Mori in the question piedar linked to. – Orphid Mar 09 '16 at 19:46

2 Answers2

0

Searching for the message in your exception returns the following SO answer:

Mixed mode assembly is built against version 'v1.1.4322'

You need to add an app.Config file and set useLegacyV2RuntimeActivationPolicy to true.

This is required to use mixed mode CLR 2 assemblies in a .NET 4 application.

Community
  • 1
  • 1
technophile
  • 3,556
  • 1
  • 20
  • 24
-2

You can temporarily add an exception block to your code with a messagebox show:

try
{
    ... (your existing code)
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

Or, you could create a handler for unhandled exceptions and rely on that. To do so, make your project's Program.cs look something like this:

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    **AppDomain.CurrentDomain.UnhandledException += Unhandled;**
    Application.Run(new FormMain());
}

static void Unhandled(object sender, UnhandledExceptionEventArgs exArgs)
{
    // log the exception, display it, put a breakpoint here, whatever
}

Note, too, that "== true" is redundant when testing booleans; you can simply use "if (soundSpeakers.Headphone)" instead (and "if (!soundSpeakers.Headphone)" for the opposite).

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 2
    Umm... this doesn't answer the question of what is wrong. Anyone coming to this answer in days to come will find it unrelated to the problem of "an unhandled exception when trying to use directx-directsound to detect if headphones are plugged in". You've also included a bunch of winforms code which doesn't seem relevant to the question. -1 – Orphid Mar 09 '16 at 19:07