Re: IO completion port with callbacks



Good morning Nick. Nice to see you again in this newsgroup.

For the question: what to be passed to BindIoCompletionCallback() in .NET
P/Invoke, and whether the delegate object needs to be pinned.

Yes, we need to pass a delegate object to the second parameter of
BindIoCompletionCallback. The following signature is generated by PInvoke
Interop Assistant
http://www.codeplex.com/clrinterop/
Apart from this, we need to Allocate a GCHandle (note: "pin" is not
necessary, we just need to call GCHandle.Alloc with GCHandleType.Normal on
the object) according to Zhang Yi's article:

Marshaling between Managed and Unmanaged Code
http://msdn.microsoft.com/en-us/magazine/cc164193.aspx
(see the section Reverse P/Invoke and Delegate Lifetime)

====================================
/// Return Type: void
///dwErrorCode: DWORD->unsigned int
///dwNumberOfBytesTransfered: DWORD->unsigned int
///lpOverlapped: LPOVERLAPPED->_OVERLAPPED*
public delegate void LPOVERLAPPED_COMPLETION_ROUTINE(uint dwErrorCode, uint
dwNumberOfBytesTransfered, ref OVERLAPPED lpOverlapped);

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct OVERLAPPED {

/// ULONG_PTR->unsigned int
public uint Internal;

/// ULONG_PTR->unsigned int
public uint InternalHigh;

/// Anonymous_7416d31a_1ce9_4e50_b1e1_0f2ad25c0196
public Anonymous_7416d31a_1ce9_4e50_b1e1_0f2ad25c0196 Union1;

/// HANDLE->void*
public System.IntPtr hEvent;
}

.....// omit some definition here.

public partial class NativeMethods {

/// Return Type: BOOL->int
///FileHandle: HANDLE->void*
///Function: LPOVERLAPPED_COMPLETION_ROUTINE
///Flags: ULONG->unsigned int
[System.Runtime.InteropServices.DllImportAttribute("kernel32.dll",
EntryPoint="BindIoCompletionCallback")]
[return:
System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool
BindIoCompletionCallback([System.Runtime.InteropServices.InAttribute()]
System.IntPtr FileHandle, LPOVERLAPPED_COMPLETION_ROUTINE Function, uint
Flags) ;

}
====================================

However, Nick, .NET base class library actually provides a method equivalent
to the native API BindIoCompletionCallback:
System.Threading.ThreadPool.BindHandle
(see: http://msdn.microsoft.com/en-us/library/aa302340.aspx)

IO completion ports are available for .NET applications to use. We can do
this through the three functions in the System.Threading.Thread namespace:

1) Overlapped.Pack
http://msdn.microsoft.com/en-us/library/system.threading.overlapped.pack.aspx
2) Overlapped.Unpack
http://msdn.microsoft.com/en-us/library/system.threading.overlapped.unpack.aspx
3) ThreadPool.BindHandle
http://msdn.microsoft.com/en-us/library/system.threading.threadpool.bindhandle.aspx

I find a good blog entry for the use of IO completion ports in .NET written
by a community member:
http://www.beefycode.com/post/Using-Overlapped-IO-from-Managed-Code.aspx
It basically demonstrates the calls of Overlapped.Pack, Overlapped.Unpack
and ThreadPool.BindHandle.

Have a very nice day!

Best Regards,
Jialiang Ge (jialge@xxxxxxxxxxxxxxxxxxxx, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxxxxxxxxxx

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

MSDN Managed Newsgroup support offering is for non-urgent issues where an
initial response from the community or a Microsoft Support Engineer within 2
business day is acceptable. Please note that each follow up response may
take approximately 2 business days as the support professional working with
you may need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations that require urgent,
real-time or phone-based interactions. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.



"nickdu" <nicknospamdu@xxxxxxxxxxxxxxxx> wrote in message
news:AC7DF456-A759-4CB1-9F09-B2D2DE865CB5@xxxxxxxxxxxxxxxx
I'm wondering how to work with IO completion ports and callbacks in .NET.
I'm writing a named pipe server which needs to work on .NET 1.1, and 2.0.
I've got it working now with Win32 events but would like to use completion
ports and callbacks.

I can create the interop call to BindIoCompletionCallback(), but what is
the
.NET object I pass for the callback function? Is it simply a delegate?
Will
having a managed callback active all the time cause GC problems? Just
wondering whether some memory will be pinned and therefore have adverse
effects.

How do I wait on a shutdown event and be in an alertable wait state?
--
Thanks,
Nick

nicknospamdu@xxxxxxxxxxxxxxxx
remove "nospam" change community. to msn.com


.



Relevant Pages

  • RE: Scheduling a Resource between OL2K3 and OWA
    ... venue of support for time critical or business down issues. ... * In conjunction with Offline Booking, enable the Resource for Delegation. ... The delegate account can also be an unattended mailbox. ... * Set the Resource to send Meeting Requests to the Delegate. ...
    (microsoft.public.exchange.admin)
  • RE: Mailbox rights Excgange 2000 and Outlook
    ... You then can find the delegate user account in "Send on behalf" area. ... Microsoft Online Partner Support ... Business-Critical Phone Support (BCPS) provides you with technical phone ... If you are outside the United States, ...
    (microsoft.public.exchange.admin)
  • Re: please validate: cannot have a delegate type nested inside sealed abstract ref class
    ... A delegate isn't an instance member, it is a nested type. ... Microsoft Online Community Support ...
    (microsoft.public.dotnet.languages.vc)
  • Re: ObservableCollection(of T): Only for UI-tier or for all tiers?
    ... WinForms control to invoke the delegate. ... call the Invoke or BeginInvoke method of the Dispather linked to the UI ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.languages.csharp)

Loading