Some platforms require COM to be initialized Fixes #463

- create a helper class
 - used primarily for XPS
This commit is contained in:
Matthew Leibowitz 2018-03-24 00:13:34 +02:00
Родитель c4ce9ab594
Коммит d3a54285a8
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 3650EBE4AA155AF9
3 изменённых файлов: 51 добавлений и 1 удалений

Просмотреть файл

@ -12,6 +12,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Definitions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)GRGlInterface.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Properties\SkiaSharpAssemblyInfo.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SKAutoCoInitialize.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SKImageInfo.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SKColor.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SKColorTable.cs" />

Просмотреть файл

@ -0,0 +1,47 @@
using System;
using System.Runtime.InteropServices;
namespace SkiaSharp
{
public class SKAutoCoInitialize : IDisposable
{
private long hResult;
public SKAutoCoInitialize()
{
if (PlatformConfiguration.IsWindows)
hResult = CoInitializeEx(IntPtr.Zero, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
else
hResult = S_OK;
}
public bool Initialized => hResult >= 0 || hResult == RPC_E_CHANGED_MODE;
public void Uninitialize()
{
if (hResult >= 0)
{
if (PlatformConfiguration.IsWindows)
CoUninitialize();
hResult = -1;
}
}
public void Dispose() => Uninitialize();
private const long S_OK = 0x00000000L;
private const long RPC_E_CHANGED_MODE = 0x80010106L;
private const uint COINIT_MULTITHREADED = 0x0;
private const uint COINIT_APARTMENTTHREADED = 0x2;
private const uint COINIT_DISABLE_OLE1DDE = 0x4;
private const uint COINIT_SPEED_OVER_MEMORY = 0x8;
[DllImport("ole32.dll", CharSet = CharSet.Ansi, SetLastError = true, CallingConvention = CallingConvention.StdCall)]
private static extern long CoInitializeEx([In, Optional] IntPtr pvReserved, [In] uint dwCoInit);
[DllImport("ole32.dll", CharSet = CharSet.Ansi, SetLastError = true, CallingConvention = CallingConvention.StdCall)]
private static extern void CoUninitialize();
}
}

Просмотреть файл

@ -49,7 +49,8 @@ namespace SkiaSharp.Tests
{
var path = Path.Combine(PathToImages, Guid.NewGuid().ToString("D") + ".xps");
using (var doc = SKDocument.CreatePdf(path))
using (new SKAutoCoInitialize())
using (var doc = SKDocument.CreateXps(path))
{
if (IsWindows)
{
@ -92,6 +93,7 @@ namespace SkiaSharp.Tests
using (var stream = new MemoryStream())
using (var managed = new SKManagedWStream(stream, false))
{
using (new SKAutoCoInitialize())
using (var doc = SKDocument.CreateXps(managed))
{
if (IsWindows)