Alternatively, you can come up with a solution that generates PInvoke methods dynamically.
open System
open System.Reflection
open System.Reflection.Emit
open System.Runtime.InteropServices
let assembly = AppDomain.CurrentDomain.DefineDynamicAssembly (new AssemblyName ("PInvokeLibrary"), AssemblyBuilderAccess.Run)
let module_builder = assembly.DefineDynamicModule ("PInvokeLibrary")
let define_dynamic_pinvoke<'d when 'd :> Delegate> (name, library) =
let invoke = typeof<'d>.GetMethod ("Invoke") (* signature of delegate 'd *)
let parameters =
invoke.GetParameters ()
|> Array.map (fun p -> p.ParameterType)
let type_builder = module_builder.DefineType (name, TypeAttributes.Public)
let method_builder =
type_builder.DefinePInvokeMethod (
name,
library,
MethodAttributes.Public ||| MethodAttributes.Static ||| MethodAttributes.PinvokeImpl,
CallingConventions.Standard,
invoke.ReturnType,
parameters,
CallingConvention.Winapi,
CharSet.Ansi)
method_builder.SetImplementationFlags (method_builder.GetMethodImplementationFlags () ||| MethodImplAttributes.PreserveSig)
let result_type = type_builder.CreateType ()
let pinvoke = result_type.GetMethod (name)
Delegate.CreateDelegate (typeof<'d>, pinvoke) :?> 'd
let beep = define_dynamic_pinvoke<Func<int, int, bool>> ("Beep", "kernel32.dll")
beep.Invoke (800, 100)