Closed dongfengxin closed 9 months ago
As the RequiresDynamicCode
message says, StructureToPtr(Object)
is unfriendly with AOT compiler. StructureToPtr<T>
is the alternative.
Can you change the function to generic and see if it solves the problem?
public static IntPtr CopyStructToGlobalMem<T>(T obj)
{
IntPtr intPtr = Marshal.AllocHGlobal(Marshal.SizeOf<T>());
Marshal.StructureToPtr(obj, intPtr, fDeleteOld: false);
return intPtr;
}
In addition, please do not ignore AOT/trim warnings during NativeAOT publishing. Methods with RequiresDynamicCode
are likely to break with AOT.
As the
RequiresDynamicCode
message says,StructureToPtr(Object)
is unfriendly with AOT compiler.StructureToPtr<T>
is the alternative.Can you change the function to generic and see if it solves the problem?
public static IntPtr CopyStructToGlobalMem<T>(T obj) { IntPtr intPtr = Marshal.AllocHGlobal(Marshal.SizeOf<T>()); Marshal.StructureToPtr(obj, intPtr, fDeleteOld: false); return intPtr; }
In addition, please do not ignore AOT/trim warnings during NativeAOT publishing. Methods with
RequiresDynamicCode
are likely to break with AOT.
Thank you so much! It Works Well.
As the
RequiresDynamicCode
message says,StructureToPtr(Object)
is unfriendly with AOT compiler.StructureToPtr<T>
is the alternative.Can you change the function to generic and see if it solves the problem?
public static IntPtr CopyStructToGlobalMem<T>(T obj) { IntPtr intPtr = Marshal.AllocHGlobal(Marshal.SizeOf<T>()); Marshal.StructureToPtr(obj, intPtr, fDeleteOld: false); return intPtr; }
In addition, please do not ignore AOT/trim warnings during NativeAOT publishing. Methods with
RequiresDynamicCode
are likely to break with AOT.
And there a a lot of waring, further more , I with little experience of nativeaot update, sometimes, did not no the solution action with the warnings。 lot of knowledge to learn with nativeaot.
Since this case was defined to not be fully compatible with NativeAOT and the alternative suggestion worked, would it be reasonable to close this issue?
Description
NativeAOT Convert C# Struct to C++ Pointer Failed
There is a C# Struct like this :
and will be Convert to a C++ Intptr with the fllowing method:
last will be used like this:
Reproduction Steps
The Above function runs well in normal release way, but didnot runs well in NativeAOT.
Expected behavior
Actual behavior
The Second line returns the failed message.
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
No response