/// <summary>
        /// Handles notifications received from the driver.
        /// </summary>
        /// <param name="driverNotification">Driver notification.</param>
        /// <returns>Reply to be sent back to the driver.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="driverNotification"/> is <see langword="null"/>.</exception>
        /// <exception cref="InvalidOperationException">No handler is found for the <paramref name="driverNotification"/> given.</exception>
        protected override object NotificationsHandler(IDriverNotification driverNotification)
        {
            if (driverNotification == null)
            {
                throw new ArgumentNullException(nameof(driverNotification));
            }

            switch (driverNotification.Type)
            {
            case (int)DriverNotificationType.OpenFileInUserMode:

                Func <OpenFileInUserModeNotification, OpenFileInUserModeNotificationReply> openHandler = this.OpenFileInUserModeHandler;
                if (openHandler != null)
                {
                    string sourceFile = Marshal.PtrToStringUni(driverNotification.Data);
                    string targetFile = Marshal.PtrToStringUni(IntPtr.Add(driverNotification.Data, Marshal.SystemDefaultCharSize * (sourceFile.Length + 1)));

                    OpenFileInUserModeNotification notification = new OpenFileInUserModeNotification
                    {
                        SourceFile = sourceFile,
                        TargetFile = targetFile,
                    };

                    return(openHandler(notification));
                }

                break;

            case (int)DriverNotificationType.CloseFileHandle:

                Action <CloseFileHandleNotification> closeHandler = this.CloseFileHandleHandler;
                if (closeHandler != null)
                {
                    CloseFileHandleNotification notification = new CloseFileHandleNotification {
                        Handle = Marshal.ReadIntPtr(driverNotification.Data)
                    };
                    closeHandler(notification);

                    return(null);
                }

                break;

            case (int)DriverNotificationType.FetchFileInUserMode:

                Func <FetchFileInUserModeNotification, FetchFileInUserModeNotificationReply> fetchHandler = this.FetchFileInUserModeHandler;
                if (fetchHandler != null)
                {
                    string sourceFile = Marshal.PtrToStringUni(driverNotification.Data);
                    string targetFile = Marshal.PtrToStringUni(IntPtr.Add(driverNotification.Data, Marshal.SystemDefaultCharSize * (sourceFile.Length + 1)));

                    FetchFileInUserModeNotification notification = new FetchFileInUserModeNotification
                    {
                        SourceFile = sourceFile,
                        TargetFile = targetFile,
                    };

                    return(fetchHandler(notification));
                }

                break;
            }

            throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "No handler found for the notification of type {0}", driverNotification.Type));
        }
Esempio n. 2
0
 /// <summary>
 /// Closes the file handle given.
 /// </summary>
 /// <param name="notification">Driver notification.</param>
 private void CloseFileHandleHandler(CloseFileHandleNotification notification)
 {
     if (notification.Handle != IntPtr.Zero)
     {
         Native.NativeMethods.CloseHandle(notification.Handle);
     }
 }
Esempio n. 3
0
        /// <summary>
        /// Handles notifications received from the driver.
        /// </summary>
        /// <param name="driverNotification">Driver notification.</param>
        /// <returns>Reply to be sent back to the driver.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="driverNotification"/> is <see langword="null"/>.</exception>
        /// <exception cref="InvalidOperationException">No handler is found for the <paramref name="driverNotification"/> given.</exception>
        protected override object NotificationsHandler(IDriverNotification driverNotification)
        {
            if (driverNotification == null)
            {
                throw new ArgumentNullException(nameof(driverNotification));
            }

            switch (driverNotification.Type)
            {
                case (int)DriverNotificationType.OpenFileInUserMode:

                    Func<OpenFileInUserModeNotification, OpenFileInUserModeNotificationReply> openHandler = this.OpenFileInUserModeHandler;
                    if (openHandler != null)
                    {
                        string sourceFile = Marshal.PtrToStringUni(driverNotification.Data);
                        string targetFile = Marshal.PtrToStringUni(IntPtr.Add(driverNotification.Data, Marshal.SystemDefaultCharSize * (sourceFile.Length + 1)));

                        OpenFileInUserModeNotification notification = new OpenFileInUserModeNotification
                        {
                            SourceFile = sourceFile,
                            TargetFile = targetFile,
                        };

                        return openHandler(notification);
                    }

                    break;

                case (int)DriverNotificationType.CloseFileHandle:

                    Action<CloseFileHandleNotification> closeHandler = this.CloseFileHandleHandler;
                    if (closeHandler != null)
                    {
                        CloseFileHandleNotification notification = new CloseFileHandleNotification { Handle = Marshal.ReadIntPtr(driverNotification.Data) };
                        closeHandler(notification);

                        return null;
                    }

                    break;

                case (int)DriverNotificationType.FetchFileInUserMode:

                    Func<FetchFileInUserModeNotification, FetchFileInUserModeNotificationReply> fetchHandler = this.FetchFileInUserModeHandler;
                    if (fetchHandler != null)
                    {
                        string sourceFile = Marshal.PtrToStringUni(driverNotification.Data);
                        string targetFile = Marshal.PtrToStringUni(IntPtr.Add(driverNotification.Data, Marshal.SystemDefaultCharSize * (sourceFile.Length + 1)));

                        FetchFileInUserModeNotification notification = new FetchFileInUserModeNotification
                        {
                            SourceFile = sourceFile,
                            TargetFile = targetFile,
                        };

                        return fetchHandler(notification);
                    }

                    break;
            }

            throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "No handler found for the notification of type {0}", driverNotification.Type));
        }