473,395 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 1105

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Webdiyer | last post by:
I want to integrate SecurID two-factor authentication system of the RSASecurity.inc into our asp.net application,but I get into trouble when trying to call the API functions of the ACE Agent,I got...
5
by: VM | last post by:
What's marshalling? I've had to use it extensively for a project but I don't know what it means. I tried to look for a definition in the Internet but I couldn't find anything that would explain...
1
by: Nadav | last post by:
Hi I am about to write a performance crutial system, I am considering writing this system based on native COM or unmanaged C++ exposed as CLI, Now, I Wonder... does exposing a native code through...
4
by: MSDousti | last post by:
Hi all, I read some Q&As in the Net, discussing Marshalization of nested structs in C# (or VB.NET). Some guys stated that .NET framework does not support this feature yet. Are they right? ...
5
by: Michael | last post by:
Hello, I have a separate Database class that handles any database work that all my asp.net pages can use. My problem is, many times I use try/catch to catch errors, and I want to output these...
4
by: Bjarne Nielsen | last post by:
Hi all From a C# program, I need to call an unmanaged (C++) dll. The dll fills an array with variables and returns that to the C# program. When I have a fixed sized array, this works fine by...
0
by: chowsaikeet | last post by:
Dear All, I have trouble doing marshaling for polymorphism. Please look at the code below: //C++ code, sample.dll Class A {
0
by: jpogorman | last post by:
Hello, I am trying to get c# custom marshaling working in a particular scenario but it does not appear to be working or not jumping into my marshaling class when I try to debug it. I am try to...
5
by: michelqa | last post by:
Hi, I need to call a lot of different native SendMessage to retreive informations from non managed application. Some win32 messages use struct pointer for lparam....how to create and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.