Re: Recognize struct members
Is the pointer that is passed a pointer to an array of structs?
Re: Recognize struct members
You actually can use pointers in C#, but you don't need to. You need to declare a struct in C# that maps to the same memory layout as the C struct does.
Code:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
struct UcappProperty
{
UInt32 Weight;
double Velocity;
}
Also, as Arjay points out, if you are expecting a pointer to a collection of C structures then your C# signature should take an array.
Re: Recognize struct members
Quote:
Originally Posted by
BigEd781
You actually can use pointers in C#, but you don't need to. You need to declare a struct in C# that maps to the same memory layout as the C struct does.
Code:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
struct UcappProperty
{
UInt32 Weight;
double Velocity;
}
LayoutKind.Sequential is the default for structs and CharSet.Ansi may not be the correct thing to put there. You'd have to check how the actual native library is compiled and what it expects.
Quote:
Also, as Arjay points out, if you are expecting a pointer to a collection of C structures then your C# signature should take an array.
That may not work. It depends on the native function.
The only way to answer this question is to see the native function and the managed P/Invoke.
Re: Recognize struct members
Well yes, it will definitely depend on the C prototype. Sequential and ANSI work for most C compilers using default settings for release and debug, but sure, they may have to change as well. If needed you can use LayoutKind.Explicit and set the offsets for each member yourself.