473,699 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(La youtKind.Explic it)]
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_CA N_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->dataUshortPt r[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_CA N_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 4504
oops, forgot the "&" in the c# line:
fixed(SERIAL_CA N_MSG_TYPE * msgPtr = (SERIAL_CAN_MSG _TYPE*) &msg[0])
kelli

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

[StructLayout(La youtKind.Explic it)]
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.PtrToSt ructure((IntPtr ) msgPtr, newStruct);

fixed(ushort *ushortPtr = newStruct.dataU shortPtr)
{
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.goo glegroups.com.. .
oops, forgot the "&" in the c# line:
fixed(SERIAL_CA N_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.Structu reToPtr(newStru ct, (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.goo glegroups.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
368
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 that contains an array of other structs. typedef struct { int nWidth; int nHeight; DWORD dwFlags; } HCA_MODE; typedef struct
19
2844
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 > >pointer-to-array-of-size-N-of-type-T (which is fine) and not having type > >array-of-size-N-of-type-T (with some exceptions, which is curious). > > So far > >the consensus seems to be that while everyone is aware of this no one knows
21
3926
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 "browse" it). So I expected that when I run this program, I get both c1.A and c2.A pointing to the same address, and changing c1.A means that also c2.A changes too. ----- BEGIN example CODE -----------
3
2170
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 it. There are lots of pointers and pointers to pointers which makes me confused. First my typedef: typedef struct { double re;
29
1977
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 { double x;
3
2843
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; } *arrays; It's supposed to be a structure containing an array of chars, an array of ints and an int. I declare functions like this : arrays *parseline(char *line, int N)
17
2568
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
19634
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) What's the difference between these 3 statements: (i) memcpy(&b, &KoefD, n); // this works somewhere in my code
64
3417
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 pointers. Can someone please explain why? thanks. Zytan
0
8691
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8620
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
9180
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9038
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
8920
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,...
1
6536
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
4633
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3060
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
3
2012
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.