Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Mapping c struct in to c# struct

Posted on 2005-05-14
Medium Priority
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
  • 2
  • 2
LVL 48

Expert Comment

ID: 14005199
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

ID: 14006712
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

AlexFM earned 500 total points
ID: 14008239
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

ID: 14024787
Hi Alex,

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


Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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 how to use strings and some functions related to them in the C programming language.

578 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