Beispiel #1
0
        static ChoWindowsManager()
        {
            try
            {
                ConsoleWindowHandle = ChoKernel32.GetConsoleWindow();
                MainWindowHandle    = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;

                if (MainWindowHandle != IntPtr.Zero)
                {
                    _origExWindowStyle = ChoUser32.GetWindowLong(MainWindowHandle, (int)GwlIndexEnum.GWL_EXSTYLE);
                }

                if (Environment.UserInteractive)
                {
                    if (ConsoleWindowHandle != IntPtr.Zero)
                    {
                        ApplicationMode = ChoApplicationMode.Console;
                    }
                    else
                    {
                        ApplicationMode = ChoApplicationMode.Windows;
                    }
                }
                else
                {
                    ApplicationMode = ChoApplicationMode.Service;
                    if (HttpContext.Current != null)
                    {
                        ApplicationMode = ChoApplicationMode.Web;
                    }
                }
            }
            catch { }
        }
Beispiel #2
0
 internal static void RegisterConsoleControlHandler()
 {
     if (!ChoConsoleSettings.Me.DisableConsoleCtrlHandler)
     {
         _consoleCtrlHandler = new ConsoleCtrlMessageHandler(ConsoleCtrlHandler);
         GC.KeepAlive(_consoleCtrlHandler);
         ChoKernel32.SetConsoleCtrlHandler(_consoleCtrlHandler, true);
     }
 }
Beispiel #3
0
        public static string ToShortName(string longName)
        {
            if (longName.IsNullOrWhiteSpace())
            {
                return(longName);
            }

            StringBuilder s = new StringBuilder(1000);

            longName = ChoPath.GetFullPath(longName);
            uint iSize = (uint)s.Capacity;
            uint iRet  = ChoKernel32.GetShortPathName(longName, s, iSize);

            return(s.ToString());
        }
Beispiel #4
0
        public static string ToShortFileName(string filePath)
        {
            if (filePath.IsNullOrWhiteSpace())
            {
                return(filePath);
            }

            StringBuilder s = new StringBuilder(1000);

            filePath = ChoPath.GetFullPath(filePath);
            string directory = Path.GetDirectoryName(filePath);
            string fileName  = Path.GetFileName(filePath);

            uint iSize = (uint)s.Capacity;
            uint iRet  = ChoKernel32.GetShortPathName(directory, s, iSize);

            return(Path.Combine(s.ToString(), fileName));
        }
Beispiel #5
0
        internal static void Initialize()
        {
            if (!ChoApplicationHost.IsApplicationHostUsed)
            {
                return;
            }

            if (!Environment.UserInteractive)
            {
                //Windows Service Mode
                ServiceBase[] ServicesToRun = new ServiceBase[]
                {
                    new ChoService()
                };
                ServiceBase.Run(ServicesToRun);
            }
            else
            {
                //Parse command line arguments, install,
                try
                {
                    ChoServiceCommandLineArgs serviceCmdLineArgs = new ChoServiceCommandLineArgs();

                    ServiceController sc = new ServiceController(ChoApplication.Host.ServiceName, Environment.MachineName);
                    if (serviceCmdLineArgs.InstallService)
                    {
                        ManagedInstallerClass.InstallHelper(new string[] { ChoApplication.EntryAssemblyLocation });
                    }
                    else if (serviceCmdLineArgs.UninstallService)
                    {
                        ManagedInstallerClass.InstallHelper(new string[] { "/u", ChoApplication.EntryAssemblyLocation });
                    }
                    else if (serviceCmdLineArgs.StartService)
                    {
                        sc.Start();
                    }
                    else if (serviceCmdLineArgs.StopService)
                    {
                        sc.Stop();
                    }
                    else if (serviceCmdLineArgs.PauseService)
                    {
                        sc.Pause();
                    }
                    else if (serviceCmdLineArgs.ContinueService)
                    {
                        sc.Continue();
                    }
                    else if (serviceCmdLineArgs.ExecuteCommand != Int32.MinValue)
                    {
                        sc.ExecuteCommand(serviceCmdLineArgs.ExecuteCommand);
                    }
                    else
                    {
                        //Console mode
                        if (ChoApplication.ApplicationMode == ChoApplicationMode.Console)
                        {
                            if (ChoConsoleSettings.Me.ConsoleMode != uint.MinValue && ChoWindowsManager.ConsoleWindowHandle != IntPtr.Zero)
                            {
                                ChoKernel32.SetConsoleMode(ChoWindowsManager.ConsoleWindowHandle, ChoConsoleSettings.Me.ConsoleMode);
                            }

                            ChoApplicationHost.RegisterConsoleControlHandler();
                        }
                        ChoApplication.Host.OnStartService(ChoApplication.Host.Args);
                    }
                }
                catch (ChoFatalApplicationException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    ChoApplication.WriteToEventLog(ex.ToString(), EventLogEntryType.Error);
                }
            }
        }
Beispiel #6
0
        internal static void Initialize(ChoApplicationHost host)
        {
            if (!ChoApplicationHost.IsApplicationHostUsed)
            {
                return;
            }

            if (!Environment.UserInteractive)
            {
                //Debugger.Break();
                ChoService service = new ChoService(host);

                service.CanHandlePowerEvent         = ChoServiceInstallerSettings.Me.CanHandlePowerEvent;
                service.CanHandleSessionChangeEvent = ChoServiceInstallerSettings.Me.CanHandleSessionChangeEvent;
                service.CanPauseAndContinue         = ChoServiceInstallerSettings.Me.CanPauseAndContinue;
                service.CanShutdown = ChoServiceInstallerSettings.Me.CanShutdown;
                service.CanStop     = ChoServiceInstallerSettings.Me.CanStop;
                service.AutoLog     = ChoServiceInstallerSettings.Me.AutoLog;
                //service.ExitCode = ChoServiceInstallerSettings.Me.ExitCode;

                //Windows Service Mode
                ServiceBase[] ServicesToRun = new ServiceBase[]
                {
                    service
                };
                ServiceBase.Run(ServicesToRun);
            }
            else
            {
                //Parse command line arguments, install,
                //try
                //{
                ChoFrameworkCmdLineArgs frameworkCmdLineArgs = new ChoFrameworkCmdLineArgs();
                frameworkCmdLineArgs.Init();
                ChoServiceCommandLineArgs serviceCmdLineArgs = new ChoServiceCommandLineArgs();
                serviceCmdLineArgs.Init();

                ServiceController sc = new ServiceController(ChoServiceCommandLineArgs.GetServiceName(), Environment.MachineName);
                if (serviceCmdLineArgs.InstallService)
                {
                    ChoManagedInstallerClass.InstallService();

                    //Save the command line parameters
                    //if (serviceCmdLineArgs.ServiceParams != null)
                    //{
                    //    try
                    //    {
                    //        ChoRegistryKey _rkAppRun = new ChoRegistryKey(String.Format(RegRunSubKey.FormatString(ChoApplication.Host.ServiceName), true));
                    //        _rkAppRun.SetValue("ImagePath", "{0} {1}".FormatString(ChoAssembly.GetEntryAssembly().Location, ChoServiceCommandLineArgs.GetServiceParams().Replace("'", @"""")));
                    //    }
                    //    catch (Exception ex)
                    //    {
                    //        System.Diagnostics.Trace.TraceError(ex.ToString());
                    //    }
                    //}
                }
                else if (serviceCmdLineArgs.UninstallService)
                {
                    ChoManagedInstallerClass.UninstallService();
                }
                else if (serviceCmdLineArgs.StartService)
                {
                    if (!ChoWindowsIdentity.IsAdministrator())
                    {
                        ChoApplication.RestartAsAdmin();
                        return;
                    }

                    if (serviceCmdLineArgs.ServiceParams == null ||
                        serviceCmdLineArgs.ServiceParams.Length == 0)
                    {
                        //ChoEnvironment.CommandLineArgs = Environment.GetCommandLineArgs().Skip(1).ToArray();
                    }
                    else
                    {
                        string commandLineArgs = null;
                        if (serviceCmdLineArgs.ServiceParams.StartsWith("\"") &&
                            serviceCmdLineArgs.ServiceParams.EndsWith("\""))
                        {
                            commandLineArgs = serviceCmdLineArgs.ServiceParams.Substring(1, serviceCmdLineArgs.ServiceParams.Length - 2);
                        }
                        else
                        {
                            commandLineArgs = serviceCmdLineArgs.ServiceParams;
                        }

                        ChoEnvironment.CommandLineArgs = commandLineArgs.SplitNTrim(' ');
                    }

                    sc.Start(ChoEnvironment.CommandLineArgs);

                    if (ChoServiceInstallerSettings.Me.Timeout > 0)
                    {
                        sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(ChoServiceInstallerSettings.Me.Timeout));
                    }
                }
                else if (serviceCmdLineArgs.StopService)
                {
                    if (!ChoWindowsIdentity.IsAdministrator())
                    {
                        ChoApplication.RestartAsAdmin();
                        return;
                    }
                    sc.Stop();
                }
                else if (serviceCmdLineArgs.PauseService)
                {
                    if (!ChoWindowsIdentity.IsAdministrator())
                    {
                        ChoApplication.RestartAsAdmin();
                        return;
                    }
                    sc.Pause();
                }
                else if (serviceCmdLineArgs.ContinueService)
                {
                    if (!ChoWindowsIdentity.IsAdministrator())
                    {
                        ChoApplication.RestartAsAdmin();
                        return;
                    }
                    sc.Continue();
                }
                else if (serviceCmdLineArgs.ExecuteCommand != Int32.MinValue)
                {
                    if (!ChoWindowsIdentity.IsAdministrator())
                    {
                        ChoApplication.RestartAsAdmin();
                        return;
                    }
                    sc.ExecuteCommand(serviceCmdLineArgs.ExecuteCommand);
                }
                else
                {
                    //ChoApplicationMode? applicationMode = ChoFrameworkCmdLineArgs.GetApplicationMode();
                    //if (applicationMode != null)
                    //    ChoApplication.ApplicationMode = applicationMode.Value;

                    //ChoProfile.WriteLine(ChoApplication.ApplicationMode.ToString());
                    if (ChoApplication.ApplicationMode == ChoApplicationMode.Windows)
                    {
                        ChoApplication.Host.OnStartService(ChoEnvironment.CommandLineArgs);
                    }
                    else if (ChoApplication.ApplicationMode == ChoApplicationMode.Console)
                    {
                        if (ChoConsoleSettings.Me.ConsoleMode != uint.MinValue && ChoWindowsManager.ConsoleWindowHandle != IntPtr.Zero)
                        {
                            ChoKernel32.SetConsoleMode(ChoWindowsManager.ConsoleWindowHandle, (uint)ChoConsoleSettings.Me.ConsoleMode);
                        }

                        ChoApplicationHost.RegisterConsoleControlHandler();
                        ChoApplication.Host.OnStartService(ChoEnvironment.CommandLineArgs);
                        //ChoApplication.Host.OnStopService();
                    }
                    else if (ChoApplication.ApplicationMode == ChoApplicationMode.Web)
                    {
                        ChoApplication.Host.OnStartService(ChoEnvironment.CommandLineArgs);
                    }
                }
                //}
                //catch (ChoFatalApplicationException)
                //{
                //    throw;
                //}
                //catch (ChoCommandLineArgException argEx)
                //{
                //    ChoApplication.DisplayMsg(argEx.Message);
                //    throw;
                //}
                //catch (ChoCommandLineArgUsageException usageEx)
                //{
                //    ChoApplication.DisplayMsg(usageEx.Message);
                //    throw;
                //}
                //catch (Exception ex)
                //{
                //    ChoApplication.DisplayMsg(ex.Message, ex);
                //    throw;
                //}
            }
        }