Skip to content

StructLayouts that differ between architectures and/or operating systems #60573

Open
@DaZombieKiller

Description

@DaZombieKiller

Today, if you need to wrap a native API that has differing structure layouts (namely, structure packing) between platforms, you're stuck in a very messy scenario with not many good options to proceed. An example of such a scenario is Valve's Steamworks API, which has a series of callback structures with packing that varies per platform (8 on Windows, 4 everywhere else).

So, when faced with this problem, some solutions that come to mind are:

  • Ship per-RID assemblies
    • Not realistically feasible outside of the runtime.
  • Duplicate the structures per platform
    • This requires duplication of function signatures that use these structures by value.
    • It also requires duplication of any other structures that use these structures.
  • Use interfaces
    • Results in boxing, which may or may not be acceptable depending on your scenario.
    • Boxing can be avoided by using generics, but this results in vastly more complicated bindings.

None of these are particularly appetizing, and all of them have significant drawbacks. Ideally, the runtime could provide some method to annotate structures to allow their layout to change depending on the current platform. If we ignore everything except structure packing, such a feature could look like:

[StructLayout(LayoutKind.Sequential, Pack = 4)]
[StructPackOverride(PlatformID.Win32NT, Pack = 8)]
public struct SomeCallbackStructure {}

Which would define a struct with a packing value of 8 on Windows, and 4 everywhere else.

cc @jkoritzinsky @AaronRobinsonMSFT

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions