private void ProcessNotification(ResizableBuffer resizableBuffer) { IntPtr bufferPointer = resizableBuffer.DangerousGetPointer(); // Marshal the notification received. DriverNotificationHeader header = (DriverNotificationHeader)Marshal.PtrToStructure(bufferPointer, typeof(DriverNotificationHeader)); DriverNotification notification = new DriverNotification { Type = header.Type, DataLength = header.DataLength, Data = bufferPointer + this.notificationHeaderSize }; // Get the reply object from the user-defined handler. object reply = null; int handlerResult = (int)NativeMethods.Ok; try { reply = this.handler(notification); } catch (Exception e) { handlerResult = Marshal.GetHRForException(e); NotificationsMonitor.Logger.Error(e, "Notification handler threw an exception."); } // Driver is not expecting any reply. if (header.ReplyLength == 0) { if (reply != null) { NotificationsMonitor.Logger.Warn(CultureInfo.InvariantCulture, "Driver is not expecting any reply, but reply object is returned from handler: {0}", reply.GetType()); } return; } int replySize = this.replyHeaderSize + MarshalingHelper.GetObjectSize(reply); if (replySize > header.ReplyLength) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Reply ({0} bytes) is bigger than the one expected by the driver ({1} bytes).", replySize, header.ReplyLength)); } DriverReplyHeader replyHeader = new DriverReplyHeader { MessageId = header.MessageId, // Notify driver about the exception thrown, if any. Status = handlerResult }; // Adjust the buffer to fit the reply. resizableBuffer.Resize(replySize); bufferPointer = resizableBuffer.DangerousGetPointer(); // Marshal reply to the output buffer. MarshalingHelper.MarshalObjectsToPointer(bufferPointer, replySize, replyHeader, reply); // And send it to the driver. uint hr = NativeMethods.FilterReplyMessage(this.filterPortHandle, bufferPointer, (uint)replySize); if (hr != NativeMethods.Ok) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Unable to send reply: 0x{0:X8}", hr), Marshal.GetExceptionForHR(unchecked((int)hr))); } }
private void ProcessNotification(ResizableBuffer resizableBuffer) { IntPtr bufferPointer = resizableBuffer.DangerousGetPointer(); // Marshal the notification received. DriverNotificationHeader header = (DriverNotificationHeader)Marshal.PtrToStructure(bufferPointer, typeof(DriverNotificationHeader)); DriverNotification notification = new DriverNotification { Type = header.Type, DataLength = header.DataLength, Data = bufferPointer + this.notificationHeaderSize }; // Get the reply object from the user-defined handler. object reply = null; int handlerResult = (int)NativeMethods.Ok; try { reply = this.handler(notification); } catch (Exception e) { handlerResult = Marshal.GetHRForException(e); NotificationsMonitor.Logger.Error(e, "Notification handler threw an exception."); } // Driver is not expecting any reply. if (header.ReplyLength == 0) { if (reply != null) { NotificationsMonitor.Logger.Warn(CultureInfo.InvariantCulture, "Driver is not expecting any reply, but reply object is returned from handler: {0}", reply.GetType()); } return; } int replySize = this.replyHeaderSize + MarshalingHelper.GetObjectSize(reply); if (replySize > header.ReplyLength) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Reply ({0} bytes) is bigger than the one expected by the driver ({1} bytes).", replySize, header.ReplyLength)); } DriverReplyHeader replyHeader = new DriverReplyHeader { MessageId = header.MessageId, // Notify driver about the exception thrown, if any. Status = handlerResult }; // Adjust the buffer to fit the reply. resizableBuffer.Resize(replySize); bufferPointer = resizableBuffer.DangerousGetPointer(); // Marshal reply to the output buffer. MarshalingHelper.MarshalObjectsToPointer(bufferPointer, replySize, replyHeader, reply); // And send it to the driver. uint hr = NativeMethods.FilterReplyMessage(this.filterPortHandle, bufferPointer, (uint)replySize); if (hr != NativeMethods.Ok) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Unable to send reply: 0x{0:X8}", hr), Marshal.GetExceptionForHR(unchecked ((int)hr))); } }