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

sizeof structure at compile time

Hi,

I'm new to C#, and am converting a C++ app which interfaces with a legacy Delphi app. We pass messages between the two apps over UDP, and my colleague is writing a conversion utility to take his Delphi message structure definitions and produce the equivalent in C#. However we have hit upon several issues, which I can't find a way round. The main one seems though to be when using the size of a previously defined message structure to define a constant for use elsewhere. So, in Delphi we have:

Expand|Select|Wrap|Line Numbers
  1. type
  2.   T_UDP_pkt = array[0..MAX_UDP_PACKET_SIZE - 1] of char;
  3.   T_UDP_header = packed record
  4.     From_MAC: T_MAC;
  5.     From_Area_ID: word; // 0= PC
  6.     Signature: array[0..5] of char;
  7.     Pkt_count: word; // Total number of packets in this set
  8.     Pkt_number: word; // Packet Number
  9.     Transaction: longword;
  10.     spare1: array[0..5] of byte;
  11.     AC_to_AC: array[0..3] of byte;
  12.     datalen: longword; // length of data block following header
  13.     Code: longword;
  14.     Last_received_transaction: longword;
  15.     Hash: TDigest;
  16.     AC_WAN_Port: word;
  17.     Spare2: array[0..7] of byte;
  18.   end;
  19. const MAX_UDP_DATA_SIZE = (sizeof(T_UDP_pkt) - sizeof(T_UDP_header));
I've managed to create the T_UDP_pkt and T_UDP_Header strutures in C# with

Expand|Select|Wrap|Line Numbers
  1. [StructLayout(LayoutKind.Sequential, Pack = 1)]
and
[MarshalAs(UnmanagedType.ByValArray, SizeConst = x)] commands,

but I can't define the constant MAX_UDP_DATA_SIZE as if I use

Expand|Select|Wrap|Line Numbers
  1. Marshal.Sizeof(T_UDP_pkt) 
I get an error about using a type when a variable is expected, or if I use

Expand|Select|Wrap|Line Numbers
  1. Marshal.Sizeof(typeof(T_UDP_pkt))
I get an error about the value not being constant. And if I use

Expand|Select|Wrap|Line Numbers
  1. sizeof(T_UDP_pkt)
I get an error saying I can't get the size of a managed type. Finally if I use

s
Expand|Select|Wrap|Line Numbers
  1. izeof(typeof(T_UDP_pkt))
I get an error that typeof(T_UDP_pkt) is not a type.

So I'm a bit stumped. Any help would be muich appreciated, as I'm sure it must be reasonably easy?!
Mar 19 '10 #1
2 2595
tlhintoq
3,525 Expert 2GB
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Mar 19 '10 #2
Thanks, sorry about that, tlhintoq, next time!
Mar 19 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Christof Warlich | last post by:
Hi, I'm trying to build an _efficient_, _purely_ _abstract_ API for inter process(or) communication, _completely_ hiding any implementation details. The core components are "Buffer", "Address"...
15
by: signuts | last post by:
I'm aware of what sizeof(...) does, what I would like to know is if sizeof(...) is compiled in or a function that's executed at run-time. Like for example { int a; printf("a is %d...
19
by: Martin Pohlack | last post by:
Hi, I have a funtion which shall compute the amount for a later malloc. In this function I need the sizes of some struct members without having an instance or pointer of the struct. As...
33
by: Chris Fogelklou | last post by:
What is wrong with the above? Don't worry, I already know (learned my lesson last week.) It is for the benefit of our resident compiler guru who seems to think you need the cast. I thought it...
7
by: dam_fool_2003 | last post by:
#include<stdio.h> int main(void) { unsigned int a=20,b=50, c = sizeof b+a; printf("%d\n",c); return 0; } out put: 24
18
by: Kenneth Brody | last post by:
Is sizeof() legal within a #if preprocessor line? Specifically, I would like to use the following "sanity check" at compile time, but I'm getting an error on the compile about the #if line. ...
90
by: pnreddy1976 | last post by:
Hi, How can we write a function, which functionality is similar to sizeof function any one send me source code Reddy
10
by: Yevgen Muntyan | last post by:
Consider the following macro: #define ALLOCIT(Type) ((Type*) malloc (sizeof (Type))) The intent is to wrap raw memory allocation of N bytes into a macro which returns allocated chunk of memory...
34
by: Raman | last post by:
Hi All, Is ut possible to calculate size of any standard data types at run time i.e without using sizeof() operator Thanks and Regards, Raman
27
by: CodeMonk3y | last post by:
gotta question on sizeof keyword does the sizeof keyword calcuates the size at compile time or run time ?? -- Posted on news://freenews.netfront.net - Complaints to news@netfront.net --
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?
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
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...

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.