COM Interop in Mono 2.0
Asked Answered
T

2

9

I am trying to use this code in a Unity project, but it seems the implementations of COM Interop in Mono/.NET differs, which causes the code to fail or crash. Running the code in .NET works fine, but running it with Mono 2.0 (outside of Unity) fails in the same way as in Unity, suggesting it is a problem with Mono in general and not Unity.

If I compile and run the code as-is, it fails because the type cast from MMDeviceEnumerator to IIMMDeviceEnumerator fails. When decorating all interfaces with [ComInterop], the cast succeeds, but the call to GetDefaultAudioEndpoint crashes Unity/Mono with an Access Violation.

It is hard to find good documentation of COM Interop on Mono in general - and particularly so regarding such an old version. Is it at all possible to get this running?

Tubular answered 14/2, 2017 at 10:29 Comment(0)
S
1

Wrap the COM functions in C funtions and call the C functions via P/Invoke instead. This can be done in two steps:

  1. Create a VC++ project that wraps the functions you needed in wasapi. Expose them via a module define file or __declspec(dllexport). Build the code into a dll that exposes the functions you need.
  2. In your Unity3D project, access them via P/Invoke.

Here is an example. In your case, just use the COM code in the C/C++ part to do what you want.

Shaker answered 21/2, 2017 at 15:22 Comment(1)
That was exactly what I ended up doingTubular
T
-3

Mono 1.0 and Mono 1.1.xx do not have support for COM.

Stop trying with Mono, Mono is for platform independence and COM Interop is Microsoft only. Use open source SDKs for Video Playing or better invoke apps from command line like vlc to play, encode etc.

Teflon answered 24/2, 2017 at 7:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.