2011-07-02 13 views

回答

12

您可以嘗試類似下面的代碼。我不知道ReturnCode是什麼,所以下面的代碼期望它是一個整數。對於任何更復雜的類型,您需要使用[<Struct>]屬性,如A-Dubb引用的答案中所述。

type ReturnCode = int 

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

要調用的函數,你會寫是這樣的:

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

BTW:你也可以張貼實現了something.dll功能的示例C代碼?如果是的話,我們可以嘗試發送答案之前運行的解決方案......

+0

很好用!但是 - 舊的C#聲明允許我做「let(n,v)= GetParent輸入」。任何方式來修改代碼以允許該語法? –

+0

哦,並且ReturnCode只是一個基於uint32的枚舉。 –

+0

似乎與CIL中的「hidebysig」裝飾器有關。在紅色門反射器中,C#方法擁有它,而F#方法則沒有。 F#提供hidebysig嗎? (我仍然試圖弄清楚甚至意味着什麼)。 –

1

也許這個類似的問題會指出你在正確的方向。看起來他在參數級使用「in」和「out」屬性F# syntax for P/Invoke signature using MarshalAs

+0

不,不工作 –

+0

通過不工作,你的意思是它不編譯? –

+0

是的。至少我是這樣解釋的。 –

0

對於其他人嘗試通過的PInvoke使用F#與EnvDte這可能會幫助:

[<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); 

這顯然是不正確略,但似乎上班。定義應該是:

[<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);