Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
572 views
in Technique[技术] by (71.8m points)

pinvoke - 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#?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...