Skip to content

Instantly share code, notes, and snippets.

@natemcmaster
Created November 25, 2015 00:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natemcmaster/c50687b8e2812cbe6903 to your computer and use it in GitHub Desktop.
Save natemcmaster/c50687b8e2812cbe6903 to your computer and use it in GitHub Desktop.
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace Sqlite3Test
{
public class Program
{
public static void Main()
{
Console.WriteLine("Loaded libsqlite3 version: " + NativeMethod.sqlite3_libversion() );
}
}
public static class NativeMethod
{
[DllImport("sqlite3", EntryPoint = "sqlite3_libversion", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr sqlite3_libversion_raw();
public static string sqlite3_libversion() => PtrToStringUTF8(sqlite3_libversion_raw());
public static string PtrToStringUTF8(IntPtr ptr)
{
if (ptr == IntPtr.Zero)
{
return null;
}
var i = 0;
while (Marshal.ReadByte(ptr, i) != 0)
{
i++;
}
var bytes = new byte[i];
Marshal.Copy(ptr, bytes, 0, i);
return Encoding.UTF8.GetString(bytes, 0, i);
}
}
}
{
"frameworks": {
"dnx451": {}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment