Solved

C# solution for C++ marcos

Posted on 2004-08-18
2
241 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now