Created
November 25, 2015 00:44
-
-
Save natemcmaster/c50687b8e2812cbe6903 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.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); | |
} | |
} | |
} |
This file contains hidden or 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
{ | |
"frameworks": { | |
"dnx451": {} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment