簡體   English   中英

c#使用帶有DllImport的const

[英]c# using const with DllImport

我有一個SDK的代碼示例,DLL是用C編寫的,並調用以下內容:

int getAttribute(const RawDevControl * control,RawDevAttribute * attribute)

我正在使用

[DllImport(@"Dev.dll",
SetLastError = true)]
internal static extern int getAttribute(const RControl *control, RAttribute *attribute);

但是當然在定義此引用函數時不能使用const作為類型。

我怎樣才能用c#做這個工作?

由於C#沒有const引用的概念,因此您不必擔心它。 在DLL方面,代碼仍然認為你有一個const指針。 因此,您的導入更改為:

[DllImport(@"Dev.dll", SetLastError = true)]
internal static extern int getAttribute(RControl control, RAttribute attribute);

當然,這假設RControlRAttribute都已在C#中定義。 如果它們是結構體,請按照MSDN上的示例來定義與P / Invoke一起使用的結構。 如果他們是班級,那就是一組不同的問題。 在這種情況下,最好是類是基於COM的。

聲明承諾該函數不會改變傳遞的對象。 因此,利用這一點,告訴pinvoke marshaller將復制它沒有意義。 應用[In]屬性:

[DllImport(@"Dev.dll", SetLastError = true)]
static extern int getAttribute([In] ref RControl control, out RAttribute attribute);

如果將參數類型聲明為類而不是結構,則省略refout關鍵字。

暫無
暫無

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

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