Mapping c struct in to c# struct

Posted on 2005-05-14
Last Modified: 2008-03-10
I am using some class library written in C and importing the methods using DllImport.

In the C class library there is a structure called iFvsMinutiaSet_t ( it is a "minutia set" class)
which contains minutia ( I hope you know about minutia or its not relevant to my question).

I want to write a minutiaSet structure and minutia structure to map c structs in order to use those classes but i am having trouble how to write "FvsMinutia_t  ptable[1];" in C# struct.

I have written down the c struct  below.

typedef struct iFvsMinutiaSet_t
   int    nbminutia; /* number of minutia set in the table */
    int      tablesize; /* max number of minutia that can be stored in the table */
    FvsMinutia_t ptable[1]; /* the minutia table starts here */
} iFvsMinutiaSet_t;

typedef struct FvsMinutia_t
    /*! descibes the type of minutia */
    FvsMinutiaType_t type;
    /*! x position */
   float   x;
    /*! y position */
    float   y;
    /*! local minutia direction */
   float   angle;
} FvsMinutia_t;

typedef /*@mutable@*/void*  FvsMinutiaSet_t;

Actually struct iFvsMinutiaSet is a object which contain FvMinutia_t table
In the class library there is a function which returns a pointer to the start position of the table
I want to get the MinutiaSet into my c# struct and get the table to the c# struct and map each minutia in the table to my C# struct minutia and the problem is i don't know how to represent
  FvsMinutia_t ptable[1]; in my C# struct

 here is the FvsMinutiaType_t

typedef enum FvsMinutiaType_t
    FvsMinutiaTypeEnding      = 0, /*!< line ending */
    FvsMinutiaTypeBranching   = 1, /*!< line branch */
    FvsMinutiaTypeCore        = 2, /*!< core point */
    FvsMinutiaTypeDelta       = 3, /*!< delta point */
} FvsMinutiaType_t;

but my main problem is here in the MinutiaSet it has defined like this
FvsMinutia_t ptable[1]  
that mean an array which has one element right?
in the program it keeps on adding minutias into the table... and there is a method called MinutiaGetBuffer(MinutiaSet m);

FvsMinutia_t* MinutiaSetGetBuffer(FvsMinutiaSet_t min)
    iFvsMinutiaSet_t* minutia = (iFvsMinutiaSet_t*)min;
    FvsMinutia_t* pret = NULL;
    if (minutia!=NULL)
        pret = minutia->ptable;

    return pret;

return a pointer to the table of minutia .........
and after that you can loop through the array and get the values of the attributes of the minutia like

FvsMinutia_t* input_minutia = MinutiaSetGetBuffer(set1);

for(int n=0; n< size(" there a method which returns total no of minutia");n++)
X =input_minutia[n].x;
Y = input_minutia[n].y;

So my question is What kind of array should i use in the C# , i know it should be a array of object type of Minutia.... can you please give me an idea?

It's quite urgent

Thanks in Advance,

Question by:sasankad
    LVL 48

    Expert Comment

    In C# ptable member of iFvsMinutiaSet_t should be decalred as IntPtr. C function fills it with pointer to unmanaged memoey block filled with array of FvsMinutia_t structures.
    Use Marshal.PtrToStructure Method to read first structure of unmanaged block to managed FvsMinutia_t structure. To read second, third etc. instances of ptable array increment value of Int32 pointer to Marshal.SizeOf FvsMinutia_t.

    Author Comment

    Thanks AlexFM for your quick response......

    i managed to go through the ptable array.....

    but i have another question

    is there a possible way i can do the vise-versa, i mean from my c# struct FvsMinutiaSet to the c struct and write to the ptable minutias?

    if possible please give me some guidence .......

    This coversion thining i am doing because i am going to save these minutias in a database and minutia matching funtion is written on c , when i have to match a minutiaSet with a database set
    i have to retrieve a mintia set from database and i have convert it in to c struct where i can use this set as a input parameter in c "minutia matching method"

    i hope you will quickly respond.

    LVL 48

    Accepted Solution

    typedef struct iFvsMinutiaSet_t
       int    nbminutia; /* number of minutia set in the table */
        int      tablesize; /* max number of minutia that can be stored in the table */
        FvsMinutia_t ptable[1]; /* the minutia table starts here */
    } iFvsMinutiaSet_t;

    If you want to fill iFvsMinutiaSet_t structure in C# program and pass this structure to C function, make this by the following way:

    1) Write C# definitions of  iFvsMinutiaSet_t and FvsMinutia_t functions. Define ptable as IntPtr.
    2) Allocate unmanaged memory block with size equal to sizeof(iFvsMinutiaSet_t) + tablesize * sizeof(FvsMinutia_t) using Marshal.AllocCoTaskMem Method. It returns IntPtr (let's call it p1).
    3) Fill iFvsMinutiaSet_t.ptable value with p1+sizeof(iFvsMinutiaSet_t) value
    4) Copy iFvsMinutiaSet_t to p1 using Marshal.StructureToPtr Method
    5) add sizeof(FvsMinutia) to p1 and copy first instance of FvsMinutia to p1 using Marshal.StructureToPtr Method. Make this for each FvsMinutia instance.
    6) Declare C function which acepts iFvsMinutiaSet_t* as function with IntPtr parameter and call it with IntPtr value returned by AllocCoTaskMem from p.2.

    I hope you have good knowledge of C/C++, in this case this description should be clear for you.

    Author Comment

    Hi Alex,

    Thank you very much for your comments. I was able to solve the problem by following your instructions.


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    stack 22 143
    IDE C compiler 5 113
    How to design, and implement simple interface 5 119
    Problem to ASCII 1 124
    Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
    Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
    The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now