Overlapped.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // System.Threading.Overlapped.cs
  3. //
  4. // Authors:
  5. // Dick Porter ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected]);
  7. //
  8. // (C) Ximian, Inc. http://www.ximian.com
  9. // (C) 2004 Novell, Inc. (http://www.novell.com)
  10. //
  11. //
  12. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System.Runtime.InteropServices;
  34. using System.Security.Permissions;
  35. namespace System.Threading
  36. {
  37. #if NET_2_0
  38. [ComVisible (true)]
  39. #endif
  40. public class Overlapped
  41. {
  42. IAsyncResult ares;
  43. int offsetL;
  44. int offsetH;
  45. int evt;
  46. #if NET_2_0
  47. IntPtr evt_ptr;
  48. #endif
  49. public Overlapped ()
  50. {
  51. }
  52. #if NET_2_0
  53. [Obsolete ("Not 64bit compatible. Please use the constructor that takes IntPtr for the event handle", false)]
  54. #endif
  55. public Overlapped(int offsetLo, int offsetHi, int hEvent, IAsyncResult ar)
  56. {
  57. offsetL = offsetLo;
  58. offsetH = offsetHi;
  59. evt = hEvent;
  60. ares = ar;
  61. }
  62. #if NET_2_0
  63. public Overlapped (int offsetLo, int offsetHi, IntPtr hEvent,
  64. IAsyncResult ar)
  65. {
  66. offsetL = offsetLo;
  67. offsetH = offsetHi;
  68. evt_ptr = hEvent;
  69. ares = ar;
  70. }
  71. #endif
  72. [CLSCompliant(false)]
  73. unsafe public static void Free (NativeOverlapped *nativeOverlappedPtr)
  74. {
  75. if ((IntPtr) nativeOverlappedPtr == IntPtr.Zero)
  76. throw new ArgumentNullException ("nativeOverlappedPtr");
  77. Marshal.FreeHGlobal ((IntPtr) nativeOverlappedPtr);
  78. }
  79. [CLSCompliant(false)]
  80. unsafe public static Overlapped Unpack (NativeOverlapped *nativeOverlappedPtr)
  81. {
  82. if ((IntPtr) nativeOverlappedPtr == IntPtr.Zero)
  83. throw new ArgumentNullException ("nativeOverlappedPtr");
  84. Overlapped result = new Overlapped ();
  85. result.offsetL = nativeOverlappedPtr->OffsetLow;
  86. result.offsetH = nativeOverlappedPtr->OffsetHigh;
  87. #if NET_2_0
  88. result.evt = (int)nativeOverlappedPtr->EventHandle;
  89. #else
  90. result.evt = nativeOverlappedPtr->EventHandle;
  91. #endif
  92. return result;
  93. }
  94. [CLSCompliant(false)]
  95. #if NET_2_0
  96. [Obsolete ("Use Pack(iocb, userData) instead", false)]
  97. #endif
  98. [MonoTODO ("Security - we need to propagate the call stack")]
  99. unsafe public NativeOverlapped *Pack (IOCompletionCallback iocb)
  100. {
  101. NativeOverlapped *result = (NativeOverlapped *) Marshal.AllocHGlobal (Marshal.SizeOf (typeof (NativeOverlapped)));
  102. result->OffsetLow = offsetL;
  103. result->OffsetHigh = offsetH;
  104. #if NET_2_0
  105. result->EventHandle = (IntPtr)evt;
  106. #else
  107. result->EventHandle = evt;
  108. #endif
  109. return result;
  110. }
  111. #if NET_2_0
  112. [CLSCompliant (false)]
  113. [ComVisible (false)]
  114. [MonoTODO ("handle userData")]
  115. unsafe public NativeOverlapped *Pack (IOCompletionCallback iocb, object userData)
  116. {
  117. NativeOverlapped *result = (NativeOverlapped *) Marshal.AllocHGlobal (Marshal.SizeOf(typeof(NativeOverlapped)));
  118. result->OffsetLow = offsetL;
  119. result->OffsetHigh = offsetH;
  120. result->EventHandle = evt_ptr;
  121. return(result);
  122. }
  123. #endif
  124. [CLSCompliant(false)]
  125. #if NET_2_0
  126. [Obsolete ("Use UnsafePack(iocb, userData) instead", false)]
  127. #endif
  128. [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
  129. unsafe public NativeOverlapped *UnsafePack (IOCompletionCallback iocb)
  130. {
  131. // no need to propagate the call stack in the unsafe version
  132. return Pack (iocb);
  133. }
  134. #if NET_2_0
  135. [ComVisible (false)]
  136. [CLSCompliant (false)]
  137. unsafe public NativeOverlapped *UnsafePack (IOCompletionCallback iocb, object userData)
  138. {
  139. return Pack (iocb, userData);
  140. }
  141. #endif
  142. public IAsyncResult AsyncResult {
  143. get { return ares; }
  144. set { ares = value; }
  145. }
  146. #if NET_2_0
  147. [Obsolete ("Not 64bit compatible. Use EventHandleIntPtr instead.", false)]
  148. #endif
  149. public int EventHandle {
  150. get { return evt; }
  151. set { evt = value; }
  152. }
  153. #if NET_2_0
  154. [ComVisible (false)]
  155. public IntPtr EventHandleIntPtr
  156. {
  157. get{
  158. return(evt_ptr);
  159. }
  160. set{
  161. evt_ptr = value;
  162. }
  163. }
  164. #endif
  165. public int OffsetHigh {
  166. get { return offsetH; }
  167. set { offsetH = value; }
  168. }
  169. public int OffsetLow {
  170. get { return offsetL; }
  171. set { offsetL = value; }
  172. }
  173. }
  174. }