Skip to content

Instantly share code, notes, and snippets.

@fredemmott
Last active July 5, 2024 11:40
Show Gist options
  • Save fredemmott/7a4d4f8584c7f217977fd39cebc98dba to your computer and use it in GitHub Desktop.
Save fredemmott/7a4d4f8584c7f217977fd39cebc98dba to your computer and use it in GitHub Desktop.
using System.Runtime.InteropServices;
using System.Text;
public partial class Program
{
[LibraryImport("OpenKneeboard_CAPI64")]
private static partial void OpenKneeboard_send_utf8(byte[] messageName, UInt64 messageNameBytes, byte[] messageData, UInt64 messageDataBytes);
public static void Main(string[] args)
{
// This is just an example dynamic path; in a real program, follow all the documented steps on the OpenKneeboard web site
var path = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + "\\OpenKneeboard\\bin\\OpenKneeboard_CAPI64";
Console.WriteLine("Path: " + path);
// This is the easy way.
// - Correct way is https://learn.microsoft.com/en-us/dotnet/standard/native-interop/native-library-loading#custom-import-resolver
// - Alternative way on .Net < 5: P/Invoke `LoadLibraryW()` first
NativeLibrary.Load(path);
// You probably also want to call `NativeLibrary.Free(return value from above)` when your code is cleaning up.
// Not a real supported message, but will show up in OpenKneeboard's "API events" log
var utf8 = Encoding.UTF8;
var messageType = utf8.GetBytes("Hello");
var messageData = utf8.GetBytes("World");
OpenKneeboard_send_utf8(messageType, Convert.ToUInt64(messageType.Length), messageData, Convert.ToUInt64(messageData.Length));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment