CoreAudioApi with threading - InvalidCastException

2

I'm writing an application to modify the volume of another application and using the CoreAudioApi dll to do so. The following block of code works perfectly on the main thread, but when called in a separate thread, an InvalidCastException is raised. I threw in a Mutex in case this was just an issue of two threads trying to access the same resource, but that doesn't appear to be the case. Any ideas as to what the problem might be? I'm stumped as a new C# programmer. I read other questions where its suggested that only the main thread has access to COM objects such as CoreAudioApi, so I need to send some kind of message to the main thread. If this is correct, what is the best way to do this?

Exception (occurs on the first Math.abs if statement):

Unable to cast COM object of type 'System.__ComObject' to interface type 'CoreAudioApi.Interfaces.IMMDevice'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{D666063F-1587-4E43-81F1-B948E807363F}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

Code:

muteSoundMutex.WaitOne();
AudioSessionControl sASC = sInfo.getSAudioSession();
if (Math.Abs(sASC.SimpleAudioVolume.MasterVolume -
      (0.05f / defaultDevice.AudioEndpointVolume.MasterVolumeLevelScalar)) < 0.0001 
    && savedVol > 0)
{
   ... // other code here. all mutexes are released correctly.
c#
multithreading
com
.net
asked on Stack Overflow Dec 24, 2012 by Ben • edited Dec 25, 2012 by VMAtm

2 Answers

2

Your error message tells about casting error during some operation with IMMDevice.
According this spec, IMMDevice has only 4 methods:

  • Activate
  • OpenPropertyStore
  • GetId
  • GetState

The code you've provided has no mention of these methods, so either you've misunderstand where is your error occurs, or this is internal error raised up to your application.

I think this is an internal error during getting the Volume from your devices.

If this error occurs into the sASC.SimpleAudioVolume.MasterVolume or defaultDevice.AudioEndpointVolume.MasterVolumeLevelScalar, then this error because of code is running into background mode. Otherwise, please add some code and stacktrace for your exception.

Error with error code E_NOINTERFACE can occur during Activate method for the device. So you need call this method into your main thread to activate device, and after that try to use it into your background mode. Can't provide any sample code, sorry.

answered on Stack Overflow Dec 25, 2012 by VMAtm • edited Dec 25, 2012 by VMAtm
0

My app was throwing this exception. I've gathered some insight about this.

  • The COM object requires all thread to run
  • If you're trying to run this object from another thread be sure that thread is running
  • If you program is running on STAThread and you are using this COM Object into a Task or another Thread then it will throw an exception like this.

So Please be sure that your program is running under MTAThread while using the COM object into a Task or Thread.

Hope this helps.

Thank you.

answered on Stack Overflow Jun 7, 2020 by Sajeeb Chandan

User contributions licensed under CC BY-SA 3.0