471,310 Members | 1,026 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

marshaling error when calling C++ DLL within C#

Hi Folks,
I am writing a c# app that interacts with some scientific hardware (a
"bird", which is just a magnetic tracker) through a C++ DLL. The actual
data from the "bird" is read by the procedure birdGetFrame(), which has
as a second parameter a pointer to the struct BIRDFRAME, into which all
the data are written when the procedure returns successfully. Just to
make things complicated, BIRDFRAME itself contains variables of other
struct types.

My issue is that when I call the procedure, I get an unhandled
exception with the following error: "Additional information: Can not
marshal parameter #2: The type definition of this type has no layout
information."

It seems that even though I crate a new variable "pframe" of type
BIRDFRAME the procedure cannot pass a pointer to it. I can't tell if
the problem is how I set up pframe, or whether the BIRDFRAME struct is
itself poorly constructed. Any help is appreciated.

I've included the listing of the relevant code below.

-Marc-
Listing:

[DllImport("bird.dll", EntryPoint = "birdRS232WakeUp", CharSet =
CharSet.Auto, SetLastError = true)]
static extern Boolean birdRS232WakeUp ( int nGroupID, Boolean
bStandAlone, int nNumDevices, ref uint pwComport, UInt32 dwBaudRate,
UInt32 dwReadTimeout, UInt32 dwWriteTimeout);
[DllImport("bird.dll", EntryPoint = "birdStartSingleFrame",
CharSet=CharSet.Auto,SetLastError=true)]
static extern void birdStartSingleFrame(int nGroupID);

[DllImport("bird.dll", EntryPoint = "birdFrameReady",
CharSet=CharSet.Auto,SetLastError=true)]
static extern bool birdFrameReady(int nGroupID);

[DllImport("bird.dll", EntryPoint = "birdGetFrame",
CharSet=CharSet.Auto,SetLastError=true)]
static extern bool birdGetFrame(int nGroupID,
[MarshalAs(UnmanagedType.LPStruct)] ref BIRDFRAME pFrame);

[DllImport("bird.dll", EntryPoint = "birdShutDown", CharSet =
CharSet.Auto, SetLastError = true)]
static extern Boolean birdShutDown ( int nGroupID);

// Bird position structure
public class BIRDPOSITION
{
short nX; // x-coordinate
short nY; // y-coordinate
short nZ; // z-coordinate
}

// Bird angles structure
public class BIRDANGLES
{
short nAzimuth; // azimuth angle
short nElevation; // elevation angle
short nRoll; // roll angle
}

// Bird matrix structure
public class BIRDMATRIX
{
short[,] n= new short [3,3]; // array of matrix elements
}

// Bird quaternion structure
public class BIRDQUATERNION
{
short nQ0; // q0
short nQ1; // q1
short nQ2; // q2
short nQ3; // q3
}

// Bird reading structure
public class BIRDREADING
{
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
short wButtons; // button states
}

// Bird frame structure
{
int dwTime; // time at which readings were taken, in msecs
[MarshalAs(UnmanagedType.LPStruct)]
BIRDREADING[] reading = new BIRDREADING [BIRD_MAX_DEVICE_NUM + 1];
// reading from each bird
}

// code to read data from bird

static int BIRD_MAX_DEVICE_NUM = 256;
uint PORT = 1;
BIRDFRAME pframe= new BIRDFRAME();
if (birdRS232WakeUp(0,true,1, ref PORT,19200,2000,2000) == true)
{
birdStartSingleFrame(0);
while (birdFrameReady(0)==false);
birdGetFrame(0, ref pframe); // **** error occurs here ****
birdShutDown(0);

}

Nov 30 '05 #1
0 1022

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by VM | last post: by
1 post views Thread by Nadav | last post: by
4 posts views Thread by MSDousti | last post: by
5 posts views Thread by Michael | last post: by
4 posts views Thread by Bjarne Nielsen | last post: by
5 posts views Thread by michelqa | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.