Note that there are some explanatory texts on larger screens.

plurals
  1. POC++/C# Float issues
    text
    copied!<p>I have a problem when trying to read a struct from C++ in C#. I did this more times and it worked always, but i never used floating point numbers.</p> <p>These are the structs i use on the C# side:</p> <pre><code>[StructLayout(LayoutKind.Sequential)] public struct ImageData { public UInt32 Width; public UInt32 Height; public UInt32 Length; public IntPtr PixelPointer; } [StructLayout(LayoutKind.Sequential)] public struct UserData { public UInt32 misc; public Boolean IsValid; public VirtualPosition RightHand; public VirtualPosition LeftHand; public VirtualPosition Head; } [StructLayout(LayoutKind.Sequential)] public struct VirtualPosition { public Position3D RealPosition; public Position3D ProjectedPosition; } [StructLayout(LayoutKind.Sequential)] public struct Position3D { public float X; public float Y; public float Z; } </code></pre> <p>This are the structs i use on the C++ side:</p> <pre><code> typedef struct XnVector3D { XnFloat X; XnFloat Y; XnFloat Z; } XnVector3D; class VirtualPosition { public: XnVector3D RealPosition; XnVector3D ProjectedPosition; VirtualPosition(void) { } VirtualPosition(XnVector3D real, XnVector3D projected) { RealPosition = real; ProjectedPosition = projected; } }; class UserData { public: int misc; // Misc number to check if i have the correct pointer ( debug only ) bool Valid; VirtualPosition RightHand; VirtualPosition LeftHand; VirtualPosition Head; }; </code></pre> <p>Now the C# part which has been used to get the data:</p> <pre><code>public unsafe UserData GetUserData(UInt32 user) { IntPtr pos = GetData(_Index, user); UserData* ptr = ((UserData*)pos.ToPointer()); UserData result = *ptr; return result; } [DllImport("KinectNative.dll", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr GetData(Int32 index, UInt32 user); </code></pre> <p>And finally the part which is used to retrieve data( C++ ):</p> <pre><code>EXPORT UserData* GetData(int index, XnUserID user) { if (_LastData != NULL) _LastData-&gt;~UserData(); system("cls"); _LastData = new UserData(_Context-&gt;GetDevice(index)-&gt;GetData(user)); std::cout &lt;&lt; "X: " &lt;&lt; (int)_LastData-&gt;Head.ProjectedPosition.X &lt;&lt; " Y:" &lt;&lt; (int)_LastData-&gt;Head.ProjectedPosition.Y &lt;&lt; std::endl; return _LastData; } </code></pre> <p>The misc number is correct when reading it in C# and the Console tells me the correct numbers, but all floats are corrupted and when i get different numbers in C++ and C#. Anyway the difference is to large to accept it. ( Got 340 in C++ and around 0.xxxxxxx in C# ).</p> <p>Any help would be welcome. Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload