PC Review


Reply
Thread Tools Rate Thread

Access hardware directly via C#?

 
 
I.P. Freely
Guest
Posts: n/a
 
      27th May 2004
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


 
Reply With Quote
 
 
 
 
Dino Chiesa [Microsoft]
Guest
Posts: n/a
 
      27th May 2004
Hey IP !

"I.P. Freely" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...

> 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
>
>



 
Reply With Quote
 
Guest
Posts: n/a
 
      27th May 2004
> 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.


 
Reply With Quote
 
I.P. Freely
Guest
Posts: n/a
 
      27th May 2004
> 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?



 
Reply With Quote
 
B0nj
Guest
Posts: n/a
 
      27th May 2004
> 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?


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Directly Access To Filesystem? Wiederanders Microsoft Dot NET Compact Framework 4 30th Oct 2008 03:02 PM
Is it possible to return defective hardware directly to Asus ? Skybuck The Destroyer DIY PC 3 3rd Jun 2007 04:21 AM
16-bit application commanding ISA hardware directly =?Utf-8?B?RGFuaWVsIFE=?= Windows XP Hardware 1 23rd May 2006 04:39 PM
Can I directly access hardware using managed code? Chris Tacke, eMVP Microsoft Dot NET Compact Framework 1 10th Nov 2004 09:37 PM
Access hardware directly via C#? I.P. Freely Microsoft C# .NET 4 27th May 2004 09:09 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:36 PM.