Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there any way to generate a non-wrap csharp struct for POD in cpp? #1897

Open
ThraceShah opened this issue Jan 25, 2025 · 1 comment
Open

Comments

@ThraceShah
Copy link

For this cpp struct:

struct MyStruct {
  int x;
  float y;
};

I hope to get a csharp struct like this:

struct MyStruct {
  int x;
  float y;
}

But actually i get a struct or class like this:

    public unsafe partial struct MyStruct
    {
        [StructLayout(LayoutKind.Sequential, Size = 8)]
        public partial struct __Internal
        {
            internal int x;
            internal float y;

            [SuppressUnmanagedCodeSecurity, DllImport("MyModule", EntryPoint = "??0MyStruct@@QEAA@AEBU0@@Z", CallingConvention = __CallingConvention.Cdecl)]
            internal static extern __IntPtr cctor(__IntPtr __instance, __IntPtr _0);
        }

        private MyStruct.__Internal __instance;
        internal ref MyStruct.__Internal __Instance => ref __instance;

        internal static MyStruct __CreateInstance(__IntPtr native, bool skipVTables = false)
        {
            return new MyStruct(native.ToPointer(), skipVTables);
        }

        internal static MyStruct __CreateInstance(__Internal native, bool skipVTables = false)
        {
            return new MyStruct(native, skipVTables);
        }

        private MyStruct(__Internal native, bool skipVTables = false)
            : this()
        {
            __instance = native;
        }

        private MyStruct(void* native, bool skipVTables = false) : this()
        {
            __instance = *(global::MyModule.MyStruct.__Internal*) native;
        }

        public MyStruct(global::MyModule.MyStruct _0)
            : this()
        {
            var ____arg0 = _0.__Instance;
            var __arg0 = new __IntPtr(&____arg0);
            fixed (__Internal* __instancePtr = &__instance)
            {
                __Internal.cctor(new __IntPtr(__instancePtr), __arg0);
            }
        }

        public int X
        {
            get
            {
                return __instance.x;
            }

            set
            {
                __instance.x = value;
            }
        }

        public float Y
        {
            get
            {
                return __instance.y;
            }

            set
            {
                __instance.y = value;
            }
        }
    }

Even though there is no export symbol for MyStruct in cpp

@tritao
Copy link
Collaborator

tritao commented Jan 26, 2025

I don't think this is possible at the moment, it's been requested a few times, a few years someone even created such a backend for targetting C, but it was never contributed as a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants