How to convert out/ref extern parameters to F#

冷暖自知 提交于 2019-11-28 11:21:27

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

A-Dubb

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

For anyone else trying to use F# with EnvDte via PInvoke this may help:

[<System.Runtime.InteropServices.DllImport("ole32.dll")>] 
extern unit CreateBindCtx(System.IntPtr inRef, IBindCtx& outParentRef);
[<System.Runtime.InteropServices.DllImport("ole32.dll")>]
extern unit GetRunningObjectTable(System.IntPtr inRef, IRunningObjectTable& outParentRef);

which apparently is slightly incorrect, but appears to work. the definition should be:

[<System.Runtime.InteropServices.DllImport("ole32.dll")>] 
extern int CreateBindCtx(System.IntPtr inRef, IBindCtx& outParentRef);
[<System.Runtime.InteropServices.DllImport("ole32.dll")>]
extern int GetRunningObjectTable(System.IntPtr inRef, IRunningObjectTable& outParentRef);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!