Quantcast
Channel: Questions in topic: "dll"
Viewing all articles
Browse latest Browse all 706

Pass C# struct containing Matrix to C++ dll

$
0
0
I have the following structure in C# which mimics a C++ structure that serves as a parameter to my DLL function: [Serializable] [StructLayout(LayoutKind.Sequential)] struct Volume { [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)] public Vector3 mExtent; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)] public Matrix4x4 mWorldToObject; } My C++ function is declared within the C# as [DllImport(dllname), CallingConvention = CallingConvention.Cdecl)] private static extern Vector3 ComputeCurl(Vector3 p, IntPtr v); How do I fill a `Volume` object with data and pass its pointer to the plugin (dll)? I don't really understand the marshalling aspects of Unity's data types. I tried the following, and the matrix data in C++ isn't right (the vector is alright weirdly..) Volume obstacle = new Volume(); obstacle.mExtent = new Vector3(); obstacle.mExtent = t.lossyScale * 0.5f; obstacle.mWorldToObject = Matrix4x4.TRS(t.position, t.rotation, Vector3.one); IntPtr ptr= Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Volume))); Marshal.StructureToPtr(obstacle, ptr, false); Do I need to use float[] instead of `Vector3` and `Matrix4x4` and individually assign (gulp) each element? In reality, I need to pass a pointer to an array of `Volume`. Is there anything special I'd need to do besides allocating a larger buffer and incrementing the `ptr` by `Marshal.Sizeof(typeof(Volume))`?

Viewing all articles
Browse latest Browse all 706

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>