473,320 Members | 2,164 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,320 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 26351


<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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.