473,386 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Access hardware directly via C#?

Greetings my dear friend,

Let's say I wanted to write an application that talks directly to a
network card. For example, say I'm designing a firewall or packet sniffer.
Do I need to use unmanaged code to do this? I'm wondering how I should
arrange the "division of labor" between managed and unmanaged code.

Let's make the example even simpler by saying that I want to design a
form with a button on it. When the button is clicked, I want to send a
packet out the ethernet card. The packet needs to be sent by talking
directly to the Ethernet card drivers, not via a standard interface like
Winsock [yes, I know that there's no practical reason to design such an
application -- it's just an example]..

So my goal is to make all of my code "managed" except what absolutely
MUST be unmanaged. So the form would be designed in C# -- as would be the
handler for the button... but when the button is clicked, I'll need to talk
to a network driver. So at that point, I'll need to use unmanaged C++ (??).
How would I handle this? As far as I know, I can't mix managed C# and
unmanaged C++ in the same assembly (can I?)... Would I design a totally
separate C++ DLL which exposes a SendPacket() method?

I'm not asking for any specific code, just general *concepts* that I can
research further in the online help, books, etc. I'm just not sure where to
begin.

I.P. Freely
Nov 16 '05 #1
4 4659
Hey IP !

"I.P. Freely" <ye*********@barnyard.glum> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
So my goal is to make all of my code "managed" except what absolutely
MUST be unmanaged. So the form would be designed in C# -- as would be the
handler for the button... but when the button is clicked, I'll need to talk to a network driver. So at that point, I'll need to use unmanaged C++ (??).

Yes, and no.
You can call into existing Win32 APIs (and pretty much any DLL) via
p/Invoke.
You don't have to author those DLLs in unmanaged C++. They could have been
authored in "anything".
How would I handle this? As far as I know, I can't mix managed C# and
unmanaged C++ in the same assembly (can I?)...
No, not with the current .NET Framework.
Would I design a totally
separate C++ DLL which exposes a SendPacket() method?

Yes, why not? and then the DLL needs to be on the path. It need not be
written in VC7.0 or VC7.1 ("unmanaged C++"). You could write it in VC6 or
something else.
I'm not asking for any specific code, just general *concepts* that I can research further in the online help, books, etc. I'm just not sure where to begin.

I.P. Freely

Nov 16 '05 #2
> Let's say I wanted to write an application that talks directly to a
network card. For example, say I'm designing a firewall or packet sniffer.
Do I need to use unmanaged code to do this? I'm wondering how I should
arrange the "division of labor" between managed and unmanaged code.

As far as I know, only device drivers can access hardware directly on the
modern OS.
Only on Win95, Win98 and WinMe, you could access the hardware directly.

So you will have to create a unmanaged network driver in your example that
does what you want and then you can use managed/unmanaged programs that
connects to this custom driver and executes the things you want.
Nov 16 '05 #3
> So you will have to create a unmanaged network driver in your example that
does what you want and then you can use managed/unmanaged programs that
connects to this custom driver and executes the things you want.


So if a pre-existing driver exposes the functionality that I need, I
could talk to that driver from managed code? For example, I could build an
IP packet in C# and send it out the Ethernet card by talking to the network
driver directly? Is a driver just a DLL that can be called using the
P/Invoke method that Dino mentioned?

Nov 16 '05 #4
> Let's make the example even simpler by saying that I want to design a
form with a button on it. When the button is clicked, I want to send a
packet out the ethernet card. The packet needs to be sent by talking
directly to the Ethernet card drivers, not via a standard interface like
Winsock [yes, I know that there's no practical reason to design such an
application -- it's just an example]..
....no, there isn't. If you wait till a situation when you actually need to
do that
comes along, you may soon get the impression that one will never exist.
So my goal is to make all of my code "managed" except what absolutely
MUST be unmanaged. So the form would be designed in C# -- as would be the
handler for the button... but when the button is clicked, I'll need to talk to a network driver. So at that point, I'll need to use unmanaged C++ (??). How would I handle this? As far as I know, I can't mix managed C# and
unmanaged C++ in the same assembly (can I?)... Would I design a totally
separate C++ DLL which exposes a SendPacket() method?
Yes. This is the most direct way to call C++ code directly from C#. The only
other way is COM, which involves more overhead.

I'm not asking for any specific code, just general *concepts* that I can research further in the online help, books, etc. I'm just not sure where to begin.


Think about what you *actually* want to do?
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: I.P. Freely | last post by:
Greetings my dear friend, Let's say I wanted to write an application that talks directly to a network card. For example, say I'm designing a firewall or packet sniffer. Do I need to use...
2
by: Wolfgang | last post by:
Dear all, I'm using GPIO (general purpose IO) of a motherboard. Accessing SMBus Components directly by the Registers of the SMBus Controller. Has anybody a more elegant way not to bind the...
13
by: BigDaDDY | last post by:
Um yeah....In case you haven't figured it out, Microsoft sucks. I'm going to be kicked back in my chair eating popcorn and watching football 10 years from now, while all you clowns are scrambling...
6
by: agnivesh | last post by:
Hi there, I'm a beginner in C programming and want to learn how to write C (pure C) code to access hardware ports (serial,parallel,usb etc.) under windows environment ? 1. Please, suggest any...
8
by: Sarah | last post by:
I need to access some data on a server. I can access it directly using UNC (i.e. \\ComputerName\ShareName\Path\FileName) or using a mapped network drive resource (S:\Path\FileName). Here is my...
12
by: david.brown.0 | last post by:
I'm trying to make a Java program access a parallel port. Java's comm API does not provide me with the control I need. I need to be able to write to the data and control pins and read the status...
17
by: Flic | last post by:
Is this possible? I know about a bit about ODBC and found how to import an ODBC database stored on the computer, however I am after accessing an SQL database stored on a webserver. I'd like to...
110
by: alf | last post by:
Hi, is it possible that due to OS crash or mysql itself crash or some e.g. SCSI failure to lose all the data stored in the table (let's say million of 1KB rows). In other words what is the worst...
5
by: =?Utf-8?B?Q2hyaXN0aWFuIEhhdmVs?= | last post by:
Hi, how can I access the RS-232 hardware interface using C# and .NET2.0 to send and receive messages to a hardware component? Christian
10
by: Les Desser | last post by:
In article <fcebdacd-2bd8-4d07-93a8-8b69d3452f3e@s50g2000hsb.googlegroups.com>, The Frog <Mr.Frog.to.you@googlemail.comMon, 14 Apr 2008 00:45:10 writes Thank you for that. It was very...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.