Cannot Marshal Non-Blittable types such as Bool through a CustomMarshaller using LibraryImport #115703
Replies: 2 comments 8 replies
-
This is the referenced file in question: https://github.com/Blizzardo1/SharpSDL3/blob/main/SDL3/OwnedEventMarshaller.cs Snippet: public static Event ConvertToManaged(nint unmanaged) {
Event managed = new();
if (unmanaged == nint.Zero) {
return managed;
}
uint type = (uint)unmanaged.ToInt32();
EventType eventType = (EventType)type;
managed.Type = eventType;
return managed;
} Essentially one of the structs contains a boolean: https://github.com/Blizzardo1/SharpSDL3/blob/main/SDL3/Structs/MouseButtonEvent.cs [StructLayout(LayoutKind.Sequential)]
public struct MouseButtonEvent
{
public EventType Type;
public uint Reserved;
public ulong Timestamp;
public uint WindowId;
public uint Which;
public byte Button;
public bool Down;
public byte Clicks;
public byte Padding;
public float X;
public float Y;
} This caused VisualStudio to complain endlessly without reason as to why. Perhaps add to the error that marshalling does or does not support certain types. Project Code: while (_game.IsRunning) {
_fps.Start();
_ = Sdl.PollEvent(out Event e); // Calling this caused an AccessViolationException after fixing ExecutionEngineException
_game.Update(e);
_game.Draw();
uint delta = (uint)_fps.GetTicks();
if (delta < 1000 / FramesPerSecond) {
Sdl.Delay((1000 / FramesPerSecond) - delta);
}
CurrentFPS = delta;
} |
Beta Was this translation helpful? Give feedback.
-
did you disable runtime marshalling? bool is NOT blittable by default as you saw (this is legacy decision that cannot be changed easily unfortunetly) but u can make it blittable by disabling runtime marshalling (in which case default marshalling is done by source generators if i remember correctly) |
Beta Was this translation helpful? Give feedback.
-
So with much frustration, I'm working on SdlSharp3 in my Repo. With struct Event, I had some bool types. Without telling me the details of the issue at hand, VisualStudio demanded I use a custom marshaller. That's until I used a byte as a bool and VisualStudio happily said, "Ok, this will do".
My question is, why is bool a non-blittable type, and how could I use LibraryImport with my non-trivial structs that contain booleans and strings?
Beta Was this translation helpful? Give feedback.
All reactions