char* in C struct mapping to C# struct

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vikas

    #1

    char* in C struct mapping to C# struct

    Hi
    I have been browsing C struct to C# mapping emails on the newsgroups,
    but I haven't been able to find a solution to my problem.

    My C structure looks like this:

    struct myCstruct {
    char * data;
    size_t size;
    };

    I want to directly map it into C# as a structure there. But all the
    emails have examples of mapping constant size character arrays (usually
    as strings). This I would like to map as a byte[].

    The aim is that C provides a function callback and passes to the
    callback this structure. Then C# uses public delegate and directly
    invokes that callback.

    I can insert another C callback or function that changes the arguments
    to the C callback and splits the structure into separate char* and
    size_t elements, but that is not my intention.
    I want to directly call C# from C.

    Any help will be appreciated.

    Thanks and regards,
    Vikas

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: char* in C struct mapping to C# struct

    Vikas,

    Your struct will look like this:

    [StructLayout(La youtKind.Sequen tial)]
    struct myCstruct
    {
    public IntPtr data;
    public int size;
    }

    You will have to manually marshal the data that you want, I believe.

    You ^might^ be able to get away with declaring it as a string, but I am
    not sure if .NET handles the memory allocation for you (and subsequent
    teardown). To be honest, it doesn't come up much because most structures
    are self-contained in that respect.

    Of course, it is easy enough to try.

    Hope this helps.

    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Vikas" <walburn@gmail. comwrote in message
    news:1158936733 .172931.33700@b 28g2000cwb.goog legroups.com...
    Hi
    I have been browsing C struct to C# mapping emails on the newsgroups,
    but I haven't been able to find a solution to my problem.
    >
    My C structure looks like this:
    >
    struct myCstruct {
    char * data;
    size_t size;
    };
    >
    I want to directly map it into C# as a structure there. But all the
    emails have examples of mapping constant size character arrays (usually
    as strings). This I would like to map as a byte[].
    >
    The aim is that C provides a function callback and passes to the
    callback this structure. Then C# uses public delegate and directly
    invokes that callback.
    >
    I can insert another C callback or function that changes the arguments
    to the C callback and splits the structure into separate char* and
    size_t elements, but that is not my intention.
    I want to directly call C# from C.
    >
    Any help will be appreciated.
    >
    Thanks and regards,
    Vikas
    >

    Comment

    Working...