473,414 Members | 1,740 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,414 software developers and data experts.

Structure Marshalling Question

I am having some trouble marshalling data between c++/C#

Here is the C++ code:
---------------------------------------------------------------------------------
typedef struct
{
......
......
}MyChildStruct;

typedef struct
{
......
......
MyChildStruct m_Children[10]; //This array of structures causes problems.
}MyParentStruct;


void SetData(MyParentStruct *pStructs, int numStructs)
{
....
}
void DoSomethingToData()
{
}


Here is the C# code
------------------------------------------------------------------------------------
struct MyChildStruct
{
......
......
};

struct MyParentStruct
{
......
......
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 10, ArraySubType = UnmanagedType.Struct)]
MyChildStruct[] m_Children;
}

class MyDLL
{
[DllImport("MyDLL.dll")]
public static extern void SetData(IntPtr pStructs, int numStructs);

[DllImport("MyDLL.dll")]
public static extern void DoSomethingToData();
}
class Foo
{
private GCHandle m_GCHandle;
private MyParentStructs[] m_MyStructs = new MyParentStructs[100];

public Foo()
{
this.m_GCHandle = GCHandle.Alloc(this.m_MyStructs, GCHandleType.Pinned);

MyDll.SetData(m_GCHandle.AddrOfPinnedObject(), m_MyStructs.Length);

}
public void DoStuff()
{
MyDll.DoSomethingToData();
}
}

---------------------------------------------------------------------------

This used to work until I needed to add the array of structures to MyStruct2. As I don't have control over the unmanaged code, i need to resolve this.
I get a runtime error "Object contains non-primitive or non-blittable data." in Foo() when GCHandle.Alloc(...) is called.

thanks in advance
-steve
Dec 15 '08 #1
1 1634
mldisibio
190 Expert 100+
Have you checked out this reference, at least until someone posts a more direct answer?
Default Marshaling for Arrays
Dec 17 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Charles Law | last post by:
I have defined LVITEM as follows: <StructLayout(LayoutKind.Sequential)> Private Structure LVITEM Dim mask As Int16 Dim iItem As Int16 Dim iSubItem As Int16 Dim state As Int16 Dim stateMask As...
16
by: Duncan Mole | last post by:
Hi, This is probably an easy one but it iy first bit of p/invoke. I am trying to use the following C struct in a call: typedef struct { BYTE SRB_Cmd; BYTE SRB_Status, BYTE ...
1
by: Betty | last post by:
When marshalling a char array in a structure from C to .NET, I get the following error: System.ArgumentException: Type could not be marshaled because the length of an embedded array instance does...
4
by: Scott Lemen | last post by:
Hi, Some Win APIs expect a structure with a fixed length string. How is it defined in VB .Net 2003? When I try to use the FixedLengthString class I get an "Array bounds cannot appear in type...
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
11
by: Lance | last post by:
Hi all, I've got a some structures defined as ////// <StructLayout(LayoutKind.Sequential)Public Structure GM_LayerInfo_t Public mDescription As String Public mNativeRect As GM_Rectangle_t...
1
by: Beorne | last post by:
I have a cpp application with this structure: //////////////C++/////////////// typedef struct StatusStructure { char FixedLenString; long LongVariable; double DoubleVariable; BOOL...
9
by: A n g l e r | last post by:
Hi all. I've got the following code as a part of managed C++ library that is loaded by a project in C#: public ref class FlyCaptureEnumsAndStructsManaged { public: typedef enum class...
1
by: d-42 | last post by:
Hi, I'm pretty sure I've just got a Marshalling problem, but I'm completely stumped. If there is a better newsgroup to post this in, please point me towards it. First I'm trying to use...
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...
0
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...

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.