Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# using const with DllImport

I have a code example from a SDK, the DLL is written in C and calls for the following:

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

I'm using

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

But of course you can not use const as a type when defining this reference function.

How can I make this work with c#?

like image 683
Clu Avatar asked Oct 19 '25 05:10

Clu


1 Answers

Since C# doesn't have the concept of const references, you don't really need to worry about it. On the DLL side, the code will still think you have a const pointer. Thus, your import changes to this:

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

This, of course, assumes that both RControl and RAttribute have been defined in C#. If they are structs, follow the examples on MSDN for defining structs for use with P/Invoke. If they are classes, that's a different set of problems. In that case, it is best if the classes are COM-based.

like image 176
hypercode Avatar answered Oct 21 '25 20:10

hypercode



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!