dotnet / corert

This repo contains CoreRT, an experimental .NET Core runtime optimized for AOT (ahead of time compilation) scenarios, with the accompanying compiler toolchain.
http://dot.net
MIT License
2.91k stars 511 forks source link

COM: CoInitialize has not been called #6046

Open sa-exe opened 6 years ago

sa-exe commented 6 years ago

Now that #5587 is resolved, there's only a few issues left that prevent me from using corert for my project. This one also happens to be related to the usage of SharpDX. Here's a simple repro (that you can download here):

using SharpDX.XAudio2;

namespace XAudioSample
{
    class Program
    {
        static void Main(string[] args)
        {
            var device = new XAudio2(XAudio2Flags.None, ProcessorSpecifier.DefaultProcessor);
            var masteringVoice = new MasteringVoice(device);
        }
    }
}
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="SharpDX.XAudio2" Version="4.0.1" />
  </ItemGroup>

  <ItemGroup>
    <!--1.0.0-alpha-26703-02 was the latest build at the time of writing this-->
    <PackageReference Include="Microsoft.DotNet.ILCompiler" Version="1.0.0-alpha-*" />
  </ItemGroup>

</Project>

Stack trace:

Unhandled Exception: SharpDX.SharpDXException: HRESULT: [0x800401F0], Module: [Unknown], ApiCode: [Unknown/Unknown], Message: CoInitialize has not been called.

   at SharpDX.Result.CheckError() + 0x2e
   at SharpDX.XAudio2.XAudio2.CreateMasteringVoice(MasteringVoice, Int32, Int32, Int32, String, Nullable`1, AudioStreamCategory) + 0x149
   at SharpDX.XAudio2.MasteringVoice..ctor(XAudio2, Int32, Int32) + 0xcf
   at XAudioSample.Program.Main(String[]) + 0x3b
   at XAudioSample!<BaseAddress>+0x15ee66

I believe this is the line where it crashes.

jkotas commented 6 years ago

CoreRT does not initialize COM currently. You should be able to workaround this by manually PInvoking to CoInitialize or CoInitializeEx for now.

sa-exe commented 6 years ago

I see. So it's not really a bug. It's just that I have previously used another COM based API -- WIC, without any issues, and now I'm getting the same exception. Anyway, feel free to close the issue.