introduction to Unmanaged Structure in C#.

In dotnet we are working with managed resources as well as unmanaged resources. Surprisingly, use of unmanaged resources are much more than the managed resources. Memory management, garbage collectors are already available with the dotnet, so its not the problem for the developer. While working with the unmanaged apis we may require to access unmanaged memory by structures.

Suppose we have unmanaged binary developed in C++, which is containing a structure declared as below

typedef struct
  char[20]  Name;
  long RollNo;
  long Marks;

and there is a publicly exposed method which takes Integer pointer (IntPtr) as a parameter. In that case to store the returned value we need to create structures with special attributes StructLayout, which makes it of fixed length and Marshalable.
Unmanaged Structure in c# similar to above structure is as written below

[StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]
        struct student
            public string Name;
            public int RollNo;
            public int Marks;

No comments:

Post a Comment