Async io and OVERLAPPED is going to be a pain point as well. Remember that structs are value types which means you cannot use them for async io if they are stack based. If they are embedded in a reference type, you are going to need to pin the object for the duration of the async call and free it when it has completed (remember that the actual pointer value of the overlapped struct must remain the same and cannot be relocated by the GC)
d
-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of Thomas F. Divine
Sent: Tuesday, February 05, 2008 11:21 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] How to interact w/ driver from .NET?
-----Original Message-----
From: xxxxx@lists.osr.com [mailto:bounce-313802-
xxxxx@lists.osr.com] On Behalf Of xxxxx@hotmail.com
Sent: Tuesday, February 05, 2008 1:55 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] How to interact w/ driver from .NET?
I understand the intention of the WDK examples are primarily focused on
driver fundamentals, but an area that I haven’t (until now) had to
approach is how to interface with my drivers from a managed language
like C#.
I’ve scoured the whitepapers, and haven’t found any universal way to do
it.
What are the preferred methods for creating a good user mode API that
can be used by .NET applications?
Actually, the same methods are used from a managed C# application as from an unmanaged ‘C’ application.
Use the Win32 CreateFile, DeviceIoControl, ReadFile, Close, etc. APIs.
The only nuisance is that these are unmanaged APIs, so you must use P/Invoke to call them in most cases.
In addition, you will have to do some work to convert ‘C’ struct definitions to forms suitable for use in managed code. Study the StructLayout attribute and friends. This will be tedious at first. You will probably learn that there are ways to define structs that make it easier to adapt them to work with managed code.
In some cases it is simply a LOT easier to create an unmanaged supporting ‘C’ DLL that does work that can probably be accomplished in C#, but requires a lot (read “too much”…) of work. In other words, try to use P/Invoke and C# as much as possible, but do not think that a “pure C#” application is a must.
If you are asking this question, then you have a lot of study and experimentation to do before you will be comfortable with this approach, but it is the way.
The pinvoke.net site is helpful in some cases.
Good luck,
Thomas F. Divine
NTDEV is sponsored by OSR
For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars
To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer