Ejemplo n.º 1
0
 public static extern IntPtr CreateFile(string lpFileName,
                                        uint dwDesiredAccess,
                                        FileShareMode dwShareMode,
                                        ref SecurityAttributes lpSecurityAttributes,
                                        FileCreationDisposition dwCreationDisposition,
                                        uint dwFlagsAndAttributes,
                                        IntPtr hTemplateFile);
Ejemplo n.º 2
0
 public static extern IntPtr CreateFile(
     string fileName,
     FileAccessRightFlags desiredAccess,
     FileShareModeFlags shareMode,
     IntPtr securityAttributes,
     FileCreationDisposition creationDisposition,
     FileFlagsAndAttributes flagsAndAttributes,
     IntPtr templateFile);
Ejemplo n.º 3
0
 public static extern SafeFileHandle CreateFile(
     [In, MarshalAs(UnmanagedType.LPWStr)] string fileName,
     [In, MarshalAs(UnmanagedType.U4)] FileAccess desiredAccess,
     [In, MarshalAs(UnmanagedType.U4)] FileShare shareMode,
     [In, MarshalAs(UnmanagedType.SysInt)] IntPtr securityAttributes,
     [In, MarshalAs(UnmanagedType.U4)] FileCreationDisposition creationDisposition,
     [In, MarshalAs(UnmanagedType.U4)] FileAttributes flagsAndAttributes,
     [In, MarshalAs(UnmanagedType.SysInt)] IntPtr templateFile);
Ejemplo n.º 4
0
 public static extern SafeFileHandle CreateFile(
     [In] string lpFileName,
     [In] AccessRights dwDesiredAccess,
     [In] FileShareMode dwShareMode,
     [In] IntPtr lpSecurityAttributes,
     [In] FileCreationDisposition dwCreationDisposition,
     [In] FileAttributeFlags dwFlagsAndAttributes,
     [In] IntPtr hTemplateFile);
Ejemplo n.º 5
0
 public static extern IntPtr CreateFile(
     string fileName,
     FileAccessRightFlags desiredAccess,
     FileShareModeFlags shareMode,
     IntPtr securityAttributes,
     FileCreationDisposition creationDisposition,
     FileFlagsAndAttributes flagsAndAttributes,
     IntPtr templateFile);
Ejemplo n.º 6
0
 public static extern IntPtr CreateFileW(
     string lpFileName,
     FileAccessMode dwDesiredAccess,
     FileShareMode dwShareMode,
     IntPtr lpSecurityAttributes,                 // always 0
     FileCreationDisposition dwCreationDisposition,
     FileFlagsAndAttributes dwFlagsAndAttributes, // typically just FILE_FLAG_OVERLAPPED
     IntPtr hTemplateFile);
Ejemplo n.º 7
0
 public static extern IntPtr CreateFile(
     String lpFileName,                  // File name 文件名
     FileDesiredAccess dwDesiredAccess,  // Access mode 通道模式
     FileShareMode dwShareMode,          // Share mode 共享模式
     IntPtr fileSecurityAttributes,      // Security Attributes 安全属性
     FileCreationDisposition dwCreationDisposition,// How to create
     uint dwFlagsAndAttributes,          // File attributes 文件属性
     uint hTemplateFile);                // Handle to template file
Ejemplo n.º 8
0
 public static extern /*SafeFileHandle*/ IntPtr CreateFile(
     string lpFileName,
     FileAccess dwDesiredAccess,
     FileShareMode dwShareMode,
     IntPtr lpSecurityAttributes,
     FileCreationDisposition dwCreationDisposition,
     FileFlagsAndAttributes dwFlagsAndAttributes,
     IntPtr hTemplateFile);
Ejemplo n.º 9
0
 internal static extern IntPtr CreateFile(
     string fileName,
     FileDesiredAccess desiredAccess,
     FileShareMode shareMode,
     IntPtr securityAttributes,
     FileCreationDisposition creationDisposition,
     FileAttributes flagsAndAttributes,
     IntPtr templateFile);
Ejemplo n.º 10
0
 internal static extern IntPtr CreateFile(
     string lpFileName,
     FileDesiredAccess dwDesiredAccess,
     FileShareMode dwShareMode,
     IntPtr lpSecurityAttributes,
     FileCreationDisposition dwCreationDisposition,
     FileAttributes dwFlagsAndAttributes,
     IntPtr hTemplateFile);
Ejemplo n.º 11
0
 public static extern FileSafeHandle CreateFile(
     [MarshalAs(UnmanagedType.LPWStr)] string lpFileName,
     AccessRights dwDesiredAccess,
     [MarshalAs(UnmanagedType.U4)] FileShare share,
     IntPtr lpSecurityAttributes,
     FileCreationDisposition dwCreationDisposition,
     [MarshalAs(UnmanagedType.U4)] FileAttributes flagsAndAttributes,
     IntPtr hTemplateFile
     );
 internal static extern SafeFileHandle CreateFileW(
     /* _In_     LPCTSTR               */ [In] string lpFileName,
     /* _In_     DWORD                 */ [In] GenericAccessRight dwDesiredAccess,
     /* _In_     DWORD                 */ [In] FileShare dwShareMode,
     /* _In_opt_ LPSECURITY_ATTRIBUTES */ [In] IntPtr lpSecurityAttributes,
     /* _In_     DWORD                 */ [In] FileCreationDisposition dwCreationDisposition,
     /* _In_     DWORD                 */ [In] FileAttributeFlag dwFlagsAndAttributes,
     /* _In_opt_ HANDLE                */ [In] IntPtr hTemplateFile
     );
Ejemplo n.º 13
0
 public static extern SafeFileHandle CreateFile(
     string lpFileName,
     FileDesiredAccess dwDesiredAccess,
     FileShareFlags dwShareMode,
     IntPtr lpSecurityAttributes,
     FileCreationDisposition dwCreationDisposition,
     FileFlagsAndAttributes dwFlagsAndAttributes,
     IntPtr hTemplateFile
 );
Ejemplo n.º 14
0
 public static extern IntPtr CreateFile(
     string lpFileName,                             // File name
     FileDesiredAccess dwDesiredAccess,             // Access mode
     FileShareMode dwShareMode,                     // Share mode
     IntPtr SecurityAttributes,                     // Security attributes
     FileCreationDisposition dwCreationDisposition, // How to create
     uint dwFlagsAndAttributes,                     // File attributes
     IntPtr hTemplateFile                           // Handle to template file
     );
Ejemplo n.º 15
0
        public static NamedPipeHandle Create(
            FileAccess access,
            string fileName,
            ObjectFlags objectFlags,
            NativeHandle rootDirectory,
            FileShareMode shareMode,
            FileCreationDisposition creationDisposition,
            FileCreateOptions createOptions,
            PipeType type,
            PipeType readMode,
            PipeCompletionMode completionMode,
            int maximumInstances,
            int inboundQuota,
            int outboundQuota,
            long defaultTimeout
            )
        {
            NtStatus         status;
            ObjectAttributes oa = new ObjectAttributes(fileName, objectFlags, rootDirectory);
            IoStatusBlock    isb;
            IntPtr           handle;

            // If a timeout wasn't specified, use a default value.
            if (defaultTimeout == 0)
            {
                defaultTimeout = -50 * Win32.TimeMsTo100Ns; // 50 milliseconds
            }
            try
            {
                if ((status = Win32.NtCreateNamedPipeFile(
                         out handle,
                         access,
                         ref oa,
                         out isb,
                         shareMode,
                         creationDisposition,
                         createOptions,
                         type,
                         readMode,
                         completionMode,
                         maximumInstances,
                         inboundQuota,
                         outboundQuota,
                         ref defaultTimeout
                         )) >= NtStatus.Error)
                {
                    Win32.Throw(status);
                }
            }
            finally
            {
                oa.Dispose();
            }

            return(new NamedPipeHandle(handle, true));
        }
Ejemplo n.º 16
0
        public static NamedPipeHandle Create(
            FileAccess access,
            string fileName,
            ObjectFlags objectFlags,
            NativeHandle rootDirectory,
            FileShareMode shareMode,
            FileCreationDisposition creationDisposition,
            FileCreateOptions createOptions,
            PipeType type,
            PipeType readMode,
            PipeCompletionMode completionMode,
            int maximumInstances,
            int inboundQuota,
            int outboundQuota,
            long defaultTimeout
            )
        {
            NtStatus status;
            ObjectAttributes oa = new ObjectAttributes(fileName, objectFlags, rootDirectory);
            IoStatusBlock isb;
            IntPtr handle;

            // If a timeout wasn't specified, use a default value.
            if (defaultTimeout == 0)
                defaultTimeout = -50 * Win32.TimeMsTo100Ns; // 50 milliseconds

            try
            {
                if ((status = Win32.NtCreateNamedPipeFile(
                    out handle,
                    access,
                    ref oa,
                    out isb,
                    shareMode,
                    creationDisposition,
                    createOptions,
                    type,
                    readMode,
                    completionMode,
                    maximumInstances,
                    inboundQuota,
                    outboundQuota,
                    ref defaultTimeout
                    )) >= NtStatus.Error)
                    Win32.Throw(status);
            }
            finally
            {
                oa.Dispose();
            }

            return new NamedPipeHandle(handle, true);
        }
Ejemplo n.º 17
0
        public static NamedPipeHandle Create(
            FileAccess access,
            string fileName,
            ObjectFlags objectFlags,
            FileHandle rootDirectory,
            FileShareMode shareMode,
            FileCreationDisposition creationDisposition,
            FileCreateOptions createOptions,
            PipeType type,
            PipeType readMode,
            PipeCompletionMode completionMode,
            int maximumInstances,
            int inboundQuota,
            int outboundQuota,
            long defaultTimeout
            )
        {
            NtStatus         status;
            ObjectAttributes oa = new ObjectAttributes(fileName, objectFlags, rootDirectory);
            IoStatusBlock    isb;
            IntPtr           handle;

            try
            {
                if ((status = Win32.NtCreateNamedPipeFile(
                         out handle,
                         access,
                         ref oa,
                         out isb,
                         shareMode,
                         creationDisposition,
                         createOptions,
                         type,
                         readMode,
                         completionMode,
                         maximumInstances,
                         inboundQuota,
                         outboundQuota,
                         ref defaultTimeout
                         )) >= NtStatus.Error)
                {
                    Win32.ThrowLastError(status);
                }
            }
            finally
            {
                oa.Dispose();
            }

            return(new NamedPipeHandle(handle, true));
        }
Ejemplo n.º 18
0
        private bool OpenDevice()
        {
            try
            {
                FileShareMode           shareModeExclusive  = FileShareMode.FILE_SHARE_NONE;
                FileShareMode           shareModeNormal     = FileShareMode.FILE_SHARE_READ | FileShareMode.FILE_SHARE_WRITE;
                FileDesiredAccess       desiredAccess       = FileDesiredAccess.GENERIC_READ | FileDesiredAccess.GENERIC_WRITE;
                FileCreationDisposition creationDisposition = FileCreationDisposition.OPEN_EXISTING;
                FileFlagsAndAttributes  flagsAttributes     = FileFlagsAndAttributes.FILE_FLAG_NORMAL | FileFlagsAndAttributes.FILE_FLAG_OVERLAPPED | FileFlagsAndAttributes.FILE_FLAG_NO_BUFFERING;

                //Try to open the device exclusively
                FileHandle = CreateFile(DevicePath, desiredAccess, shareModeExclusive, IntPtr.Zero, creationDisposition, flagsAttributes, IntPtr.Zero);
                Exclusive  = true;

                //Try to open the device normally
                if (FileHandle == null || FileHandle.IsInvalid || FileHandle.IsClosed)
                {
                    //Debug.WriteLine("Failed to open device exclusively, opening normally.");
                    FileHandle = CreateFile(DevicePath, desiredAccess, shareModeNormal, IntPtr.Zero, creationDisposition, flagsAttributes, IntPtr.Zero);
                    Exclusive  = false;
                }

                //Check if the device is opened
                if (FileHandle == null || FileHandle.IsInvalid || FileHandle.IsClosed)
                {
                    //Debug.WriteLine("Failed to open hid device: " + DevicePath);
                    Connected = false;
                    Installed = false;
                    return(false);
                }
                else
                {
                    //Debug.WriteLine("Opened hid device: " + DevicePath + ", exclusively: " + Exclusive);
                    Connected = true;
                    Installed = true;
                    return(true);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failed to open hid device: " + ex.Message);
                Connected = false;
                Installed = false;
                return(false);
            }
        }
Ejemplo n.º 19
0
        private static SafeMailslotHandle CreateFile(string name,
                                                     IntPtr securityAttributes,
                                                     IntPtr hTemplateFile,
                                                     FileDesiredAccess access = FileDesiredAccess.GenericWrite,
                                                     FileShareMode shareMode  = FileShareMode.FileShareRead,
                                                     FileCreationDisposition creationDisposition = FileCreationDisposition.OpenExisting,
                                                     int flagsAndAttributes = 0
                                                     )
        {
            var ret = CreateFile(name, access, shareMode, securityAttributes, creationDisposition, flagsAndAttributes, hTemplateFile);

            if (ret.IsInvalid)
            {
                throw Throw("Unable to create new mailslot");
            }
            return(ret);
        }
 /// <summary>
 /// Handles intercepted file access.
 /// </summary>
 /// <param name="fileName"></param>
 /// <param name="desiredAccess"></param>
 /// <param name="shareMode"></param>
 /// <param name="securityAttributes"></param>
 /// <param name="creationDisposition"></param>
 /// <param name="flagsAndAttributes"></param>
 /// <param name="templateFile"></param>
 /// <returns></returns>
 public IntPtr CreateFile(string fileName, FileAccessRightFlags desiredAccess, FileShareModeFlags shareMode,
                            IntPtr securityAttributes, FileCreationDisposition creationDisposition,
                            FileFlagsAndAttributes flagsAndAttributes, IntPtr templateFile)
 {
   var request = new FileRequest
   {
     CreationDisposition = creationDisposition,
     Path = fileName,
     ResourceType = ResourceType.File
   };
   using (EngineCore.Engine.GetEngineProcessingSpace())
   {
     var virtualPath = _fileSystem.GetVirtualPath(request);
     return HostFileSystem.NativeMethods.CreateFile(
       virtualPath, desiredAccess, shareMode, securityAttributes,
       creationDisposition, flagsAndAttributes, templateFile);
   }
 }
Ejemplo n.º 21
0
        /// <summary>
        /// Handles intercepted file access.
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="desiredAccess"></param>
        /// <param name="shareMode"></param>
        /// <param name="securityAttributes"></param>
        /// <param name="creationDisposition"></param>
        /// <param name="flagsAndAttributes"></param>
        /// <param name="templateFile"></param>
        /// <returns></returns>
        public IntPtr CreateFile(string fileName, FileAccessRightFlags desiredAccess, FileShareModeFlags shareMode,
                                 IntPtr securityAttributes, FileCreationDisposition creationDisposition,
                                 FileFlagsAndAttributes flagsAndAttributes, IntPtr templateFile)
        {
            var request = new FileRequest
            {
                CreationDisposition = creationDisposition,
                Path         = fileName,
                ResourceType = ResourceType.File
            };

            using (EngineCore.Engine.GetEngineProcessingSpace())
            {
                var virtualPath = _fileSystem.GetVirtualPath(request);
                return(HostFileSystem.NativeMethods.CreateFile(
                           virtualPath, desiredAccess, shareMode, securityAttributes,
                           creationDisposition, flagsAndAttributes, templateFile));
            }
        }
 public static extern SafeFileHandle CreateFile(string fileName, FileAccess desiredAccess, FileShare shareMode, IntPtr securityAttributes, FileCreationDisposition creationDisposition, FileAttributes flagsAndAttributes, IntPtr templateFile);
Ejemplo n.º 23
0
 public static extern SafePipeHandle CreateFile(string fileName, FileDesiredAccess desiredAccess, FileShareMode shareMode, SECURITY_ATTRIBUTES securityAttributes, FileCreationDisposition creationDisposition, int flagsAndAttributes, IntPtr hTemplateFile);
Ejemplo n.º 24
0
 public static extern SafeMailslotHandle CreateFile(string fileName,
                                                    FileDesiredAccess desiredAccess, FileShareMode shareMode,
                                                    IntPtr securityAttributes,
                                                    FileCreationDisposition creationDisposition,
                                                    int flagsAndAttributes, IntPtr hTemplateFile);
Ejemplo n.º 25
0
 public static extern SafeMailslotHandle CreateFile(string fileName,
     FileDesiredAccess desiredAccess, FileShareMode shareMode,
     IntPtr securityAttributes,
     FileCreationDisposition creationDisposition,
     int flagsAndAttributes, IntPtr hTemplateFile);
Ejemplo n.º 26
0
        private bool OpenDevice(DriverProductIds driverProduct)
        {
            try
            {
                //Check driver product type
                if (driverProduct == DriverProductIds.TTC_PRODUCTID_KEYBOARD)
                {
                    HardwareTarget = "hid\\ttcvcontrkb";
                }
                else if (driverProduct == DriverProductIds.TTC_PRODUCTID_MOUSEREL)
                {
                    HardwareTarget = "hid\\ttcvcontrmsrel";
                }
                else if (driverProduct == DriverProductIds.TTC_PRODUCTID_MOUSEABS)
                {
                    HardwareTarget = "hid\\ttcvcontrmsabs";
                }

                //Find the virtual device path
                IEnumerable <EnumerateInfo> SelectedHidDevice = EnumerateDevicesDi(GuidClassHidDevice, true);
                foreach (EnumerateInfo EnumDevice in SelectedHidDevice)
                {
                    if (EnumDevice.HardwareId.ToLower() == HardwareTarget)
                    {
                        DevicePath = EnumDevice.DevicePath;
                        break;
                    }
                }

                FileShareMode           shareModeExclusive  = FileShareMode.FILE_SHARE_NONE;
                FileShareMode           shareModeNormal     = FileShareMode.FILE_SHARE_READ | FileShareMode.FILE_SHARE_WRITE;
                FileDesiredAccess       desiredAccess       = FileDesiredAccess.GENERIC_WRITE;
                FileCreationDisposition creationDisposition = FileCreationDisposition.OPEN_EXISTING;
                FileFlagsAndAttributes  flagsAttributes     = FileFlagsAndAttributes.FILE_FLAG_NORMAL | FileFlagsAndAttributes.FILE_FLAG_OVERLAPPED | FileFlagsAndAttributes.FILE_FLAG_NO_BUFFERING;

                //Try to open the device exclusively
                FileHandle = CreateFile(DevicePath, desiredAccess, shareModeExclusive, IntPtr.Zero, creationDisposition, flagsAttributes, IntPtr.Zero);
                Exclusive  = true;

                //Try to open the device normally
                if (FileHandle == null || FileHandle.IsInvalid || FileHandle.IsClosed)
                {
                    //Debug.WriteLine("Failed to open TetherScript device exclusively, opening normally.");
                    FileHandle = CreateFile(DevicePath, desiredAccess, shareModeNormal, IntPtr.Zero, creationDisposition, flagsAttributes, IntPtr.Zero);
                    Exclusive  = false;
                }

                //Check if the device is opened
                if (FileHandle == null || FileHandle.IsInvalid || FileHandle.IsClosed)
                {
                    //Debug.WriteLine("Failed to open TetherScript device: " + DevicePath);
                    Connected = false;
                    Installed = false;
                    return(false);
                }
                else
                {
                    //Debug.WriteLine("Opened TetherScript device: " + DevicePath + ", exclusively: " + Exclusive);
                    Connected = true;
                    Installed = true;
                    return(true);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failed to open TetherScript device: " + ex.Message);
                Connected = false;
                Installed = false;
                return(false);
            }
        }
Ejemplo n.º 27
0
 public static extern IntPtr CreateFile(
     String lpFileName,                  // File name
     FileDesiredAccess dwDesiredAccess,  // Access mode
     FileShareMode dwShareMode,          // Share mode
     IntPtr fileSecurityAttributes,      // Security Attributes
     FileCreationDisposition dwCreationDisposition,// How to create
     uint dwFlagsAndAttributes,          // File attributes
     uint hTemplateFile);
Ejemplo n.º 28
0
 public static extern SafeFileHandle CreateFile(string fileName, FileAccess desiredAccess, FileShare shareMode, IntPtr securityAttributes, FileCreationDisposition creationDisposition, FileAttributes flagsAndAttributes, IntPtr templateFile);
Ejemplo n.º 29
0
 public static extern SafePipeHandle CreateFile(string fileName,
     FileDesiredAccess desiredAccess, FileShareMode shareMode,
     SECURITY_ATTRIBUTES securityAttributes,
     FileCreationDisposition creationDisposition,
     int flagsAndAttributes, IntPtr hTemplateFile);
Ejemplo n.º 30
0
 internal static extern IntPtr CreateFile(
     string lpFileName,
     FileDesiredAccess dwDesiredAccess,
     FileShareMode dwShareMode,
     IntPtr lpSecurityAttributes,
     FileCreationDisposition dwCreationDisposition,
     FileAttributes dwFlagsAndAttributes,
     IntPtr hTemplateFile);