Solved

C# solution for C++ marcos

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET 5 Templates 2 78
Sent email using Shared Mailbox from outlook in C# 5 24
List<PaisEntity> - show some 1 24
insert value of checklistbox checked 4 27
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
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…

777 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