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.