private ServiceInfo GetUnixServiceInfo(string sservicename)
        {
            ServiceInfo serviceInfo = new ServiceInfo();

            serviceInfo.serviceName = lvService.SelectedItems[0].SubItems[0].Text;

            IntPtr pHandle = ServiceManagerInteropWrapper.ApiLwSmAcquireServiceHandle(serviceInfo.serviceName);

            if (pHandle != IntPtr.Zero)
            {
                ServiceManagerApi.LW_SERVICE_STATUS serviceStatus = ServiceManagerInteropWrapper.ApiLwSmQueryServiceStatus(pHandle);
                switch (serviceStatus.state)
                {
                case ServiceManagerApi.LW_SERVICE_STATE.LW_SERVICE_STATE_RUNNING:
                case ServiceManagerApi.LW_SERVICE_STATE.LW_SERVICE_STATE_STARTING:
                    serviceInfo.IsRunning = true;
                    break;

                case ServiceManagerApi.LW_SERVICE_STATE.LW_SERVICE_STATE_STOPPED:
                case ServiceManagerApi.LW_SERVICE_STATE.LW_SERVICE_STATE_STOPPING:
                case ServiceManagerApi.LW_SERVICE_STATE.LW_SERVICE_STATE_PAUSED:
                case ServiceManagerApi.LW_SERVICE_STATE.LW_SERVICE_STATE_DEAD:
                    serviceInfo.IsRunning = false;
                    break;
                }
                ServiceManagerInteropWrapper.ApiLwSmReleaseServiceHandle(pHandle);
            }

            return(serviceInfo);
        }
        public override void Refresh()
        {
            base.Refresh();

            if (plugin == null)
            {
                plugin = pi as ServiceManagerPlugin;
            }

            this.lblCaption.Text = plugin.GetPlugInNode().Text;

            if (plugin.IsConnectionSuccess)
            {
                if (Configurations.currentPlatform == LikewiseTargetPlatform.Windows)
                {
                    if (!plugin.Do_LogonSCManager())
                    {
                        Logger.Log("Service Control Manager.Refresh(): Failed to authenticate the specified user");
                        return;
                    }
                }
            }
            else
            {
                return;
            }

            if (treeNode.Nodes.Count == 0 || treeNode.IsModified)
            {
                if (Configurations.currentPlatform == LikewiseTargetPlatform.Windows)
                {
                    Dictionary <string, string[]> services = ServiceManagerWindowsWrapper.EnumManagementServices();
                    if (services != null && services.Count != 0)
                    {
                        foreach (string name in services.Keys)
                        {
                            ListViewItem lvItem = new ListViewItem(services[name]);
                            lvItem.Tag = name;
                            lvService.Items.Add(lvItem);
                        }
                    }
                }
                else
                {
                    string[] sServiceEnum = ServiceManagerInteropWrapper.ApiLwSmEnumerateServices();
                    if (sServiceEnum != null)
                    {
                        foreach (string name in sServiceEnum)
                        {
                            string serviceState = string.Empty;
                            IntPtr pHandle      = ServiceManagerInteropWrapper.ApiLwSmAcquireServiceHandle(name);
                            if (pHandle != IntPtr.Zero)
                            {
                                ServiceManagerApi.LW_SERVICE_INFO   serviceInfo   = ServiceManagerInteropWrapper.ApiLwSmQueryServiceInfo(pHandle);
                                ServiceManagerApi.LW_SERVICE_STATUS serviceStatus = ServiceManagerInteropWrapper.ApiLwSmQueryServiceStatus(pHandle);

                                switch (serviceStatus.state)
                                {
                                case ServiceManagerApi.LW_SERVICE_STATE.LW_SERVICE_STATE_DEAD:
                                    serviceState = "Dead";
                                    break;

                                case ServiceManagerApi.LW_SERVICE_STATE.LW_SERVICE_STATE_PAUSED:
                                    serviceState = "Paused";
                                    break;

                                case ServiceManagerApi.LW_SERVICE_STATE.LW_SERVICE_STATE_RUNNING:
                                    serviceState = "Running";
                                    break;

                                case ServiceManagerApi.LW_SERVICE_STATE.LW_SERVICE_STATE_STARTING:
                                    serviceState = "Starting";
                                    break;

                                case ServiceManagerApi.LW_SERVICE_STATE.LW_SERVICE_STATE_STOPPED:
                                    serviceState = "Stopped";
                                    break;

                                case ServiceManagerApi.LW_SERVICE_STATE.LW_SERVICE_STATE_STOPPING:
                                    serviceState = "Stopping";
                                    break;
                                }

                                ListViewItem lvItem = new ListViewItem(new string[] { name,
                                                                                      serviceInfo.pwszDescription,
                                                                                      serviceState,
                                                                                      (serviceInfo.bAutostart)?"Automatic": "Manual",
                                                                                      "root" });
                                lvItem.Tag = name;
                                lvService.Items.Add(lvItem);

                                ServiceManagerInteropWrapper.ApiLwSmReleaseServiceHandle(pHandle);
                            }
                        }
                    }
                }
            }

            lvService.Columns[lvService.Columns.Count - 1].Width = 200;
        }
Exemplo n.º 3
0
        private void GetUnixServiceInfo()
        {
            txtStartParameters.Text = string.Empty;
            IntPtr pHandle = ServiceManagerInteropWrapper.ApiLwSmAcquireServiceHandle(serviceName);

            if (pHandle != IntPtr.Zero)
            {
                string sArgs = string.Empty;
                ServiceManagerApi.LW_SERVICE_STATUS serviceStatus = ServiceManagerInteropWrapper.ApiLwSmQueryServiceStatus(pHandle);
                ServiceManagerApi.LW_SERVICE_INFO   serviceInfo   = ServiceManagerInteropWrapper.ApiLwSmQueryServiceInfo(pHandle);

                lblServiceNameValue.Text = serviceName;
                txtDisplayName.Text      = serviceInfo.pwszName;
                txtDescription.Text      = serviceInfo.pwszDescription;
                txtPathToExecute.Text    = serviceInfo.pwszPath;

                foreach (string args in serviceInfo.ppwszArgs)
                {
                    txtStartParameters.Text += " " + args;
                }

                txtStartParameters.Text = txtStartParameters.Text.Trim();
                btnPause.Visible        = btnResume.Visible = false;

                cmbStartupType.SelectedIndex = serviceInfo.bAutostart ? 0 : 1;

                switch (serviceStatus.state)
                {
                case ServiceManagerApi.LW_SERVICE_STATE.LW_SERVICE_STATE_DEAD:
                    lblServiceStatusValue.Text = "Dead";
                    break;

                case ServiceManagerApi.LW_SERVICE_STATE.LW_SERVICE_STATE_PAUSED:
                    lblServiceStatusValue.Text = "Paused";
                    break;

                case ServiceManagerApi.LW_SERVICE_STATE.LW_SERVICE_STATE_RUNNING:
                    lblServiceStatusValue.Text = "Running";
                    break;

                case ServiceManagerApi.LW_SERVICE_STATE.LW_SERVICE_STATE_STARTING:
                    lblServiceStatusValue.Text = "Starting";
                    break;

                case ServiceManagerApi.LW_SERVICE_STATE.LW_SERVICE_STATE_STOPPED:
                    lblServiceStatusValue.Text = "Stopped";
                    break;

                case ServiceManagerApi.LW_SERVICE_STATE.LW_SERVICE_STATE_STOPPING:
                    lblServiceStatusValue.Text = "Stopping";
                    break;
                }

                switch (serviceStatus.state)
                {
                case ServiceManagerApi.LW_SERVICE_STATE.LW_SERVICE_STATE_RUNNING:
                case ServiceManagerApi.LW_SERVICE_STATE.LW_SERVICE_STATE_STARTING:
                    btnStart.Enabled = false;
                    btnStop.Enabled  = true;
                    break;


                case ServiceManagerApi.LW_SERVICE_STATE.LW_SERVICE_STATE_STOPPED:
                case ServiceManagerApi.LW_SERVICE_STATE.LW_SERVICE_STATE_STOPPING:
                case ServiceManagerApi.LW_SERVICE_STATE.LW_SERVICE_STATE_PAUSED:
                case ServiceManagerApi.LW_SERVICE_STATE.LW_SERVICE_STATE_DEAD:
                    btnStart.Enabled = true;
                    btnStop.Enabled  = false;
                    break;
                }
                ServiceManagerInteropWrapper.ApiLwSmReleaseServiceHandle(pHandle);
            }
        }