|
-
August 23rd, 2011, 02:51 AM
#1
Recognize struct members
Hello,
I am making a class library in C#. The purpose of this library is to receive a struct as input (from other C program which is written in LabWindows), recognize its members and return a string which is built using these members.
For e.g.
The struct is:
Code:
typedef struct
{
uint32_t Weight;
double Velocity;
}UCAPP_Property_t;
The function in C# should look like this:
Code:
public CreateWriteString(void* aPointerToStruct, string writeString)
{
for(int i = 0; i < numberOfElements in aPointerToStruct; i++)
writeStringBuilder.Append(ToString(*(aPointerToStruct + i));
writeString = writeStringBuilder.ToString();
}
Since I can not use pointer in C#, I have no idea how to do this. I have to do this in C# because of existing library which is in C#.
Regards
RB
-
August 24th, 2011, 12:42 PM
#2
Re: Recognize struct members
Is the pointer that is passed a pointer to an array of structs?
-
August 24th, 2011, 01:29 PM
#3
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.
-
August 24th, 2011, 03:04 PM
#4
Re: Recognize struct members
 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.
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.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
August 24th, 2011, 03:56 PM
#5
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|