簡體   English   中英

C#調用非托管代碼:“無法從'CustomClass'轉換為'ref System.IntPtr'”

[英]C# calling unmanaged code: “Cannot convert from 'CustomClass' to 'ref System.IntPtr'”

我在c#中為c ++庫編寫包裝函數。 我在c ++中的函數采用兩個指針作為參數,它更改其中一個指針指向( foo )的內容。 所以在C#中,我需要將此作為ref傳遞給我? 我有:

C ++:

ErrorCode MyFunction(void* dev, void* foo);

C#:

[DllImport("MyLib.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern ExceptionClass.ErrorCode MyFunction(IntPtr dev, ref IntPtr foo);

當我像這樣在C#中調用函數時

public void CallFunction(DevClass dev, FooClass foo)
{
  MyFunction(dev, ref foo);
}

我得到了錯誤

Error 3 Argument 2: cannot convert from 'ref FooClass' to 'ref System.IntPtr'

我認為您不必為第二個參數foo放置引用

根據MSDN

IntPtr類型可由支持指針的語言使用,並作為在支持和不支持指針的語言之間引用數據的常用方式。

因此,IntPtr就像一個指針,您可以修改它指向的數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM