Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I tried your struct in two projects (C++/C#) and it seems to work correctly.</p> <p>I use this in C++:</p> <pre><code>#include "stdafx.h" #include "windows.h" #pragma pack(1) // pack the following structures on one-byte boundaries // Bird position structure typedef struct tagBIRDPOSITION { short nX; // x-coordinate short nY; // y-coordinate short nZ; // z-coordinate } BIRDPOSITION; // Bird angles structure typedef struct tagBIRDANGLES { short nAzimuth; // azimuth angle short nElevation; // elevation angle short nRoll; // roll angle } BIRDANGLES; // Bird matrix structure typedef struct tagBIRDMATRIX { short n[3][3]; // array of matrix elements } BIRDMATRIX; // Bird quaternion structure typedef struct tagBIRDQUATERNION { short nQ0; // q0 short nQ1; // q1 short nQ2; // q2 short nQ3; // q3 } BIRDQUATERNION; #pragma pack() // resume normal packing of structures typedef struct tagBIRDREADING { BIRDPOSITION position; // position of receiver BIRDANGLES angles; // orientation of receiver, as angles BIRDMATRIX matrix; // orientation of receiver, as matrix BIRDQUATERNION quaternion; // orientation of receiver, as quaternion WORD wButtons; // button states } BIRDREADING; #define BIRD_MAX_DEVICE_NUM 126 // Bird frame structure // // NOTE: In stand-alone mode, the bird reading is stored in reading[0], and // all other array elements are unused. In master/slave mode, the "reading" // array is indexed by bird number - for example, bird #1 is at reading[1], // bird #2 is at reading[2], etc., and reading[0] is unused. typedef struct tagBIRDFRAME { DWORD dwTime; // time at which readings were taken, in msecs BIRDREADING reading[BIRD_MAX_DEVICE_NUM + 1]; // reading from each bird } BIRDFRAME; extern "C" __declspec( dllexport ) BOOL birdGetMostRecentFrame(int nGroupID, BIRDFRAME *pframe) { pframe-&gt;dwTime = GetTickCount(); for (int i = 0; i &lt; sizeof(pframe-&gt;reading)/sizeof(*pframe-&gt;reading); i++) { pframe-&gt;reading[i] = BIRDREADING(); pframe-&gt;reading[i].position.nX = (short)i; pframe-&gt;reading[i].position.nY = (short)i + 1; pframe-&gt;reading[i].position.nZ = (short)i + 2; } return TRUE; } </code></pre> <p>And this in C#:</p> <pre><code> [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct BIRDPOSITION { public short nX; public short nY; public short nZ; } [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct BIRDANGLES { public short nAzimuth; // azimuth angle public short nElevation; // elevation angle public short nRoll; // roll angle } [StructLayout(LayoutKind.Sequential, Pack = 1)] public unsafe struct BIRDMATRIX { public fixed short n[9]; } [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct BIRDQUATERNION { public short nQ0; // q0 public short nQ1; // q1 public short nQ2; // q2 public short nQ3; // q3 } [StructLayout(LayoutKind.Sequential, Pack = 0)] public struct BIRDREADING { public BIRDPOSITION position; public BIRDANGLES angles; public BIRDMATRIX matrix; public BIRDQUATERNION quaternion; public ushort wButtons; } [StructLayout(LayoutKind.Sequential, Pack = 0)] public struct BIRDFRAME { public uint dwTime; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 127)] public BIRDREADING[] readings; } public partial class Form1 : Form { [DllImport(@"Bird.dll", CallingConvention = CallingConvention.Cdecl)] public static extern bool birdGetMostRecentFrame(int nGroupID, ref BIRDFRAME frame); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { BIRDFRAME bf = new BIRDFRAME(); if (checkBox1.Checked) { bf.readings = new BIRDREADING[127]; } if (birdGetMostRecentFrame(0, ref bf)) { listBox1.Items.Add(bf.dwTime.ToString()); Console.WriteLine(bf.dwTime.ToString()); for (int k = 1; k &lt;= 3; k++) { var reading = bf.readings[k]; Console.WriteLine(String.Format(" [{0}][{1}][{2}]", reading.position.nX.ToString(), reading.position.nY.ToString(), reading.position.nZ.ToString())); listBox1.Items.Add(String.Format(" [{0}][{1}][{2}]", reading.position.nX.ToString(), reading.position.nY.ToString(), reading.position.nZ.ToString())); } } } } </code></pre> <p>In both cases (reading initialized as new in C#, or not it gives back meaningful data:</p> <pre><code>40067905 [1][2][3] [2][3][4] [3][4][5] 40068653 [1][2][3] [2][3][4] [3][4][5] 40069418 [1][2][3] [2][3][4] [3][4][5] 40072585 [1][2][3] [2][3][4] [3][4][5] </code></pre> <p>Did it on Windows7 64 bit. I'm attaching a ZIP with both projects for VS2010.</p> <p>Here is the link:</p> <p><a href="http://hotfile.com/dl/115296617/9644496/testCS.zip.html" rel="nofollow">http://hotfile.com/dl/115296617/9644496/testCS.zip.html</a></p> <p>It seems that you are setting wrong values inside that position struct :/</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