By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,689 Members | 1,109 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,689 IT Pros & Developers. It's quick & easy.

Converting #ifdef and #ifndef to C#

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a


<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

P: n/a
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

P: n/a


<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

P: n/a
<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

P: n/a
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

P: n/a


<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 discussion thread is closed

Replies have been disabled for this discussion.