473,729 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 = "birdRS232WakeU p", 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 = "birdStartSingl eFrame",
CharSet=CharSet .Auto,SetLastEr ror=true)]
static extern void birdStartSingle Frame(int nGroupID);

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

[DllImport("bird .dll", EntryPoint = "birdGetFra me",
CharSet=CharSet .Auto,SetLastEr ror=true)]
static extern bool birdGetFrame(in t nGroupID,
[MarshalAs(Unman agedType.LPStru ct)] ref BIRDFRAME pFrame);

[DllImport("bird .dll", EntryPoint = "birdShutDo wn", 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(Unman agedType.LPStru ct)]
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 (birdRS232WakeU p(0,true,1, ref PORT,19200,2000 ,2000) == true)
{
birdStartSingle Frame(0);
while (birdFrameReady (0)==false);
birdGetFrame(0, ref pframe); // **** error occurs here ****
birdShutDown(0) ;

}

Nov 30 '05 #1
0 1117

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

Similar topics

3
4735
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 an error message saying "Cannot marshal parameter #2: Invalid managed/unmanaged type combination (this value type must be paired with Struct)" when calling "AceGetPinParams(iHandle,ref sd_pin)" function,here's my test code: private static...
5
96491
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 what it is. Is converting a C-style struct to a C# class (or struct) marshalling? And what about the function exports? Are those Marshalling too? Thanks for your help and thanks for helping me with the Dll exports (the several messages I posted in...
1
2476
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 CLI ( using a mixed mode DLL ) have any performance penalty? consider one Native C++ CLI assembly calling a methos od another CLI Native C++ Assembly, will there be any marshaling penalty, will there be any penelty at-all? is it possible to pass a...
4
3143
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? BTW, by nested structs I mean structures such as this:
5
4289
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 errors to the webpage with response.write(). Unfortunately Response.anything from within my class generate a "Response is not declared" error. How can I get Response to work from within a class, or is there a better way to handle errors in...
4
9663
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 using the attribute The calling program doesn't know the size of the array, so I would like the dll to dynamically expand the array, and return the right sized array.
0
946
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
1875
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 implement a performance improvement in the transfer of thousands of objects (ClientInfoDescriptor) over a remote call. The objects all have a model object (IDataDrivenModel) to describe them but there may only two or three model objects in total. These...
5
5235
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 marshaling the struct to be able to use it in sendmessage... Here is an example LM_GETITEM: http://msdn.microsoft.com/en-us/library/bb760720(VS.85).aspx
0
8923
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8763
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9428
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9203
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6025
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4796
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2686
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.