/// <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));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Opens the file given.
        /// </summary>
        /// <param name="notification">Driver notification.</param>
        /// <returns>Structure containing the reply data.</returns>
        private OpenFileInUserModeNotificationReply OpenFileInUserModeHandler(OpenFileInUserModeNotification notification)
        {
            this.ImpersonateCurrentThread();

            //
            // NOTE: You may want to open a different source file depending on where the local file is located.
            // string targetFile = notification.TargetFile;
            //

            string sourceFile = PathHelper.ChangeDeviceNameToDriveLetter(notification.SourceFile);

            IntPtr handle = Native.NativeMethods.CreateFile(sourceFile, FileAccess.Read, FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, FileAttributes.Normal, IntPtr.Zero);
            if (handle == IntPtr.Zero)
            {
                Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
            }

            return new OpenFileInUserModeNotificationReply { Handle = handle };
        }
Exemplo 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));
        }