Last active
August 8, 2020 20:48
-
-
Save mattleibow/33f54a166cd6bdcad8580b1f43e45077 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
namespace ConsoleApp1 | |
{ | |
unsafe class Program | |
{ | |
private static readonly IntPtr libSkiaSharp; | |
static Program() | |
{ | |
libSkiaSharp = Win32.LoadLibrary(@"C:\Users\mattl\.nuget\packages\skiasharp\2.80.1\runtimes\win-x86\native\libSkiaSharp.dll"); | |
} | |
static void Main(string[] args) | |
{ | |
var font = sk_font_new(); | |
var res = sk_font_measure_text(font, null, IntPtr.Zero, 0, IntPtr.Zero, IntPtr.Zero); | |
Console.WriteLine(res); | |
} | |
internal static IntPtr sk_font_new() | |
{ | |
var proc = Win32.GetProcAddress(libSkiaSharp, "sk_font_new"); | |
var del = Marshal.GetDelegateForFunctionPointer<Delegates.sk_font_new>(proc); | |
return del.Invoke(); | |
} | |
internal static float sk_font_measure_text(IntPtr font, void* text, IntPtr byteLength, int encoding, IntPtr bounds, IntPtr paint) | |
{ | |
var proc = Win32.GetProcAddress(libSkiaSharp, "sk_font_measure_text"); | |
var del = Marshal.GetDelegateForFunctionPointer<Delegates.sk_font_measure_text>(proc); | |
return del.Invoke(font, text, byteLength, encoding, bounds, paint); | |
} | |
private partial class Delegates | |
{ | |
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] | |
internal delegate IntPtr sk_font_new(); | |
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] | |
internal delegate Single sk_font_measure_text(IntPtr font, void* text, IntPtr byteLength, int encoding, IntPtr bounds, IntPtr paint); | |
} | |
} | |
public static class Win32 | |
{ | |
private const string SystemLibrary = "Kernel32.dll"; | |
[DllImport(SystemLibrary, SetLastError = true, CharSet = CharSet.Ansi)] | |
public static extern IntPtr LoadLibrary(string lpFileName); | |
[DllImport(SystemLibrary, SetLastError = true, CharSet = CharSet.Ansi)] | |
public static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName); | |
[DllImport(SystemLibrary, SetLastError = true, CharSet = CharSet.Ansi)] | |
public static extern void FreeLibrary(IntPtr hModule); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment