C
C#7mo ago
Odex

How to marshall pointer to array inside struct

I have the following struct
[StructLayout(LayoutKind.Sequential)]
public unsafe struct Font
{
public int BaseSize;
public int GlyphCount;
public int GlyphPadding;
public Texture Texture;
public Rectangle* Recs;
public GlyphInfo* Glyphs;
}
[StructLayout(LayoutKind.Sequential)]
public unsafe struct Font
{
public int BaseSize;
public int GlyphCount;
public int GlyphPadding;
public Texture Texture;
public Rectangle* Recs;
public GlyphInfo* Glyphs;
}
Here Recs and Glyphs are C like arrays iirc. I want to marshal them to managed ones but I get the following error
[StructLayout(LayoutKind.Sequential)]
public unsafe struct Font
{
public int BaseSize;
public int GlyphCount;
public int GlyphPadding;
public Texture Texture;
// Already tried [MarshalAs(UnmanagedType.ByValArray)]
public Rectangle[] Recs;
public GlyphInfo[] Glyphs;
}
[StructLayout(LayoutKind.Sequential)]
public unsafe struct Font
{
public int BaseSize;
public int GlyphCount;
public int GlyphPadding;
public Texture Texture;
// Already tried [MarshalAs(UnmanagedType.ByValArray)]
public Rectangle[] Recs;
public GlyphInfo[] Glyphs;
}
Error SYSLIB1051 : The type 'MyNamespace.Font' is not supported by source-generated P/Invokes. The generated source will not handle marshalling of parameter 'font'.
Error SYSLIB1051 : The type 'MyNamespace.Font' is not supported by source-generated P/Invokes. The generated source will not handle marshalling of parameter 'font'.
I am using LibraryImport
8 Replies
Odex
OdexOP7mo ago
even if I made a custom marshaller, will my code actually compile? I think it doesn't like the type being an array at all. ain't much useful copy-pasting a link with no further explanatory btw hmm I'll try also @cap5lut https://discord.com/channels/143867839282020352/169726586931773440/1239284198968397935 what about this situation
MODiX
MODiX7mo ago
cap5lut
if yes, there are inline arrays, tho u need a type for each size:
[InlineArray(10)]
public struct BufferWith10Elements<T>
{
private T _element0;
}

public struct MyStruct
{
public int someValue;
public BufferWith10Elements<int> numbers;
}
[InlineArray(10)]
public struct BufferWith10Elements<T>
{
private T _element0;
}

public struct MyStruct
{
public int someValue;
public BufferWith10Elements<int> numbers;
}
Quoted by
<@1010118213754949672> from #help-0 (click here)
React with ❌ to remove this embed.
Odex
OdexOP7mo ago
on second thought, do I need to know Rectangle* Recs size in order to make a managed array/span? like the span constructor takes length as a second parameter i remember
Odex
OdexOP7mo ago
@calledude you were right, after making my own marshaller I don't get error anymore
No description
No description
Odex
OdexOP7mo ago
let's test
cap5lut
cap5lut7mo ago
ive never touched interop with native myself, i cant help here, sorry
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
sibber
sibber7mo ago
thats not supported yet by LibraryImport afaik youd have to write a custom marshaller or use DllImport which is what id recommend
Want results from more Discord servers?
Add your server