CppSharp
CppSharp copied to clipboard
FunctionToInstanceMethodPass generates invalid code when returning struct by value (C# generator)
Brief Description
When a function that is added to a class that returns a struct/class by value, the pass generates invalid code. The same function with the pass deactivated works fine. Primitive values and pointers also work. Example:
// header
class MYLIBRARY_API TestClass
{
int a;
};
struct MYLIBRARY_API SimpleStruct
{
int a;
};
MYLIBRARY_API SimpleStruct TestClassMyStruct1(TestClass* bb);
// cpp
MYLIBRARY_API SimpleStruct TestClassMyStruct1(TestClass* bb)
{
SimpleStruct s;
s.a = 6;
return s;
}
A simple test program prints 0 instead of 6:
SimpleStruct s1 = c.MyStruct1;
Console.WriteLine(s1.A);
Invalid code (compiles fine, just does not do what it should):
[SuppressUnmanagedCodeSecurity, DllImport("MyLibrary", EntryPoint = "?TestClassMyStruct1@@YA?AUSimpleStruct@@PEAVTestClass@@@Z", CallingConvention = __CallingConvention.Cdecl)]
internal static extern void MyStruct1(__IntPtr __instance, __IntPtr @return);
public global::MyLibraryBindings.SimpleStruct MyStruct1
{
get
{
var __ret = new global::MyLibraryBindings.SimpleStruct.__Internal();
__Internal.MyStruct1(__Instance, new IntPtr(&__ret));
return global::MyLibraryBindings.SimpleStruct.__CreateInstance(__ret);
}
}
Correct code with pass disabled:
[SuppressUnmanagedCodeSecurity, DllImport("MyLibrary", EntryPoint = "?TestClassMyStruct1@@YA?AUSimpleStruct@@PEAVTestClass@@@Z", CallingConvention = __CallingConvention.Cdecl)]
internal static extern global::MyLibraryBindings.SimpleStruct.__Internal TestClassMyStruct1(__IntPtr bb);
public static global::MyLibraryBindings.SimpleStruct TestClassMyStruct1(global::MyLibraryBindings.TestClass bb)
{
var __arg0 = bb is null ? __IntPtr.Zero : bb.__Instance;
var __ret = __Internal.TestClassMyStruct1(__arg0);
return global::MyLibraryBindings.SimpleStruct.__CreateInstance(__ret);
}
OS: Windows
Used settings
Everything default apart from library and include setup, activating verbose mode and setting generator to C#.
Fixed in a recent CppSharp commit, probably #3414294ab8287e24d96219005cb5486dec7b1843. Thanks @josetr!