473,788 Members | 2,733 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 ArgumentExcepti on with a description “Type
ErrorInjectionB uffer 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_Ebu fRead(
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_Ebu fWrite(
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 ConsoleApplicat ion1
{
class Class1
{
[DllImport(@"bus api32.dll", EntryPoint = "BusTools_EI_Eb ufRead")]
public static extern int BusTools_EI_Ebu fRead(
uint cardnum,
uint errorid,
[MarshalAs(Unman agedType.LPStru ct)]
ref ErrorInjectionB uffer ebuf
);

[DllImport(@"bus api32.dll", EntryPoint = "BusTools_EI_Eb ufWrite")]
public static extern int BusTools_EI_Ebu fWrite(
uint cardnum,
uint errorid,
[MarshalAs(Unman agedType.LPStru ct)]
ref ErrorInjectionB uffer ebuf
);

[STAThread]
static void Main(string[] args)
{
Console.WriteLi ne("SizeOf(stru ct ErrorInjectionB uffer) = " +
Marshal.SizeOf( typeof(ErrorInj ectionBuffer)). ToString()); // Throws
ArgumentExcepti on
}
}

[Serializable]
[StructLayout(La youtKind.Sequen tial)]
public struct ErrorInjectionB uffer
{
private const int ErrorInjectionC ount = 33;

private ushort bufferType;
[MarshalAs(Unman agedType.ByValA rray, SizeConst = ErrorInjectionC ount)]
private ErrorInjectionD ata[] errors;
}

[Serializable]
[StructLayout(La youtKind.Sequen tial)]
public struct ErrorInjectionD ata
{
private byte eType;
private byte eData;
}
}

Nov 16 '05 #1
5 4319
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*********@di scussions.micro soft.com> wrote in message
news:AD******** *************** ***********@mic rosoft.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
1899
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 any members) and marshal the tmp array back to the new struct. The new struct shows the same data. The single type is easy, that just gets copied. The byte ref type is the interesting part. The address (i.e ref) of the original byte gets...
6
10820
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 lines)? TIA! sb
1
4220
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 I call to GetTokenInformation I receive a buffer size (see //HERE... comment in code), but when I let the code continue, asp.net just sits there returning nothing, apparently on the Marshal.AllocHGlobal call. Here's the library function:
2
7491
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 : C declaration : struct Point {
8
3325
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. However, the DEBUG_EVENT structure is defined as a union, and the size and contents vary depending on the event code contained in the header. typedef struct _DEBUG_EVENT { DWORD dwDebugEventCode; DWORD dwProcessId;
2
4859
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 singles that correspond to those in my structure detailed below. So when I load the file, all that I know for certain is that there will be some multiple of these eight singles represented in the binary data. My code below will read the data...
1
4779
by: jurot | last post by:
Hi. I have struct in C++: struct MY_STRUCT { int x; int y; char** arrNames; //array of strings }
4
4729
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 has already been covered. I have been trying to learn c# as fast as possible in the last month and in doing so i have been re-visiting old C/C++ problems and trying to overcome them in C#. What i have is a very complex serise of structures. Id...
2
7207
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 Marshal.PtrToStructure to copy the all the data into the "other" array? unsafe public struct DeadReckoning {
0
9498
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
10177
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10113
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,...
0
8995
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, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7519
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
6750
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
5402
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
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4074
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.