CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2008
    Posts
    78

    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

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Recognize struct members

    Is the pointer that is passed a pointer to an array of structs?

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    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.

  4. #4
    Join Date
    May 2007
    Posts
    1,546

    Re: Recognize struct members

    Quote Originally Posted by BigEd781 View Post
    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.

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    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
  •  





Click Here to Expand Forum to Full Width

Featured