Beispiel #1
0
        public override void Eject()
        {
            IntPtr fileHandle = IntPtr.Zero;

            try {
                // Create an handle to the drive
                fileHandle = WindowsAPI.CreateFile(this.CreateDeviceIOPath,
                                                   WindowsAPI.GENERICREAD, 0, IntPtr.Zero,
                                                   WindowsAPI.OPENEXISTING, 0, IntPtr.Zero);

                if ((int)fileHandle != WindowsAPI.INVALID_HANDLE)
                {
                    // Eject the disk
                    uint returnedBytes;
                    WindowsAPI.DeviceIoControl(fileHandle, WindowsAPI.IOCTL_STORAGE_EJECT_MEDIA,
                                               IntPtr.Zero, 0,
                                               IntPtr.Zero, 0,
                                               out returnedBytes,
                                               IntPtr.Zero);
                }
            } catch {
                throw new Exception(Marshal.GetLastWin32Error().ToString());
            } finally {
                // Close Drive Handle
                WindowsAPI.CloseHandle(fileHandle);
                fileHandle = IntPtr.Zero;
            }
        }
Beispiel #2
0
        private static void UsDelay(int us)
        {
            long duetime    = -10 * us;
            int  hWaitTimer = WindowsAPI.CreateWaitableTimer(WindowsAPI.NULL, true, WindowsAPI.NULL);

            WindowsAPI.SetWaitableTimer(hWaitTimer, ref duetime, 0, WindowsAPI.NULL, WindowsAPI.NULL, false);
            while (WindowsAPI.MsgWaitForMultipleObjects(1, ref hWaitTimer, false, Timeout.Infinite, WindowsAPI.QS_TIMER))
            {
                ;
            }
            WindowsAPI.CloseHandle(hWaitTimer);
        }
Beispiel #3
0
        protected virtual void Disposing(bool disposing)
        {
            if (this.m_disposed)
            {
                return;
            }

            this.m_disposed = true;

            WindowsAPI.CloseHandle(this.m_token);
            if (this.m_context != null)
            {
                this.m_context.Dispose();
            }

            if (disposing)
            {
                GC.SuppressFinalize(this);
            }
        }
Beispiel #4
0
        public override bool Kill()
        {
            Close();

            if (!Exists)
            {
                return(true);
            }

            var pid = (uint)PID.ToInt32();
            var prc = pid != 0 ? WindowsAPI.OpenProcess(WindowsAPI.PROCESS_ALL_ACCESS, false, pid) : IntPtr.Zero;

            if (prc != IntPtr.Zero)
            {
                WindowsAPI.TerminateProcess(prc, 0);
                WindowsAPI.CloseHandle(prc);
            }

            return(!Exists);
        }
Beispiel #5
0
        public IdentityImpersonationScope(string domainName, string userName, string password, WindowsAPI.LogonType logonType, WindowsAPI.LogonProvider logonProvider)
        {
            if (string.IsNullOrWhiteSpace(userName))
            {
                throw new ArgumentException("userName is null or empty.", "userName");
            }

            if (!WindowsAPI.LogonUser(userName, !string.IsNullOrWhiteSpace(domainName) ? domainName : ".", password, logonType, logonProvider, ref this.m_token))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            try {
                this.m_context = WindowsIdentity.Impersonate(this.m_token);
            } catch (Exception ex) {
                try {
                    WindowsAPI.CloseHandle(this.m_token);
                } finally {
                    throw ex;
                }
            }
        }