473,394 Members | 1,306 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.

Converting #ifdef and #ifndef to C#

I am converting a couple of C header files to C#. It is mainly just a
bunch C structs but I am not sure how to handle the #ifdef and #ifndef
in C#. For example:
#ifndef DATE_TIME
#define DATE_TIME unsigned long
#endif // NOT DATE_TIME

The #define DATE_TIME I am handling with:
public const UInt32 DATE_TIME;

What should be the rigfht approach to convert this type of C code?

Thanks,
Garyb

Nov 25 '06 #1
6 26368


<ca***********@yahoo.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
>I am converting a couple of C header files to C#. It is mainly just a
bunch C structs but I am not sure how to handle the #ifdef and #ifndef
in C#. For example:
#ifndef DATE_TIME
#define DATE_TIME unsigned long
#endif // NOT DATE_TIME

The #define DATE_TIME I am handling with:
public const UInt32 DATE_TIME;
That's not the same thing. The C code declares a type alias, and you just
defined a single const variable instance.
What should be the rigfht approach to convert this type of C code?
Well you have two choices.

Either retain the C type alias. In C# the 'using' directive can create a
type alias for a single file. At the top of the file add:

using DATE_TIME = System.Int32;

Or just replace all occurances of DATE_TIME with the corresponding CLR type,
Int32.

Which to use? If you're hoping to retain the flow and "flavor" of the C
code, use the type alias. For instance if you are interoperating with a DLL
that uses these types, the type alias is appropriate.

If you are doing a more substantial rewrite, you should get rid of the type
alias because in normal C# usage they are very rarely used.

David
Nov 25 '06 #2
Thanks for the response.
Actually I made a mistake in my previous post. I relaced #define
DATE_TIME unsigned long with
public UInt32 DATE_TIME;
I didn't mean to add const. And yes I do intend to get rid of the type
alias.

I do have quite a bit of code like:
#define FP_STATUS_QUERY 0x10
which I am replacing with
public const byte FP_STATUS_QUERY = 0x10;

But what about the
#ifndef DATE_TIME
#endif

Garyb
On Nov 25, 10:09 am, "David Browne" <davidbaxterbrowne no potted
m...@hotmail.comwrote:
<canoewhite...@yahoo.comwrote in messagenews:11**********************@m7g2000cwm.go oglegroups.com...>I am converting a couple of C header files to C#. It is mainly just a
bunch C structs but I am not sure how to handle the #ifdef and #ifndef
in C#. For example:
#ifndef DATE_TIME
#define DATE_TIME unsigned long
#endif // NOT DATE_TIME
The #define DATE_TIME I am handling with:
public const UInt32 DATE_TIME;That's not the same thing. The C code declares a type alias, and you just
defined a single const variable instance.
What should be the rigfht approach to convert this type of C code?Well you have two choices.

Either retain the C type alias. In C# the 'using' directive can create a
type alias for a single file. At the top of the file add:

using DATE_TIME = System.Int32;

Or just replace all occurances of DATE_TIME with the corresponding CLR type,
Int32.

Which to use? If you're hoping to retain the flow and "flavor" of the C
code, use the type alias. For instance if you are interoperating with a DLL
that uses these types, the type alias is appropriate.

If you are doing a more substantial rewrite, you should get rid of the type
alias because in normal C# usage they are very rarely used.

David
Nov 25 '06 #3


<ca***********@yahoo.comwrote in message
news:11**********************@45g2000cws.googlegro ups.com...
Thanks for the response.
Actually I made a mistake in my previous post. I relaced #define
DATE_TIME unsigned long with
public UInt32 DATE_TIME;
I didn't mean to add const. And yes I do intend to get rid of the type
alias.

I do have quite a bit of code like:
#define FP_STATUS_QUERY 0x10
which I am replacing with
public const byte FP_STATUS_QUERY = 0x10;
That's correct. That #define just creates a constant value.
But what about the
#ifndef DATE_TIME
#endif
. . ..
You don't need it. Since C# has no notion of include files, you don't need
to check if the symbol has been previously defined. Just add

using DATE_TIME = System.UInt32;

To the top of the file, and you can use DATE_TIME as a type alias for
UInt32.

David
Nov 25 '06 #4
<ca***********@yahoo.comwrote in message
news:11**********************@45g2000cws.googlegro ups.com...
<snip>
I do have quite a bit of code like:
#define FP_STATUS_QUERY 0x10
which I am replacing with
public const byte FP_STATUS_QUERY = 0x10;

But what about the
#ifndef DATE_TIME
#endif

A common C/C++ idiom is to do some thing like this in a header file

#ifndef _FOOBAR
#define _FOOBAR

// _FOOBAR definitions

#endif //_FOOBAR

The purpose is to prevent multiple definitions from occurring when a
header file is includes multiple times.
From the small snippet that you provided, I can't be sure, but it does
look like this sort of thing.

If that is the case you should be able to safely ignore it in C#.
Note that I am ONLY talking about the
#ifndef _FOOBAR
#define _FOOBAR
Type of preprocessor stuff here. Other conditional preprocessor logic
MIGHT be relevant
Bill


Nov 25 '06 #5
Here is some actual code I need to replace and is probably the most
troubling:

#ifndef DATE_TIME
#define DATE_TIME unsigned long
#endif
#ifndef TIME_64
#ifdef _WIN32
#define TIME_64 __time64_t
#else // NOT _WIN32
#define TIME_64 STRUCT {DATE_TIME date_time; ULONG
date_time_extension;}
#endif
#endif

typedef STRUCT
{
FSD_PKT_HEADER pkt_header;
UCHAR status_type;
USHORT data_size;
TIME_64 current_date_time;
USHORT crc_16;
} ST_CURRENT_TIME_PKT;

I realize that __time64_t is not directly supported in .NET and could
use some suggestions in dealing with aconversion.

Thanks,
Gary

Nov 25 '06 #6


<ca***********@yahoo.comwrote in message
news:11**********************@f16g2000cwb.googlegr oups.com...
Here is some actual code I need to replace and is probably the most
troubling:

#ifndef DATE_TIME
#define DATE_TIME unsigned long
#endif
#ifndef TIME_64
#ifdef _WIN32
#define TIME_64 __time64_t
#else // NOT _WIN32
#define TIME_64 STRUCT {DATE_TIME date_time; ULONG
date_time_extension;}
#endif
#endif

typedef STRUCT
{
FSD_PKT_HEADER pkt_header;
UCHAR status_type;
USHORT data_size;
TIME_64 current_date_time;
USHORT crc_16;
} ST_CURRENT_TIME_PKT;

I realize that __time64_t is not directly supported in .NET and could
use some suggestions in dealing with aconversion.
Something like:

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace CInterop
{
using DATE_TIME = System.Int32;
using UCHAR = System.Char;
using USHORT = System.UInt16;
using ULONG = System.UInt32;
[StructLayout(LayoutKind.Sequential)]
internal struct TIME_64
{
public DATE_TIME date_time;
public ULONG date_time_extension;
}

[StructLayout(LayoutKind.Sequential)]
internal struct ST_CURRENT_TIME_PKT
{
public IntPtr pkt_header;
public UCHAR status_type;
public USHORT data_size;
public TIME_64 current_date_time;
public USHORT crc_16;
}
}

David

Nov 25 '06 #7

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

Similar topics

7
by: Anonymous | last post by:
I have a mixed-language pgm (Fortran and C++) which needs to pass a few hundred values from C++ to Fortran. One way to do this is to have a Fortran module and a C++ structure (or class) with an...
5
by: lovecreatesbeauty | last post by:
Do #ifdef or #ifndef have some defects? I ever heard that some people use #if defined() or #if !defined() instead of using #ifdef or #ifndef in header file.
6
by: Michael B Allen | last post by:
Which is the preferred method for preprocessor tests and why? #ifdef XYZ or #if XYZ or #if defined(XYZ) and
2
by: Paolo | last post by:
I imported a VC++6.0 project into VC++7.1. The conversion operation makes a mess with Preprocessor Definitions, adding a "$(NoInherit)" for each file. For example: I had a DLL project in VC++6.0...
1
by: Michael Sgier | last post by:
Hi I get the error: No case-independent string comparison (stricmp, strcasecmp) with the code below. Why...where should stricmp be defined? And how do i get rid of the error on Linux? // //...
5
by: anushhprabu | last post by:
#include <stdio.h> #include <ctype.h> #define DEBUG 1 main() { char inbuf; int i = 0; int lcnt = 0; gets(inbuf); while(*(inbuf+i))
1
by: ajaypatel19 | last post by:
I have code like #ifndef ABC #define ABC ... ... #ifdef XYZ ... ..
6
by: anirbid.banerjee | last post by:
Hi, I need to write a macro which would have a lot many conditional #ifdef ... #endif blocks in it. #define _xx_macro (x) { ... \ ... \ /* some code (); */ #ifdef _SOME_STMT \ ... \ ... \
15
by: allthecoolkidshaveone | last post by:
I want to convert a string representation of a number ("1234") to an int, with overflow and underflow checking. Essentially, I'm looking for a strtol() that converts int instead of long. The...
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: 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
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
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
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.