-
The documentation for If one uses unsafe C# with a pointer to a struct that has this attribute, then are the offsets of member fields always the same as those specified by the attributes? if so then should we adjust the wording of the documentation to avoid any misunderstanding by the use of "managed" and "unmanaged" representations? See also: #100621 (reply in thread) |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 12 replies
-
It is explained in https://learn.microsoft.com/en-us/dotnet/framework/interop/interop-marshalling
Yes, the managed representation can differ from unmanaged representation. Types with identical managed and unmanaged representation are called blittable types: https://learn.microsoft.com/en-us/dotnet/framework/interop/blittable-and-non-blittable-types . |
Beta Was this translation helpful? Give feedback.
-
Well this isn't a major concern for me, there are evidently several ways to do this and the code runs absolutely fine. But there does seem to be a bit of a problem in general with either the language or the runtime. The memory layout might be independent of platform altogether (like say a COFF or ELF file structure). If one writes some C# API to consume a COFF or ELF or GIF or other files made of intricate packed structures, one will want that API to always "see" the file's content correctly no matter what platform we are running on. The real question here is how can one write C# code that might run on any platform, yet be confident it will honor a memory layout contract. It seems (if I've understood everyone) that specifying an explicit layout with specific offset attributes will always work (if the structs are blittable, which in my case they will always be) leaving aside issues of "endianess". The reason I've been pursuing this I had conflicting advice from others, some one way was good others said it wasn't and I should do it another way and so on, I just wanted to get complete clarity on how this currently works. |
Beta Was this translation helpful? Give feedback.
-
@CyrusNajmabadi, Consider 0x4675636B204F6666 in an MCU vector register as an example, to fully understand meaning, one needs information about alignment and padding and offsets. |
Beta Was this translation helpful? Give feedback.
It is explained in https://learn.microsoft.com/en-us/dotnet/framework/interop/interop-marshalling
Yes, the managed representation can differ from unmanaged representation. Types with identical managed and unmanaged representation are called blittable types: https://learn.microsoft.com/en-us/dotnet/framework/interop/blittable-and-non-blittable-types .