Solved

C# solution for C++ marcos

Posted on 2004-08-18
2
240 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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

763 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

7 Experts available now in Live!

Get 1:1 Help Now