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

arrays, structs, pointers, casting - help!

i am new to c# so if this is a trivial problem, forgive me! i've
searched the web and after 2 days still cannot solve it. i am
converting a c++ application to c# - the problem code is listed below.
the c++ code uses a byte array (msg[256]) to hold a serial message.
since the message can be of different layouts, the array is cast to the
appropriate layout "type" (struct SERIAL_CAN_MSG_TYPE, in this
example). to complicate things, there is an array at the end of the
SERIAL_CAN_MSG_TYPE struct, data[], which can hold variable data types
(byte, ushort, and uint) -AND- can be of variable length (more than 2
elements). with that said, here's the c++ code:

---------- start c++ code -------------
typedef unsigned char INT_8U;
typedef unsigned short int INT_16U;
typedef short int INT_16;
....
typedef struct
{
INT_8U priority;
INT_16 dlc;
INT_8U messageType;
INT_8U channel;
INT_8U data[2];
}SERIAL_CAN_MSG_TYPE;
....
INT_8U msg[256];
SERIAL_CAN_MSG_TYPE* msgPtr;
msgPtr = (SERIAL_CAN_MSG_TYPE*)&(msg[0]);
....
// uses casting to assign a ushort to the byte array (data)
for(i = 0; i < 64; i++)
{
*((INT_16U *)&msgPtr->data[i*4]) = spoolChannel[i].address;
}
------- end c++ code --------

..... and here's my attempt at converting to c#:

----------- start c# code ------------
[StructLayout(LayoutKind.Explicit)]
unsafe public struct SERIAL_CAN_MSG_TYPE
{
[FieldOffset(0)]public byte priority;
[FieldOffset(1)]public short dlc;
[FieldOffset(3)]public byte messageType;
[FieldOffset(4)]public byte channel;
// to take care of diff element types in 1 array,
// since i can't cast on left-hand side of equation
[FieldOffset(5)]public byte [] dataBytePtr;
[FieldOffset(5)]public ushort [] dataUshortPtr;
[FieldOffset(5)]public uint [] dataUintPtr;
};
....
byte [] msg = new byte[256];
fixed(SERIAL_CAN_MSG_TYPE * msgPtr = (SERIAL_CAN_MSG_TYPE*) msg[0])
{
...
for(i = 0; i < 64; i++)
{
// assigns a ushort to the data array using ushort ptr
msgPtr->dataUshortPtr[i] = spoolChannel[i].address;
}
...
}
----------- end c# code --------

above c# code produces error:
"Cannot take the address or size of a variable of a managed
type('SERIAL_CAN_MSG_TYPE')"
on the "fixed" line.

i've read that in c#, a pointer is not permitted to point to a struct
that contains references (arrays). i'm sure it has something to do
with managed/unmanaged code, but i don't understand it deep enough to
figure out a solution!

thanks for any help...

Nov 17 '05 #1
4 4487
oops, forgot the "&" in the c# line:
fixed(SERIAL_CAN_MSG_TYPE * msgPtr = (SERIAL_CAN_MSG_TYPE*) &msg[0])
kelli

Nov 17 '05 #2
Doesn't it work for you?

[StructLayout(LayoutKind.Explicit)]
unsafe public struct SERIAL_CAN_MSG_TYPE
{
[FieldOffset(0)]public byte priority;
[FieldOffset(1)]public short dlc;
[FieldOffset(3)]public byte messageType;
[FieldOffset(4)]public byte channel;
// to take care of diff element types in 1 array,
// since i can't cast on left-hand side of equation
[FieldOffset(5)]public byte [] dataBytePtr;
[FieldOffset(5)]public ushort [] dataUshortPtr;
[FieldOffset(5)]public uint [] dataUintPtr;
};

public unsafe void test()
{
byte [] msg = new byte[256];

fixed(byte *msgPtr = msg)
{
SERIAL_CAN_MSG_TYPE newStruct = new SERIAL_CAN_MSG_TYPE();

Marshal.PtrToStructure((IntPtr) msgPtr, newStruct);

fixed(ushort *ushortPtr = newStruct.dataUshortPtr)
{
for(int i = 0; i < 64; i++)
{
// assigns a ushort to the data array using ushort ptr
*(ushortPtr + i) = 0;//spoolChannel[i].address;
}
}
}
}

It doesn't cast you msg[256] into a SERIAL_CAN_MSG_TYPE, PtrToStructure
(basically a memcpy) fill the new struct and from there work with it.

Is it mandatory to modificate the values on the original msg[256] or can you
use the newStruct???

Gustavo.

"kelli" <k-******@tamu.edu> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
oops, forgot the "&" in the c# line:
fixed(SERIAL_CAN_MSG_TYPE * msgPtr = (SERIAL_CAN_MSG_TYPE*) &msg[0])
kelli

Nov 17 '05 #3
gustavo - thank you so much - it does seem to work (compiles anyway!).

you may be right - i think i can fill the structure, then use
Marshal.Copy (maybe?) to get the data into a byte array which is passed
to the next function.

Nov 17 '05 #4
If you need back the array then may be you can use

Marshal.StructureToPtr(newStruct, (IntPtr) msgPtr, true);

Marshal.Copy also should work, but I don't know if it could have some
problem.

Gustavo.
"kelli" <k-******@tamu.edu> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
gustavo - thank you so much - it does seem to work (compiles anyway!).

you may be right - i think i can fill the structure, then use
Marshal.Copy (maybe?) to get the data into a byte array which is passed
to the next function.

Nov 17 '05 #5

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

Similar topics

1
by: Dave A | last post by:
The following C code specifies the interface into a DLL. I need to access it from C#. How do I do declare it? I have done simple ones before but this particular API requires a pointer to a struct...
19
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
3
by: Christian F | last post by:
Hi, I'm a C-newbie and I would like to know if I am doing something wrong in the code below. It is working, but I'm afraid it might not be correct because I don't really understand everything of...
29
by: Hamish | last post by:
I'm trying to use an C API which is for geometry calculations. The function requires an argument for an array of polygons: coordpt **polygons //-1] where coordpt is: typedef struct {...
3
by: Michel Rouzic | last post by:
It's the first time I try using structs, and I'm getting confused with it and can't make it work properly I firstly define the structure by this : typedef struct { char *l1; int *l2; int Nval; }...
17
by: goldfita | last post by:
I saw some code that appeared to do something similar to this struct foo { char offset; int d; }; struct foo { int a; int b;
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
64
by: Zytan | last post by:
I know there are no pointers in C#, but if you do: a = b; and a and b are both arrays, they now both point to the same memory (changing one changes the other). So, it makes them seem like...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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...

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.