Cannot take the address or size of a variable of a managed type
-
hi all, following is a structure that i hv define [StructLayout(LayoutKind.Sequential, Size = 16)] public struct MESSAGE_INFO { public UInt16 _MessageCode; public UInt16 _ChannelNo; [MarshalAs(UnmanagedType.ByValArray, SizeConst=6)] public UInt16[] _Parameter; } Now when i declare pointer to this structure, It throws the error "Cannot take the address or size of a variable of a managed type" MESSAGE_INFO* ptrMsgInfo But if i declare "public UInt16[] _Parameter" as only "public UInt16 _Parameter" then it compiles perfectly. Any light on this would be highly appreciated... Thanx in advance
Vikas Salvi Programmer Analyst
-
hi all, following is a structure that i hv define [StructLayout(LayoutKind.Sequential, Size = 16)] public struct MESSAGE_INFO { public UInt16 _MessageCode; public UInt16 _ChannelNo; [MarshalAs(UnmanagedType.ByValArray, SizeConst=6)] public UInt16[] _Parameter; } Now when i declare pointer to this structure, It throws the error "Cannot take the address or size of a variable of a managed type" MESSAGE_INFO* ptrMsgInfo But if i declare "public UInt16[] _Parameter" as only "public UInt16 _Parameter" then it compiles perfectly. Any light on this would be highly appreciated... Thanx in advance
Vikas Salvi Programmer Analyst
Hello Vikas, MSDN[^] says (at the top of the page) that the pointer can point to "Any user-defined struct type that contains fields of unmanaged types only". When you define the field as an array, then it's considered a managed type. When you define it as just a UInt16, then it's considered an unmanaged type (sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, or bool).
Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com