Solved

C# solution for C++ marcos

Posted on 2004-08-18
2
243 Views
Last Modified: 2010-05-19
As an exercise I am currently porting some of my code from C/C++ to C#. Since the Framework does not have the low level Win32 support I have to link directly to the dll exports, namely DeviceIoControl, CreateFile, ReadFile, WriteFile.
While all this works like a charm I am running into a snag. I am porting over all the defined values in the Win32 header files. Many of them are defined through macros, especially the DeviceIoControlCodes.
What would be an elegant solution in C# so I don't have to hardcode each value but would be able to make them dependant on a few parameters.

For example:

Original macro from the "winioctl.h" file:
#define CTL_CODE( DeviceType, Function, Method, Access ) (((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method))
This makes if very simple to define private control codes

How would this look like in C# without hardcoding the value?
0
Comment
Question by:gsteup
2 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11833338
there are no macros such as this ...

the appropriate way would be to create a sstatic helper class of methods ... and enums for your values being passed
0
 
LVL 3

Accepted Solution

by:
bigjim2000 earned 100 total points
ID: 11844550
You don't need to link to the CreateFile, ReadFile, WriteFile exports... .those are built into .NET in the System.IO namespace.

As for the #define... no, you can't do that, as such, in C#.

For that elegant solution, I'd just make your deviceIO class in C++, then import it into C# :-p  but that is just me.

C# doesen't do too well with the low level API stuff, save for using pointers.  It is getting better all the time, and people are developing code to work with hardware, but C++ is still better for that kind of stuff IMHO.

-Eric
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question