Solved

C# solution for C++ marcos

Posted on 2004-08-18
2
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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!
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

730 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