Some platforms require COM to be initialized Fixes #463
- create a helper class - used primarily for XPS
This commit is contained in:
Родитель
c4ce9ab594
Коммит
d3a54285a8
|
@ -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)
|
||||
|
|
Загрузка…
Ссылка в новой задаче