Exemplo n.º 1
0
        static void Main(string[] args)
        {
            //Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("he");
            for (int i = 0; i < args.Length; i++)
            {
                string s = args[i];
                if (s == "driverinstall" || s == "-driverinstall")
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new WelcomeDialog());
                    return;
                }
                else if (s == "re-enabledevice" || s == "-re-enabledevice")
                {
                    try
                    {
                        i++;
                        string deviceInstanceId = args[i];
                        DS4Devices.reEnableDevice(deviceInstanceId);
                        Environment.ExitCode = 0;
                        return;
                    }
                    catch (Exception)
                    {
                        Environment.ExitCode = Marshal.GetLastWin32Error();
                        return;
                    }
                }
            }
            System.Runtime.GCSettings.LatencyMode = System.Runtime.GCLatencyMode.LowLatency;

            try
            {
                Process.GetCurrentProcess().PriorityClass = System.Diagnostics.ProcessPriorityClass.High;
            }
            catch
            {
                // Ignore problems raising the priority.
            }
            try
            {
                // another instance is already running if OpenExsting succeeds.
                threadComEvent = EventWaitHandle.OpenExisting(SingleAppComEventName);
                threadComEvent.Set(); // signal the other instance.
                threadComEvent.Close();
                return;               // return immediatly.
            }
            catch { /* don't care about errors */ }
            // Create the Event handle
            threadComEvent = new EventWaitHandle(false, EventResetMode.AutoReset, SingleAppComEventName);
            CreateInterAppComThread();

            if (mutex.WaitOne(TimeSpan.Zero, true))
            {
                rootHub = new ControlService();
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new DS4Form(args));
                mutex.ReleaseMutex();
            }

            // End the communication thread.
            singleAppComThread.CancelAsync();
            while (singleAppComThread.IsBusy)
            {
                Thread.Sleep(50);
            }
            threadComEvent.Close();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            //Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("ja");
            for (int i = 0, argsLen = args.Length; i < argsLen; i++)
            {
                string s = args[i];
                if (s == "driverinstall" || s == "-driverinstall")
                {
                    Application.EnableVisualStyles();
                    Application.Run(new WelcomeDialog(true));
                    return;
                }
                else if (s == "re-enabledevice" || s == "-re-enabledevice")
                {
                    try
                    {
                        i++;
                        string deviceInstanceId = args[i];
                        DS4Devices.reEnableDevice(deviceInstanceId);
                        Environment.ExitCode = 0;
                        return;
                    }
                    catch (Exception)
                    {
                        Environment.ExitCode = Marshal.GetLastWin32Error();
                        return;
                    }
                }
                else if (s == "runtask" || s == "-runtask")
                {
                    TaskService ts     = new TaskService();
                    Task        tasker = ts.FindTask("RunDS4Windows");
                    if (tasker != null)
                    {
                        tasker.Run("");
                    }

                    Environment.ExitCode = 0;
                    return;
                }
            }

            GCSettings.LatencyMode = GCLatencyMode.LowLatency;

            try
            {
                Process.GetCurrentProcess().PriorityClass =
                    System.Diagnostics.ProcessPriorityClass.High;
            }
            catch { } // Ignore problems raising the priority.

            try
            {
                // another instance is already running if OpenExsting succeeds.
                threadComEvent = EventWaitHandle.OpenExisting(SingleAppComEventName,
                                                              System.Security.AccessControl.EventWaitHandleRights.Synchronize |
                                                              System.Security.AccessControl.EventWaitHandleRights.Modify);
                threadComEvent.Set(); // signal the other instance.
                threadComEvent.Close();
                return;               // return immediatly.
            }
            catch { /* don't care about errors */ }

            // Create the Event handle
            threadComEvent = new EventWaitHandle(false, EventResetMode.ManualReset, SingleAppComEventName);
            //System.Threading.Tasks.Task.Run(() => CreateTempWorkerThread());
            //CreateInterAppComThread();
            CreateTempWorkerThread();
            //System.Threading.Tasks.Task.Run(() => { Thread.CurrentThread.Priority = ThreadPriority.Lowest; CreateInterAppComThread(); Thread.CurrentThread.Priority = ThreadPriority.Lowest; }).Wait();

            //if (mutex.WaitOne(TimeSpan.Zero, true))
            //{
            createControlService();
            //rootHub = new ControlService();
            Application.EnableVisualStyles();
            ds4form = new DS4Form(args);
            Application.Run();
            //mutex.ReleaseMutex();
            //}

            exitComThread = true;
            threadComEvent.Set();  // signal the other instance.
            while (testThread.IsAlive)
            {
                Thread.SpinWait(500);
            }
            threadComEvent.Close();
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            //Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("ja");
            for (int i = 0, argsLen = args.Length; i < argsLen; i++)
            {
                string s = args[i];
                if (s == "driverinstall" || s == "-driverinstall")
                {
                    Application.EnableVisualStyles();
                    Application.Run(new Forms.WelcomeDialog(true));
                    return;
                }
                else if (s == "re-enabledevice" || s == "-re-enabledevice")
                {
                    try
                    {
                        i++;
                        string deviceInstanceId = args[i];
                        DS4Devices.reEnableDevice(deviceInstanceId);
                        Environment.ExitCode = 0;
                        return;
                    }
                    catch (Exception)
                    {
                        Environment.ExitCode = Marshal.GetLastWin32Error();
                        return;
                    }
                }
                else if (s == "runtask" || s == "-runtask")
                {
                    TaskService ts     = new TaskService();
                    Task        tasker = ts.FindTask("RunDS4Windows");
                    if (tasker != null)
                    {
                        tasker.Run("");
                    }

                    Environment.ExitCode = 0;
                    return;
                }
                else if (s == "command" || s == "-command")
                {
                    i++;

                    IntPtr hWndDS4WindowsForm = IntPtr.Zero;

                    if (args[i].Length > 0 && args[i].Length <= 256)
                    {
                        // Find the main DS4Form window handle and post WM_COPYDATA inter-process message. IPCClasName.dat file was created by the main DS4Windows process
                        // and it contains the name of the DS4Form .NET window class name string (the hash key part of the string is dynamically generated by .NET engine and it may change,
                        // so that's why the main process re-creates the file if the window class name is changed by .NET framework). Finding the WND handle usig both class name and window name
                        // limits chances that WM_COPYDATA message is sent to a wrong window.

                        hWndDS4WindowsForm = FindWindow(ReadIPCClassNameMMF(), "DS4Windows");
                        if (hWndDS4WindowsForm != IntPtr.Zero)
                        {
                            COPYDATASTRUCT cds;
                            cds.lpData = IntPtr.Zero;

                            try
                            {
                                cds.dwData = IntPtr.Zero;
                                cds.cbData = args[i].Length;
                                cds.lpData = Marshal.StringToHGlobalAnsi(args[i]);
                                SendMessage(hWndDS4WindowsForm, Forms.DS4Form.WM_COPYDATA, IntPtr.Zero, ref cds);
                            }
                            finally
                            {
                                if (cds.lpData != IntPtr.Zero)
                                {
                                    Marshal.FreeHGlobal(cds.lpData);
                                }
                            }
                        }
                        else
                        {
                            Environment.ExitCode = 2;
                        }
                    }
                    else
                    {
                        Environment.ExitCode = 1;
                    }

                    return;
                }
            }

            try
            {
                Process.GetCurrentProcess().PriorityClass =
                    System.Diagnostics.ProcessPriorityClass.High;
            }
            catch { } // Ignore problems raising the priority.

            // Force Normal IO Priority
            IntPtr ioPrio = new IntPtr(2);

            Util.NtSetInformationProcess(Process.GetCurrentProcess().Handle,
                                         Util.PROCESS_INFORMATION_CLASS.ProcessIoPriority, ref ioPrio, 4);

            // Force Normal Page Priority
            IntPtr pagePrio = new IntPtr(5);

            Util.NtSetInformationProcess(Process.GetCurrentProcess().Handle,
                                         Util.PROCESS_INFORMATION_CLASS.ProcessPagePriority, ref pagePrio, 4);

            try
            {
                // another instance is already running if OpenExsting succeeds.
                threadComEvent = EventWaitHandle.OpenExisting(SingleAppComEventName,
                                                              System.Security.AccessControl.EventWaitHandleRights.Synchronize |
                                                              System.Security.AccessControl.EventWaitHandleRights.Modify);
                threadComEvent.Set(); // signal the other instance.
                threadComEvent.Close();
                return;               // return immediatly.
            }
            catch { /* don't care about errors */ }

            // Create the Event handle
            threadComEvent = new EventWaitHandle(false, EventResetMode.ManualReset, SingleAppComEventName);
            //System.Threading.Tasks.Task.Run(() => CreateTempWorkerThread());
            //CreateInterAppComThread();
            CreateTempWorkerThread();
            //System.Threading.Tasks.Task.Run(() => { Thread.CurrentThread.Priority = ThreadPriority.Lowest; CreateInterAppComThread(); Thread.CurrentThread.Priority = ThreadPriority.Lowest; }).Wait();

            //if (mutex.WaitOne(TimeSpan.Zero, true))
            //{
            createControlService();
            //rootHub = new ControlService();
            Application.EnableVisualStyles();
            ds4form = new Forms.DS4Form(args);
            Application.Run();
            //mutex.ReleaseMutex();
            //}

            exitComThread = true;
            threadComEvent.Set();  // signal the other instance.
            while (testThread.IsAlive)
            {
                Thread.SpinWait(500);
            }
            threadComEvent.Close();

            if (ipcClassNameMMA != null)
            {
                ipcClassNameMMA.Dispose();
            }
            if (ipcClassNameMMF != null)
            {
                ipcClassNameMMF.Dispose();
            }
        }