Пример #1
0
        protected void Dispose(bool disposing)
        {
            if (!_isDisposed)
            {
                this.IsAvailable = false;

                if (disposing)
                {
                    // Dispose managed resources.
                }

                // Dispose unmanaged resources.
                if (_hSharedMemory != IntPtr.Zero)
                {
                    WinApi.CloseHandle(_hSharedMemory);
                }
                if (_pSharedMemory != IntPtr.Zero)
                {
                    WinApi.UnmapViewOfFile(_pSharedMemory);
                }

                if (_sharedMutex != null)
                {
                    _sharedMutex.Close();
                }
                if (_sharedReqEvent != null)
                {
                    _sharedReqEvent.Close();
                }
                if (_sharedRespEvent != null)
                {
                    _sharedRespEvent.Close();
                }
            }

            _isDisposed = true;
        }
        private void Dispose(bool disposing)
        {
            lock (_disposeLock)
            {
                if (!_isDisposed)
                {
                    if (disposing)
                    {
                        // Dispose managed resources.

                        // Dispose wait handles.
                        if (_procSafeWaitHandle != null)
                        {
                            _procSafeWaitHandle.Dispose();
                        }

                        // Abort monitor thread.
                        if (_monitorThread != null)
                        {
                            _monitorThread.Abort();
                        }

                        if (_consoleParams.IsAvailable)
                        {
                            // Close console window.
                            unsafe
                            {
                                ConsoleParams *consoleParams = (ConsoleParams *)_consoleParams.Get();

                                if (consoleParams->ConsoleWindowHandle != IntPtr.Zero)
                                {
                                    WinApi.SendMessage(consoleParams->ConsoleWindowHandle, WinApi.WM_CLOSE,
                                                       IntPtr.Zero, IntPtr.Zero);
                                }
                            }
                        }

                        // Dispose shared memory objects.
                        if (_consoleParams != null)
                        {
                            _consoleParams.Dispose();
                        }
                        if (_consoleScreenInfo != null)
                        {
                            _consoleScreenInfo.Dispose();
                        }
                        if (_consoleCursorInfo != null)
                        {
                            _consoleCursorInfo.Dispose();
                        }
                        if (_consoleBufferInfo != null)
                        {
                            _consoleBufferInfo.Dispose();
                        }
                        if (_consoleBuffer != null)
                        {
                            _consoleBuffer.Dispose();
                        }
                        if (_consoleCopyInfo != null)
                        {
                            _consoleCopyInfo.Dispose();
                        }
                        if (_consolePasteInfo != null)
                        {
                            _consolePasteInfo.Dispose();
                        }
                        if (_consoleMouseEvent != null)
                        {
                            _consoleMouseEvent.Dispose();
                        }
                        if (_consoleNewSizeInfo != null)
                        {
                            _consoleNewSizeInfo.Dispose();
                        }
                        if (_consoleNewScrollPos != null)
                        {
                            _consoleNewScrollPos.Dispose();
                        }

                        //// Kill console process.
                        //if (_process != null)
                        //{
                        //    _process.Kill();
                        //    _process.Dispose();
                        //}
                    }

                    // Dispose unmanaged resources.
                }

                _isDisposed = true;
            }
        }
        private void InjectDll(string dllFileName)
        {
            bool retValue;

            // Get handle to target process.
            _hProcess = WinApi.OpenProcess(ProcessAccessFlags.All, false, _procInfo.dwProcessId);
            if (_hProcess == IntPtr.Zero)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(),
                                         string.Format("Cannot open process with ID {0}.", _procInfo.dwProcessId));
            }

            // Get address of LoadLibrary function in kernel32.
            _hKernel32 = WinApi.GetModuleHandle("kernel32");
            if (_hKernel32 == IntPtr.Zero)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(),
                                         "Cannot get handle to kernel32 module.");
            }

            // LoadLibraryA (ascii)/LoadLibraryW (unicode)
            IntPtr addrLoadLibrary = WinApi.GetProcAddress(_hKernel32, "LoadLibraryA");

            if (addrLoadLibrary == IntPtr.Zero)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(),
                                         "Cannot find LoadLibrary function in kernel32 module.");
            }

            // Write file name of DLL into process memory.
            _procBaseAddress = WinApi.VirtualAllocEx(_hProcess, IntPtr.Zero, dllFileName.Length,
                                                     WinApi.MEM_COMMIT, WinApi.PAGE_READWRITE);
            if (_procBaseAddress == IntPtr.Zero)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(),
                                         "Error allocating virtual memory in target process.");
            }

            int bytesWritten;

            retValue = WinApi.WriteProcessMemory(_hProcess, _procBaseAddress,
                                                 Encoding.ASCII.GetBytes(dllFileName), dllFileName.Length, out bytesWritten);
            if (!retValue)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(),
                                         "Error writing DLL file name in target process memory.");
            }

            // Create remote thread to load library into target process.
            IntPtr hThread = WinApi.CreateRemoteThread(_hProcess, IntPtr.Zero, 0, addrLoadLibrary,
                                                       _procBaseAddress, 0, out _threadId);

            if (hThread == IntPtr.Zero)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(),
                                         "Error creating remote thread in target process.");
            }

            // Wait for thread to finish.
            int waitRet = WinApi.WaitForSingleObject(hThread, 10000);

            if (waitRet == WinApi.WAIT_FAILED)
            {
                throw new Win32Exception();
            }
            if (waitRet == WinApi.WAIT_TIMEOUT)
            {
                throw new TimeoutException();
            }

            // Get handle to loaded module from exit code.
            // Note: exit code will be zero unless DLL has already terminated.
            int exitCode;

            WinApi.GetExitCodeThread(hThread, out exitCode);
            _hModule = (IntPtr)exitCode;

            // Clean up.
            WinApi.VirtualFreeEx(_hProcess, _procBaseAddress, dllFileName.Length, WinApi.MEM_RELEASE);
            WinApi.CloseHandle(hThread);
        }
        public void Initialize()
        {
            int retValue;

            // Start new console process.
            StartProcess();

            // Inject DLL into console process.
            InjectDll(this.InjectionDllFileName);

            // Resume main thread of console process.
            WinApi.ResumeThread(_procInfo.hThread);
            WinApi.CloseHandle(_procInfo.hThread);

            // Wait for DLL to set console handle.
            retValue = WinApi.WaitForSingleObject(_consoleParams.RequestEvent.SafeWaitHandle
                                                  .DangerousGetHandle(), 1000);
            if (retValue == WinApi.WAIT_FAILED)
            {
                throw new Win32Exception();
            }
            if (retValue == WinApi.WAIT_TIMEOUT)
            {
                throw new TimeoutException();
            }

            // Create wait handle for console process.
            _procSafeWaitHandle = new SafeWaitHandle(_procInfo.hProcess, false);

            // Set language of console window.
            unsafe
            {
                ConsoleParams *consoleParams = (ConsoleParams *)_consoleParams.Get();

                if (!WinApi.PostMessage(consoleParams->ConsoleWindowHandle, WinApi.WM_INPUTLANGCHANGEREQUEST,
                                        IntPtr.Zero, new IntPtr(CultureInfo.CurrentCulture.KeyboardLayoutId)))
                {
                    throw new Win32Exception();
                }
            }

            // Start thread to monitor console.
            _monitorThread      = new Thread(new ThreadStart(MonitorThread));
            _monitorThread.Name = "Console Monitor";
            _monitorThread.Start();

            // Resume monitor thread.
            unsafe
            {
                ConsoleParams *consoleParams = (ConsoleParams *)_consoleParams.Get();
                IntPtr         hHookThread   = WinApi.OpenThread(ThreadAccess.ALL_ACCESS, false,
                                                                 consoleParams->HookThreadId);

                if (WinApi.ResumeThread(hHookThread) == -1)
                {
                    throw new Win32Exception();
                }
                WinApi.CloseHandle(hHookThread);
            }

            // Raise event.
            if (ConsoleOpened != null)
            {
                ConsoleOpened(this, new EventArgs());
            }
        }
Пример #5
0
 public static void Beep(int freq, int duration)
 {
     WinApi.Beep(freq, duration);
 }
Пример #6
0
 public static void Beep()
 {
     WinApi.MessageBeep(-1);
 }