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

Struct with array(s)

I am reading data to/from a file which contains C++ structures like this

struct StructCPP
{
long lDgType;
FILETIME ftDgTime;
char cName[128];
char cVersion[30];
char cSpare[98];
long lCount;
}

I am trying to access the data in the file using C# structures like this:

struct StructCSharp1 // structure without array(s)
{
public uint DgType;
public ulong DgTime;
public int Count;
};

struct StructCSharp2 // structure with arrays
{
public uint DgType;
public ulong DgTime;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=128)]
public byte[] Name;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=30)]
public byte[] Version;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=98)]
public byte[] Spare;
public int Count;
};

Trying to access the data in the structures:
....
unsafe
{
fixed (byte* pb = &pBuf[0]) // pointing to somewhere in the file
{
StructCSharp1 dg1 = new StructCSharp1(); // OK
StructCSharp2 dg2 = new StructCSharp2(); // OK
StructCSharp1 dg11 = *(StructCSharp1*)(pb); // OK
StructCSharp2 dg22 = *(StructCSharp2*)(pb);} // Fails
}

The last statement (dg 22 = ...) gives the compiler error:
"Cannot take the address or size of a variable of a managed type"
('...\StructCSharp2')

Any suggestions?
Nov 15 '05 #1
3 1203
Olav,
Any suggestions?


Any reason you can't fill the structs member by member instead?

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #2
Mattias Sjögren <ma********************@mvps.org> wrote:
Olav,
Any suggestions?


Any reason you can't fill the structs member by member instead?

Mattias


Mathias,

Do you really mean like this?

unsafe
{
fixed (byte* pb = &pBuf[12])
{
int nCount = *(UInt32*)(pb +128+128+128+30+98);
}
}

If not, please advise me.

Nov 15 '05 #3
Olav,
Do you really mean like this?

unsafe
{
fixed (byte* pb = &pBuf[12])
{
int nCount = *(UInt32*)(pb +128+128+128+30+98);
}
}

If not, please advise me.


You don't have to use unsafe code for this. I'd do

int nCount = pBuf[i] | pBuf[i+1] << 8 |
pBuf[i+2] << 16 | pBuf[i+3] << 32;

or use the BitConverter class.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #4

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

Similar topics

6
by: Stuart Norris | last post by:
Dear Readers, I am attempting to initialise a struct contiaing a dynamic character string. In the example below I am trying to initialise the name field so that my struct does not waste space. ...
19
by: Geetesh | last post by:
Recently i saw a code in which there was a structer defination similar as bellow: struct foo { int dummy1; int dummy2; int last }; In application the above array is always allocated at...
20
by: fix | last post by:
Hi all, I feel unclear about what my code is doing, although it works but I am not sure if there is any possible bug, please help me to verify it. This is a trie node (just similar to tree nodes)...
2
by: beetle | last post by:
Hello, I'm storing data in several different binary tree's. The root node is located in a struct containing general data about the tree. struct lnode { char *fname; int nentry;
4
by: PCHOME | last post by:
Hi! I have questions about qsort( ). Is anyone be willing to help? I use the following struct: struct Struct_A{ double value; ... } *AA, **pAA;
5
by: Cybertof | last post by:
Hello, Is it possible to convert a VB6 Array of Struct to a C# Array Of Struct ? The test context is a C# application calling a VB6 ActiveX DLL Function using UDT (User Defined Type) and...
3
by: GrkEngineer | last post by:
I recently had to use someone's struct from a native app to receive data over Udp. The struct has a array member which looked like this: struct sensorHdr{ char sName; }; When I tried to make...
4
by: DaHool | last post by:
Hi there !!! I browsed around the Internet in search for a solution of a little difficult problem i have in VB.NET.... However, i cannot find a suitable anwser anywhere, so i thought i'll give...
9
by: AM | last post by:
Hi, I have a C++ Dll that has a function that is being exported as shown below extern "C" __declspec(dllexport) validationResult __stdcall _validateData(double dataToMat, int time); A...
11
by: abhiM | last post by:
I have a struct that has an array in it. I need to assign space to the array in a function and pass the corresponding struct by reference to another function so that it can store values into the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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:
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,...

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.