using PInvoke with an array of struct

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • puchie
    New Member
    • Apr 2010
    • 2

    #1

    using PInvoke with an array of struct

    Hi!

    I want to call a function from a c++ unmanaged dll but i'm having a hard time in doing it because of an array parameter.

    the definition in c++ of the method is:


    Code:
    int __export __stdcallInsertStruct(int countS,  AInfo info[11])
    and

    Code:
    typedef struct
    {
    char* a;
    char b;
    char* c;
    }AInfo;
    In my c# code i have:

    Code:
    [StructLayout(LayoutKind.Sequential)]
            public struct AInfo
            {
                public string a;
                public char b;
                public string c;
            }
    and

    Code:
    private delegate IntPtr InsertStructDelegate(int countS,  AInfo[] info);
    I fill the array before calling this delegate and i
    can't manage to get this to work. Always get an error of broken memory.

    do i have to use another type instead of AInfo[] in my method definition?
    Like IntPtr or byte[]?

    Can someone help me with this one?
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    I don't know specifically, but check the Marshal class for some usefull things.
    I think you may have to marshal your object as a special C array. Don't remember how, but I know saw something like that somewhere

    Comment

    • puchie
      New Member
      • Apr 2010
      • 2

      #3
      I think the problem here is to Marshal the string fields. Does anyone hava an idea on how to do this. I Tried a lot of different approaches without success.

      Thanks

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Same deal, somewhere there's a property or something under the marshalling that says "make this a c-style array of char" or lpcstr or something

        Comment

        Working...