Overlapped I/O and C#

If there are any C# folks out there can you tell me how do true overlapped
I/O to a driver with C#? I have a driver for a client where it is best if
they have a pool of N requests and keep firing them ASAP. In regular C I
would be using a either a pool of overlapped structures or even an I/O
completion port model. While I have done a little C# asynchronous I/O is
not somewhere I have wandered and with wonderful comments such as “Don’t
initiate a second write before the first completes” that appears in the
BeginWrite call which is implied to be async, I am wondering if this is
doable?

Don Burn
Windows Filesystem and Driver Consulting
Website: http://www.windrvr.com
Blog: http://msmvps.com/blogs/WinDrvr

C# can call any native C API via the PInvoke facility, so if you really want you can do overlapped I/O the same way you would do it from C.

I’d have to go look, but there almost certainly are already C# classes to do async I/O, which would be easier than using the PInvoke facility. I know there are some C# samples that do async I/O on codeproject.com.

A quick Google search finds the C# class System.Threading.Overlapped, which is a managed wrapper for the overlapped structure used from C. There must also be classes that use System.Threading.Overlapped.

Jan

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of Don Burn
Sent: Monday, December 9, 2013 9:32 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] Overlapped I/O and C#

If there are any C# folks out there can you tell me how do true overlapped I/O to a driver with C#? I have a driver for a client where it is best if they have a pool of N requests and keep firing them ASAP. In regular C I would be using a either a pool of overlapped structures or even an I/O completion port model. While I have done a little C# asynchronous I/O is not somewhere I have wandered and with wonderful comments such as “Don’t initiate a second write before the first completes” that appears in the BeginWrite call which is implied to be async, I am wondering if this is doable?

Don Burn
Windows Filesystem and Driver Consulting
Website: http://www.windrvr.com
Blog: http://msmvps.com/blogs/WinDrvr


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

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

Jan,

Googling this give you the msdn page for the Overlapped class, and a
whole bunch of samples from various places (all of which have comments that
they DON’T WORK). I can find nothing in the Microsoft documentation. Now I
could probably roll my own interface that hid the mess, but my client really
wants to use my driver with C# and pretty much standard classes.

Don Burn
Windows Filesystem and Driver Consulting
Website: http://www.windrvr.com
Blog: http://msmvps.com/blogs/WinDrvr

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Jan Bottorff
Sent: Monday, December 09, 2013 12:49 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] Overlapped I/O and C#

C# can call any native C API via the PInvoke facility, so if you really want
you can do overlapped I/O the same way you would do it from C.

I’d have to go look, but there almost certainly are already C# classes to do
async I/O, which would be easier than using the PInvoke facility. I know
there are some C# samples that do async I/O on codeproject.com.

A quick Google search finds the C# class System.Threading.Overlapped, which
is a managed wrapper for the overlapped structure used from C. There must
also be classes that use System.Threading.Overlapped.

Jan

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Don Burn
Sent: Monday, December 9, 2013 9:32 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] Overlapped I/O and C#

If there are any C# folks out there can you tell me how do true overlapped
I/O to a driver with C#? I have a driver for a client where it is best if
they have a pool of N requests and keep firing them ASAP. In regular C I
would be using a either a pool of overlapped structures or even an I/O
completion port model. While I have done a little C# asynchronous I/O is
not somewhere I have wandered and with wonderful comments such as “Don’t
initiate a second write before the first completes” that appears in the
BeginWrite call which is implied to be async, I am wondering if this is
doable?

Don Burn
Windows Filesystem and Driver Consulting
Website: http://www.windrvr.com
Blog: http://msmvps.com/blogs/WinDrvr


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

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


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

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

I kind of doubt that the classes are really broken. A lot of the comments in the c# user community I have seen are based on not understanding the lower level principals about which they think are non functional. Sending async io in C# is actually quite simple, the biggest task is to pin the buffers for the lifetime of the IO request using GCHandle.Alloc(, GCHandleType.Pinned) and then getting the buffer through ToIntPtr(). Your wrapper to ReadFile/WriteFile then take the IntPtr instead of the more specified type value. You must also declare your own OVERLAPPED definition and you can use the same trick as in native, where anything after the OVERLAPPED itself can be a part of an uber structure and hold your context data. You can get fancy and have classes create tasks to truly integrate into the async support in the compiler, but that is bonus points.

The standard classes do NOT cover DeviceIoControl, so if you client needs standard classes and IOCTL these are conflicting requirements.

d

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of Don Burn
Sent: Monday, December 9, 2013 9:55 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] Overlapped I/O and C#

Jan,

Googling this give you the msdn page for the Overlapped class, and a whole bunch of samples from various places (all of which have comments that they DON’T WORK). I can find nothing in the Microsoft documentation. Now I could probably roll my own interface that hid the mess, but my client really wants to use my driver with C# and pretty much standard classes.

Don Burn
Windows Filesystem and Driver Consulting
Website: http://www.windrvr.com
Blog: http://msmvps.com/blogs/WinDrvr

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Jan Bottorff
Sent: Monday, December 09, 2013 12:49 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] Overlapped I/O and C#

C# can call any native C API via the PInvoke facility, so if you really want
you can do overlapped I/O the same way you would do it from C.

I’d have to go look, but there almost certainly are already C# classes to do
async I/O, which would be easier than using the PInvoke facility. I know
there are some C# samples that do async I/O on codeproject.com.

A quick Google search finds the C# class System.Threading.Overlapped, which
is a managed wrapper for the overlapped structure used from C. There must
also be classes that use System.Threading.Overlapped.

Jan

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Don Burn
Sent: Monday, December 9, 2013 9:32 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] Overlapped I/O and C#

If there are any C# folks out there can you tell me how do true overlapped
I/O to a driver with C#? I have a driver for a client where it is best if
they have a pool of N requests and keep firing them ASAP. In regular C I
would be using a either a pool of overlapped structures or even an I/O
completion port model. While I have done a little C# asynchronous I/O is
not somewhere I have wandered and with wonderful comments such as “Don’t
initiate a second write before the first completes” that appears in the
BeginWrite call which is implied to be async, I am wondering if this is
doable?

Don Burn
Windows Filesystem and Driver Consulting
Website: http://www.windrvr.com
Blog: http://msmvps.com/blogs/WinDrvr


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

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


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

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


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

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

Look for IAsyncResult and friends.

Thomas F. Divine

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Don Burn
Sent: Monday, December 9, 2013 12:55 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] Overlapped I/O and C#

Jan,

Googling this give you the msdn page for the Overlapped class, and a
whole bunch of samples from various places (all of which have comments that
they DON’T WORK). I can find nothing in the Microsoft documentation. Now I
could probably roll my own interface that hid the mess, but my client really
wants to use my driver with C# and pretty much standard classes.

Don Burn
Windows Filesystem and Driver Consulting
Website: http://www.windrvr.com
Blog: http://msmvps.com/blogs/WinDrvr

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Jan Bottorff
Sent: Monday, December 09, 2013 12:49 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] Overlapped I/O and C#

C# can call any native C API via the PInvoke facility, so if you really want
you can do overlapped I/O the same way you would do it from C.

I’d have to go look, but there almost certainly are already C# classes to do
async I/O, which would be easier than using the PInvoke facility. I know
there are some C# samples that do async I/O on codeproject.com.

A quick Google search finds the C# class System.Threading.Overlapped, which
is a managed wrapper for the overlapped structure used from C. There must
also be classes that use System.Threading.Overlapped.

Jan

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Don Burn
Sent: Monday, December 9, 2013 9:32 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] Overlapped I/O and C#

If there are any C# folks out there can you tell me how do true overlapped
I/O to a driver with C#? I have a driver for a client where it is best if
they have a pool of N requests and keep firing them ASAP. In regular C I
would be using a either a pool of overlapped structures or even an I/O
completion port model. While I have done a little C# asynchronous I/O is
not somewhere I have wandered and with wonderful comments such as “Don’t
initiate a second write before the first completes” that appears in the
BeginWrite call which is implied to be async, I am wondering if this is
doable?

Don Burn
Windows Filesystem and Driver Consulting
Website: http://www.windrvr.com
Blog: http://msmvps.com/blogs/WinDrvr


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

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


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

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


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

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

> completion port model. While I have done a little C# asynchronous I/O is

not somewhere I have wandered and with wonderful comments such as “Don’t
initiate a second write before the first completes” that appears in the
BeginWrite call which is implied to be async

Are you using async/await stuff from .NET 4?

If yes - then this is NOT overlapped IO, and NOT threads.

This is state-saving co-routines using 1 thread, and the purpose is to allow any code to return to the UI message loop ASAP, so that the lame UI developers will not freeze the UI trying to resolve the DNS name or connect to the server.


Maxim S. Shatskih
Microsoft MVP on File System And Storage
xxxxx@storagecraft.com
http://www.storagecraft.com