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
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; }student;
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{[MarshalAs(UnmanagedType.LPWStr,SizeConst=20)]public string Name;[MarshalAs(UnmanagedType.I4)]public int RollNo;[MarshalAs(UnmanagedType.I4)]public int Marks;}
No comments:
Post a Comment