473,698 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 26442


<ca***********@ yahoo.comwrote in message
news:11******** **************@ m7g2000cwm.goog legroups.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" <davidbaxterbro wne no potted
m...@hotmail.co mwrote:
<canoewhite...@ yahoo.comwrote in messagenews:11* *************** ******@m7g2000c wm.googlegroups .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.goog legroups.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.goog legroups.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_exten sion;}
#endif
#endif

typedef STRUCT
{
FSD_PKT_HEADER pkt_header;
UCHAR status_type;
USHORT data_size;
TIME_64 current_date_ti me;
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.goo glegroups.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_exten sion;}
#endif
#endif

typedef STRUCT
{
FSD_PKT_HEADER pkt_header;
UCHAR status_type;
USHORT data_size;
TIME_64 current_date_ti me;
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.Collecti ons.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(La youtKind.Sequen tial)]
internal struct TIME_64
{
public DATE_TIME date_time;
public ULONG date_time_exten sion;
}

[StructLayout(La youtKind.Sequen tial)]
internal struct ST_CURRENT_TIME _PKT
{
public IntPtr pkt_header;
public UCHAR status_type;
public USHORT data_size;
public TIME_64 current_date_ti me;
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
5207
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 identical data layout. I have used this idea with no problem, but it necessitates making changes very carefully to the Fortran module and the C++ structure to keep them "in sync". I am looking for a program which translates source files between...
5
5777
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
74347
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
6637
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 where the definitions were: _UNICODE,_DEBUG,_WIN32_DCOM,WIN32,_WINDOWS,_WINDLL,_AFXDLL,_USRDLL In VC++7.1, these are the preprocessor definitions of the project (right-click the project in Solution Explorer and choose Properties -> C++ ->...
1
3216
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? // // Function portability // #ifndef HAVE_STRICMP
5
3538
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
4313
by: ajaypatel19 | last post by:
I have code like #ifndef ABC #define ABC ... ... #ifdef XYZ ... ..
6
27846
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
9654
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 problem with strtol() is that a number that fits into a long might be too big for an int. sscanf() doesn't seem to do the over/underflow checking. atoi(), of course, doesn't do any checking. I've long thought it odd that there aren't strtoi() and...
0
9164
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
9029
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...
0
8870
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7734
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6524
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
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.