473,508 Members | 2,370 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

encapsulate c enum's inC++/CLI for C#

I am writing C# code that interacts with a external device's firmware
which is writen in C. There is a common enum file the firmware code is
using that I would like to consume in the C# world. I understand that
C# cannot use C header files directly, so I thought I could wrap the
enum file in a C++/CLI assembly. So I have done something like this:

------------------------------------
#ifdef __cplusplus_cli
namespace MyCompany::InstrumentCommsEnums
{
#endif

#define CT_MAXZONE 0x7
#define CT_MAXCHANNEL 0x8
#define CT_ADCCHANNEL 0xB

typedef enum toggleonoff{
TOG_OFF = 0,
TOG_ON = 1
}TOGGLEONOFF;

#ifdef __cplusplus_cli
}
#endif
------------------------------------

With hopes of getting to the toggleonoff enum this way:

MyCompany.InstrumentCommsEnums.toggleonoff.TOG_OFF

After referring the C++/CLI project in the C# code, there is no
MyCompany.InstrumentCommsEnums namespace. Any thoughts?

Jan 30 '06 #1
9 2432
ca******@gmail.com wrote:
I am writing C# code that interacts with a external device's firmware
which is writen in C. There is a common enum file the firmware code
is using that I would like to consume in the C# world. I understand
that C# cannot use C header files directly, so I thought I could wrap
the enum file in a C++/CLI assembly. So I have done something like
this:

------------------------------------
#ifdef __cplusplus_cli
namespace MyCompany::InstrumentCommsEnums
{
#endif

#define CT_MAXZONE 0x7
#define CT_MAXCHANNEL 0x8
#define CT_ADCCHANNEL 0xB

typedef enum toggleonoff{
TOG_OFF = 0,
TOG_ON = 1
}TOGGLEONOFF;

#ifdef __cplusplus_cli
}
#endif
------------------------------------

With hopes of getting to the toggleonoff enum this way:

MyCompany.InstrumentCommsEnums.toggleonoff.TOG_OFF

After referring the C++/CLI project in the C# code, there is no
MyCompany.InstrumentCommsEnums namespace. Any thoughts?


C++/CLI supports two kinds of enum:

enum foo
{
// traditional C/C++ enum
};

enum class bar
{
// .NET enum
};

Change your wrapper to something like

------------------------------------

#define CT_MAXZONE 0x7
#define CT_MAXCHANNEL 0x8
#define CT_ADCCHANNEL 0xB

#ifdef __cplusplus_cli
namespace MyCompany::InstrumentCommsEnums
{
enum class toggleonoff
#else
typedef enum toggleonoff
#endif
{
TOG_OFF = 0,
TOG_ON = 1
}TOGGLEONOFF;

#ifdef __cplusplus_cli
}
#endif
------------------------------------

.... and it might just work. Of course, you can also re-type the enum(s) in
..NET form, although there may be more chance of error along that route.

-cd
Jan 30 '06 #2
shouldn't the enum be declared public also?

kind regards,
Bruno.
"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:OT**************@TK2MSFTNGP10.phx.gbl...
ca******@gmail.com wrote:
I am writing C# code that interacts with a external device's firmware
which is writen in C. There is a common enum file the firmware code
is using that I would like to consume in the C# world. I understand
that C# cannot use C header files directly, so I thought I could wrap
the enum file in a C++/CLI assembly. So I have done something like
this:

------------------------------------
#ifdef __cplusplus_cli
namespace MyCompany::InstrumentCommsEnums
{
#endif

#define CT_MAXZONE 0x7
#define CT_MAXCHANNEL 0x8
#define CT_ADCCHANNEL 0xB

typedef enum toggleonoff{
TOG_OFF = 0,
TOG_ON = 1
}TOGGLEONOFF;

#ifdef __cplusplus_cli
}
#endif
------------------------------------

With hopes of getting to the toggleonoff enum this way:

MyCompany.InstrumentCommsEnums.toggleonoff.TOG_OFF

After referring the C++/CLI project in the C# code, there is no
MyCompany.InstrumentCommsEnums namespace. Any thoughts?


C++/CLI supports two kinds of enum:

enum foo
{
// traditional C/C++ enum
};

enum class bar
{
// .NET enum
};

Change your wrapper to something like

------------------------------------

#define CT_MAXZONE 0x7
#define CT_MAXCHANNEL 0x8
#define CT_ADCCHANNEL 0xB

#ifdef __cplusplus_cli
namespace MyCompany::InstrumentCommsEnums
{
enum class toggleonoff
#else
typedef enum toggleonoff
#endif
{
TOG_OFF = 0,
TOG_ON = 1
}TOGGLEONOFF;

#ifdef __cplusplus_cli
}
#endif
------------------------------------

... and it might just work. Of course, you can also re-type the enum(s)
in .NET form, although there may be more chance of error along that route.

-cd

Jan 30 '06 #3
I have the enum declared as public and I am including the enum.h file
in a enum.cpp file like the following code snippet and there is no
using namespace stuff in the header file anymore. The problem is I
still cannot see the MyCompany.InstrumentCommsEnums namespace in the C#
code.s

namespace MyCompany
{
namespace InstrumentCommsEnums
{

#include "enums.h"

}
}

Jan 30 '06 #4
I have the enum declared as public and I am including the enum.h file
in a enum.cpp file like the following code snippet and there is no
using namespace stuff in the header file anymore. The problem is I
still cannot see the MyCompany.InstrumentCommsEnums namespace in the C#
code.s

namespace MyCompany
{
namespace InstrumentCommsEnums
{

#include "enums.h"

}
}

Jan 30 '06 #5
I have the enum declared as public and I am including the enum.h file
in a enum.cpp file like the following code snippet and there is no
using namespace stuff in the header file anymore. The problem is I
still cannot see the MyCompany.InstrumentCommsEnums namespace in the C#
code.s

namespace MyCompany
{
namespace InstrumentCommsEnums
{

#include "enums.h"

}
}

Jan 30 '06 #6
I have the enum declared as public and I am including the enum.h file
in a enum.cpp file like the following code snippet and there is no
using namespace stuff in the header file anymore. The problem is I
still cannot see the MyCompany.InstrumentCommsEnums namespace in the C#
code.s

namespace MyCompany
{
namespace InstrumentCommsEnums
{

#include "enums.h"

}
}

Jan 30 '06 #7
Bruno van Dooren wrote:
shouldn't the enum be declared public also?


Yeah, that'd probably help :)

-cd
Jan 30 '06 #8
shouldn't the enum also be made public if you need it to be accessible by
others?

kind regards,
Bruno

"Carl Daniel [VC++ MVP]" wrote:
ca******@gmail.com wrote:
I am writing C# code that interacts with a external device's firmware
which is writen in C. There is a common enum file the firmware code
is using that I would like to consume in the C# world. I understand
that C# cannot use C header files directly, so I thought I could wrap
the enum file in a C++/CLI assembly. So I have done something like
this:

------------------------------------
#ifdef __cplusplus_cli
namespace MyCompany::InstrumentCommsEnums
{
#endif

#define CT_MAXZONE 0x7
#define CT_MAXCHANNEL 0x8
#define CT_ADCCHANNEL 0xB

typedef enum toggleonoff{
TOG_OFF = 0,
TOG_ON = 1
}TOGGLEONOFF;

#ifdef __cplusplus_cli
}
#endif
------------------------------------

With hopes of getting to the toggleonoff enum this way:

MyCompany.InstrumentCommsEnums.toggleonoff.TOG_OFF

After referring the C++/CLI project in the C# code, there is no
MyCompany.InstrumentCommsEnums namespace. Any thoughts?


C++/CLI supports two kinds of enum:

enum foo
{
// traditional C/C++ enum
};

enum class bar
{
// .NET enum
};

Change your wrapper to something like

------------------------------------

#define CT_MAXZONE 0x7
#define CT_MAXCHANNEL 0x8
#define CT_ADCCHANNEL 0xB

#ifdef __cplusplus_cli
namespace MyCompany::InstrumentCommsEnums
{
enum class toggleonoff
#else
typedef enum toggleonoff
#endif
{
TOG_OFF = 0,
TOG_ON = 1
}TOGGLEONOFF;

#ifdef __cplusplus_cli
}
#endif
------------------------------------

.... and it might just work. Of course, you can also re-type the enum(s) in
..NET form, although there may be more chance of error along that route.

-cd

Jan 30 '06 #9
Cleaned up the header file some more and I can see the namespace now.
Sorry for the four posts, Google didn't seem to be responding so I kept
hiting post, opps!

Jan 30 '06 #10

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

Similar topics

1
6869
by: Douglas | last post by:
Sorry for the re-post, ISP troubles grr... anyways... Gday All, I am developing a DLL (using VB6) for use in an IIS5 ASP Application. This DLL contains a series of 'Public Enum'(s)
8
4681
by: Kamil Grymuza | last post by:
Hi Is it possible to have a name of enumerated value in string somehow? enum { ID1 = 1, ID2=2, } And can I somehow make something like cout<<ID1<<endl; and heve printed "ID1" not 1?
8
8494
by: Bruno BAGUETTE | last post by:
Hello, I have to migrate a MySQL database to a PostgreSQL database without procedures. The problem is that this MySQL database uses ENUM, do you see what can I do to migrate ENUM into...
5
4020
by: John Smith | last post by:
I have a custom enum to list comparison operators: =, <, <=, >, >= . I have created it as follows: public enum Comparator {Equal, LessThan, LessThanOrEqual,GreaterThan,GreaterThanOrEqual} Now...
20
3471
by: Alexander Muylaert | last post by:
Hi I hate this syntax MyEnum aEnum; ... if ((aEnum == MyEnum.Green) || (aEnum == MyEnum.Red) || (aEnum == MyEnum.Yellow)) ... In delphi we have something like
8
8203
by: SteveK | last post by:
I'm getting the error: "Cannot implicitly convert type 'MovesDBMigrate.MotionNameElementTypes' to 'int'" for this line of code: m_nameElementTableNames = "Tbl_NameCharacters"; Of course if...
1
10386
by: Dwight.Dexter | last post by:
I have an enum variable. Is there some way to cast an int or BitArray and get my enum value? I have to communicate with an old C-style sever so I get an array of bits("0","1"). But I'm using an...
2
2194
by: Ray Cassick \(Home\) | last post by:
I have a function that takes a value is as System.Enum and I want to be able to look at that value and determine if it is a regular enum or an enum that has a <Flag()> attribute set on it. I am...
10
3868
by: Randy | last post by:
Hi, Can anyone point me to a complete, compilable example of Besser's ENUM++ mechanism? I downloaded it from CUJ and gave it a try but got errors just trying to compile the header enum.h. ...
0
7224
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
7118
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7323
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,...
0
7379
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...
1
7038
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
5625
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,...
0
4706
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...
0
3192
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...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.