I have a function in C that accepts a hexadecimal parameter. I need to call this function from C#. My current approach does not seem right because my C function is returning me the wrong number.
Here is the declaration of my C function:
enum tags {
TAG_A = -1,
TAG_B = 0x00,
TAG_C = 0xC1,
...
};
int myfunction(enum tags t);
Here is my C# code:
enum tags {
TAG_A = -1,
TAG_B = 0x00,
TAG_C = 0xC1,
...
}
[DllImport ("mylibraryname")]
public static extern int myfunction(tags t);
myfunction(tags.TAG_B);
I am on Mac and I am using Mono and Xcode to do all of these work. The C function can be assumed correct because it’s an open-source library I downloaded. I think it’s something wrong with the hexadecimal numbers but I’m not sure.
solution:
I’ve ticked one answer, though actually setting the C# enum to long solved my problem. So in C#, I have:
enum tags : long {
TAG_A = -1,
TAG_B = 0x00,
TAG_C = 0xC1,
…
}
Hexadecimal is simply a different way of expressing a literal integer value. It’s irrelevant to your problem. For instance
TAG_B = 0x00andTAG_B = 0both mean exactly the same thing.The problem is possibly that the C
enumis a 16 bit integer, whereas the C#enumis 32 bit. Instead of creating an enum in C#, try just doing it as straightInt32values:Or, as L.B suggested, you can just set the type of the
enummembers: