Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert out/ref extern parameters to F#

I've got a C# extern declaration that goes like this:

    [DllImport("something.dll")]
    public static extern ReturnCode GetParent(IntPtr inRef, out IntPtr outParentRef);

How to translate that to F#?

like image 525
Dax Fohl Avatar asked Jul 02 '11 18:07

Dax Fohl


2 Answers

You can try something like the code below. I don't know what ReturnCode is, so the code below expects it is an integer. For any more complex type, you'll need to use [<Struct>] attribute as in the answer referenced by A-Dubb.

type ReturnCode = int

[<System.Runtime.InteropServices.DllImport("something.dll")>]
extern ReturnCode GetParent(System.IntPtr inRef, System.IntPtr& outParentRef);

To call the function, you'd write something like this:

let mutable v = nativeint 10
let n = GetParent(nativeint 0, &v)

BTW: Could you also post a sample C code that implements the function in something.dll? If yes, we could try running the solution before sending an answer...

like image 179
Tomas Petricek Avatar answered Sep 23 '22 02:09

Tomas Petricek


Maybe this similar question will point you in the right direction. Looks like he used attributes at the parameter level for "in" and "out" F# syntax for P/Invoke signature using MarshalAs

like image 40
A-Dubb Avatar answered Sep 25 '22 02:09

A-Dubb