473,403 Members | 2,284 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,403 software developers and data experts.

Marshal struct with embeded array of struct

I am coding a managed C# wrapper for an unmanaged C DLL and I am unable to
marshal a structure that contains an array of structures. When executed, the
following code throws an ArgumentException with a description “Type
ErrorInjectionBuffer can not be marshaled as an unmanaged structure; no
meaningful size or offset can be computed.” I have also included snippets
from the .H file. Any assistance would be greatly appreciated.

[busapi.h] (partial)
#if defined(__WIN32__)
#include <windows.h>
#define CCONV _stdcall
#define NOMANGLE
#endif

#define BT_U16BIT unsigned short
#define BT_U8BIT unsigned char
#define BT_UINT unsigned int
#define BT_INT int

#define EI_COUNT 33 /* Number of error injection words supported by SW */
/* The HW supports one more, for the 33rd data word*/

typedef struct api_eibuf
{
BT_U16BIT buftype; // error injection buffer type
struct
{
BT_U8BIT etype; // error code (e.g. EI_NONE, EI_TWOBUS, etc)
BT_U8BIT edata; // error data (if req'd)
}
error[EI_COUNT];
}
API_EIBUF;

NOMANGLE BT_INT CCONV BusTools_EI_EbufRead(
BT_UINT cardnum, // (i) card number (0 - based)
BT_UINT errorid, // (i) number of error injection buffer to read
API_EIBUF * ebuf); // (o) pointer to resulting buffer values

NOMANGLE BT_INT CCONV BusTools_EI_EbufWrite(
BT_UINT cardnum, // (i) card number (0 - based)
BT_UINT errorid, // (i) number of error injection buffer to write
API_EIBUF * ebuf); // (i) pointer to buffer values to be written to
HW

[C#]
using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
class Class1
{
[DllImport(@"busapi32.dll", EntryPoint = "BusTools_EI_EbufRead")]
public static extern int BusTools_EI_EbufRead(
uint cardnum,
uint errorid,
[MarshalAs(UnmanagedType.LPStruct)]
ref ErrorInjectionBuffer ebuf
);

[DllImport(@"busapi32.dll", EntryPoint = "BusTools_EI_EbufWrite")]
public static extern int BusTools_EI_EbufWrite(
uint cardnum,
uint errorid,
[MarshalAs(UnmanagedType.LPStruct)]
ref ErrorInjectionBuffer ebuf
);

[STAThread]
static void Main(string[] args)
{
Console.WriteLine("SizeOf(struct ErrorInjectionBuffer) = " +
Marshal.SizeOf(typeof(ErrorInjectionBuffer)).ToStr ing()); // Throws
ArgumentException
}
}

[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct ErrorInjectionBuffer
{
private const int ErrorInjectionCount = 33;

private ushort bufferType;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = ErrorInjectionCount)]
private ErrorInjectionData[] errors;
}

[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct ErrorInjectionData
{
private byte eType;
private byte eData;
}
}

Nov 16 '05 #1
5 4297
Daniel,

See if this helps

http://www.dotnetinterop.com/faq/?q=...ithStructArray

Mattias

--
Mattias Sjgren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
There are 33 elements in the array. Other than flattening the array, is
there another way?

"Mattias Sjögren" wrote:
Daniel,

See if this helps

http://www.dotnetinterop.com/faq/?q=...ithStructArray

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #3
Daniel,
There are 33 elements in the array. Other than flattening the array, is
there another way?


Since the nested struct in this case is just two bytes, you could
replace the array with a ushort[], and then manually extract the high
and low bytes (eData and eType respectively) from each ushort value.

Mattias

--
Mattias Sjgren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #4
Do you know if this functionality will be addressed in .NET 2.0?

"Mattias Sjögren" wrote:
Daniel,
There are 33 elements in the array. Other than flattening the array, is
there another way?


Since the nested struct in this case is just two bytes, you could
replace the array with a ushort[], and then manually extract the high
and low bytes (eData and eType respectively) from each ushort value.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #5
No it won't. Simply because a generic marshaller cannot handle all possible
mappings between unmanaged data structures and managed object lay-outs.
Note also that the CLR and the managed languages weren't designed for this
kind of operability with legacy C style data and code. If you need this
level of interop you should use managed C++ (or the upcomming CLI/C++) a
language that was designed for this.

Willy.

"Daniel Brown" <Da*********@discussions.microsoft.com> wrote in message
news:AD**********************************@microsof t.com...
Do you know if this functionality will be addressed in .NET 2.0?

"Mattias Sjgren" wrote:
Daniel,
>There are 33 elements in the array. Other than flattening the array, is
>there another way?


Since the nested struct in this case is just two bytes, you could
replace the array with a ushort[], and then manually extract the high
and low bytes (eData and eType respectively) from each ushort value.

Mattias

--
Mattias Sjgren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #6

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

Similar topics

0
by: William Stacey | last post by:
The following code works, but I can't figure out why. I take a struct with two members, a single byte and byte. I then marshal the whole struct to a byte. I create a new struct (without init'ing...
6
by: SB | last post by:
I feel dumb to ask because I bet this is a simple question... Looking at the code below, can someone please explain why I get two different values in my Marshal.SizeOf calls (see the commented...
1
by: dhornyak | last post by:
I have been banging my head against the wall for a while now, and can't seem to id the problem. I've been through a ton of posts and the code doesn't seem any different. Can anybody see it? When...
2
by: Cyril | last post by:
Hello, I have a problem to marshal a structure that contains an array of an others struct. This array is an array size fixed (MyStruct myStructs and not MyStruct *myStructs). For example : ...
8
by: Charles Law | last post by:
Can anyone suggest how I would marshal a variable length structure back from an API call. Specifically, I am looking at the WaitForDebugEvent function, which returns a DEBUG_EVENT structure. ...
2
by: twawsico | last post by:
I have a piece of code that needs to read the contents of a binary file (that I've created with another app) into an array of structures. The binary data in the file represents just a series of...
1
by: jurot | last post by:
Hi. I have struct in C++: struct MY_STRUCT { int x; int y; char** arrNames; //array of strings }
4
by: cleanrabbit | last post by:
Hello! I hate having to do this, because im almost certain there is someone in the world that has come across this problem and i just havent found their solution yet, so i do appologise if this...
2
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
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: 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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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 projectplanning, coding, testing,...

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.