/// <summary> /// Create a new point-to-point message queue object. /// </summary> /// <param name="name"> /// Indicates the name of a named queue. Set to null to create /// an unnamed queue. /// </param> /// <param name="maxMessages"> /// Indicates the length of the queue in messages. /// </param> /// <param name="maxMessageSize"> /// Indicates the maximum size of each message in the queue. /// </param> /// <param name="readAccess"> /// Set to true if messages will be read from the queue, false /// if messages will be written to the queue. A read/write /// queue cannot be created. /// </param> /// <param name="flags"> /// Flags indicating how the queue will operate. /// </param> public PointToPointMsgQueue( string name, int maxMessages, int maxMessageSize, bool readAccess, PointToPointMsgQueueFlags flags ) { hMsgQueue = IntPtr.Zero; created = false; // Set up the descriptor for the queue, based on the // parameters. MSGQUEUEOPTIONS opt = new MSGQUEUEOPTIONS(); opt.dwFlags = (int)flags; opt.dwMaxMessages = maxMessages; opt.cbMaxMessage = maxMessageSize; opt.bReadAccess = ( readAccess ? 1 : 0 ); // Actually create the queue. hMsgQueue = PointToPointMsgQueuePInvokes.CreateMsgQueue( name, opt ); if ( hMsgQueue != IntPtr.Zero ) { // Get indication of whether we created the queue or // if we just attached to an existing queue. uint err = PointToPointMsgQueuePInvokes.GetLastError(); if ( err == 0 ) { created = true; } else { created = false; } } }
/// <summary> /// Create a new point-to-point message queue object. /// </summary> /// <param name="name"> /// Indicates the name of a named queue. Set to null to create /// an unnamed queue. /// </param> /// <param name="maxMessages"> /// Indicates the length of the queue in messages. /// </param> /// <param name="maxMessageSize"> /// Indicates the maximum size of each message in the queue. /// </param> /// <param name="readAccess"> /// Set to true if messages will be read from the queue, false /// if messages will be written to the queue. A read/write /// queue cannot be created. /// </param> /// <param name="flags"> /// Flags indicating how the queue will operate. /// </param> public PointToPointMsgQueue(string name, int maxMessages, int maxMessageSize, bool readAccess, PointToPointMsgQueueFlags flags) { hMsgQueue = IntPtr.Zero; created = false; // Set up the descriptor for the queue, based on the // parameters. MSGQUEUEOPTIONS opt = new MSGQUEUEOPTIONS(); opt.dwFlags = (int)flags; opt.dwMaxMessages = maxMessages; opt.cbMaxMessage = maxMessageSize; opt.bReadAccess = (readAccess ? 1 : 0); // Actually create the queue. hMsgQueue = PointToPointMsgQueuePInvokes.CreateMsgQueue(name, opt); if (hMsgQueue != IntPtr.Zero) { // Get indication of whether we created the queue or // if we just attached to an existing queue. uint err = PointToPointMsgQueuePInvokes.GetLastError(); if (err == 0) { created = true; } else { created = false; } } }
/// <summary> /// Write a new item to the message queue. /// </summary> /// <param name="buffer"> /// Caller-allocated byte array for the data. /// </param> /// <param name="bufferSize"> /// Number of bytes in the caller-allocated buffer. /// </param> /// <param name="timeout"> /// Time-out value in ms for the read. If no messages arrive /// before the time-out occurs, the number of bytes read will /// be zero. /// </param> /// <param name="writeFlags"> /// Can be set to AlertMsg, if an 'alert message' is to be posted /// to the queue. Otherwise, set to zero. /// </param> /// <returns> /// True is returned on a successful write; false otherwise. /// </returns> public bool WriteMsgQueue(byte[] buffer, int bufferSize, int timeout, PointToPointMsgQueueFlags writeFlags) { bool result = PointToPointMsgQueuePInvokes.WriteMsgQueue( hMsgQueue, buffer, bufferSize, timeout, (int)writeFlags); // Check the result and throw exceptions on errors. // ???? return(result); }
internal void StatusThread() { // Ask the system to notify our message queue when devices of // the indicated class are added or removed. IntPtr h = RequestDeviceNotifications( devClass.ToGuid().ToByteArray(), mq.Handle, fAll); // Wait for the queue to be signaled. When it is, decode // the message and call any listeners for it. If the // queue closes suddenly, that's our cue to exit, shutting // down notifications before we leave. PointToPointMsgQueue q = mq; while (mq != null) // Check the global object here. { // Wait. On return, true indicates that the queue is // signaled; false is a chance to check whether we should // exit. if (q.Wait(-1)) { // Read the event data. int bytes = 0; PointToPointMsgQueueFlags readFlags = 0; DEVDETAIL devDetail = new DEVDETAIL(); if (q.ReadMsgQueue(devDetail.getBytes(), DEVDETAIL.MAX_DEVDETAIL_SIZE, ref bytes, -1, ref readFlags)) { // Handle the event. OnDeviceNotification(new DeviceNotificationArgs( devDetail.guidDevClass, devDetail.fAttached, devDetail.szName)); } } else { System.Diagnostics.Debug.WriteLine(this, "mq is null in monitoring thread. Exiting..."); } } System.Diagnostics.Debug.WriteLine(this, "exiting DeviceStatusMonitor thread."); // Stop notifications to us. StopDeviceNotifications(h); }
/// <summary> /// Read the next item from the message queue. /// </summary> /// <param name="buffer"> /// Caller-allocated byte array for the data. /// </param> /// <param name="bufferSize"> /// Number of bytes in the caller-allocated buffer. /// </param> /// <param name="bytesRead"> /// Return value indicating number of bytes actually read and /// stored in the buffer. /// </param> /// <param name="timeout"> /// Time-out value in ms for the read. If no messages arrive /// before the time-out occurs, the number of bytes read will /// be zero. /// </param> /// <param name="readFlags"> /// Will be set to AlertMsg if the message was written to the /// queue with that flag set. Zero, otherwise. /// </param> /// <returns> /// True is returned on a successful read; false otherwise. /// </returns> public bool ReadMsgQueue(byte[] buffer, int bufferSize, ref int bytesRead, int timeout, ref PointToPointMsgQueueFlags readFlags) { int fl = 0; bool result = PointToPointMsgQueuePInvokes.ReadMsgQueue( hMsgQueue, buffer, bufferSize, ref bytesRead, timeout, ref fl); readFlags = (PointToPointMsgQueueFlags)fl; // Check the result and throw exceptions on errors. // ???? return(result); }
/// <summary> /// Write a new item to the message queue. /// </summary> /// <param name="buffer"> /// Caller-allocated byte array for the data. /// </param> /// <param name="bufferSize"> /// Number of bytes in the caller-allocated buffer. /// </param> /// <param name="timeout"> /// Time-out value in ms for the read. If no messages arrive /// before the time-out occurs, the number of bytes read will /// be zero. /// </param> /// <param name="writeFlags"> /// Can be set to AlertMsg, if an 'alert message' is to be posted /// to the queue. Otherwise, set to zero. /// </param> /// <returns> /// True is returned on a successful write; false otherwise. /// </returns> public bool WriteMsgQueue( byte[] buffer, int bufferSize, int timeout, PointToPointMsgQueueFlags writeFlags ) { bool result = PointToPointMsgQueuePInvokes.WriteMsgQueue( hMsgQueue, buffer, bufferSize, timeout, (int)writeFlags ); // Check the result and throw exceptions on errors. // ???? return result; }
/// <summary> /// Read the next item from the message queue. /// </summary> /// <param name="buffer"> /// Caller-allocated byte array for the data. /// </param> /// <param name="bufferSize"> /// Number of bytes in the caller-allocated buffer. /// </param> /// <param name="bytesRead"> /// Return value indicating number of bytes actually read and /// stored in the buffer. /// </param> /// <param name="timeout"> /// Time-out value in ms for the read. If no messages arrive /// before the time-out occurs, the number of bytes read will /// be zero. /// </param> /// <param name="readFlags"> /// Will be set to AlertMsg if the message was written to the /// queue with that flag set. Zero, otherwise. /// </param> /// <returns> /// True is returned on a successful read; false otherwise. /// </returns> public bool ReadMsgQueue( byte[] buffer, int bufferSize, ref int bytesRead, int timeout, ref PointToPointMsgQueueFlags readFlags ) { int fl = 0; bool result = PointToPointMsgQueuePInvokes.ReadMsgQueue( hMsgQueue, buffer, bufferSize, ref bytesRead, timeout, ref fl ); readFlags = (PointToPointMsgQueueFlags)fl; // Check the result and throw exceptions on errors. // ???? return result; }
internal void StatusThread() { // Ask NDISUIO to send us notification messages for our // adapter. IntPtr ndisAccess = FileEx.CreateFile( NDISUIOPInvokes.NDISUIO_DEVICE_NAME, FileAccess.All, FileShare.None, FileCreateDisposition.OpenExisting, NDISUIOPInvokes.FILE_ATTRIBUTE_NORMAL | NDISUIOPInvokes.FILE_FLAG_OVERLAPPED); if ((int)ndisAccess == FileEx.InvalidHandle) { return; } NDISUIO_REQUEST_NOTIFICATION ndisRequestNotification = new NDISUIO_REQUEST_NOTIFICATION(); ndisRequestNotification.hMsgQueue = mq.Handle; ndisRequestNotification.dwNotificationTypes = NDISUIOPInvokes.NDISUIO_NOTIFICATION_MEDIA_SPECIFIC_NOTIFICATION | NDISUIOPInvokes.NDISUIO_NOTIFICATION_MEDIA_CONNECT | NDISUIOPInvokes.NDISUIO_NOTIFICATION_MEDIA_DISCONNECT | NDISUIOPInvokes.NDISUIO_NOTIFICATION_BIND | NDISUIOPInvokes.NDISUIO_NOTIFICATION_UNBIND; UInt32 xcount = 0; if (!NDISUIOPInvokes.DeviceIoControl(ndisAccess, NDISUIOPInvokes.IOCTL_NDISUIO_REQUEST_NOTIFICATION, ndisRequestNotification.getBytes(), NDISUIO_REQUEST_NOTIFICATION.Size, null, 0, ref xcount, IntPtr.Zero)) { System.Diagnostics.Debug.WriteLine(this, "Error in DeviceIoControl to request notifications!"); } // Each notification will be of this type. NDISUIO_DEVICE_NOTIFICATION ndisDeviceNotification = new NDISUIO_DEVICE_NOTIFICATION(); // Wait for the queue to be signaled. When it is, decode // the message and call any listeners for it. If the // queue closes suddenly, that's our cue to exit, shutting // down NDISUIO notifications before we leave. PointToPointMsgQueue q = mq; while (mq != null) // Check the global object here. { // Wait. On return, true indicates that the queue is // signaled; false is a chance to check whether we should // exit. if (q.Wait(-1)) { // Read the event data. int bytes = 0; PointToPointMsgQueueFlags readFlags = 0; if (q.ReadMsgQueue(ndisDeviceNotification.getBytes(), NDISUIO_DEVICE_NOTIFICATION.Size, ref bytes, -1, ref readFlags)) { // Handle the event. OnAdapterNotification(new AdapterNotificationArgs( ndisDeviceNotification.ptcDeviceName, (NdisNotificationType)ndisDeviceNotification.dwNotificationType)); } } else { System.Diagnostics.Debug.WriteLine(this, "mq is null in monitoring thread. Exiting..."); } } System.Diagnostics.Debug.WriteLine(this, "exiting AdapterStatusMonitor thread."); if (!NDISUIOPInvokes.DeviceIoControl(ndisAccess, NDISUIOPInvokes.IOCTL_NDISUIO_CANCEL_NOTIFICATION, null, 0, null, 0, ref xcount, IntPtr.Zero)) { System.Diagnostics.Debug.WriteLine(this, "Error in DeviceIoControl to stop notifications!"); // ???? } // Don't forget to close our handle to NDISUIO. FileEx.CloseHandle(ndisAccess); }