远程计算机上的更新服务描述失败

时间:2018-06-08 16:39:54

标签: c# windows-services remote-management service-installer

我创建了一个安装Windows服务的类,然后在远程计算机上启动它。我可以很好地安装服务,并启动它,但我在更改远程计算机上的服务描述时遇到问题

以下是我正在使用的代码:

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool ChangeServiceConfig2(ServiceControlHandle hService, SERVICE_INFO_LEVEL dwInfoLevel, object lpInfo=null);


    public enum SERVICE_INFO_LEVEL
    {
        SERVICE_CONFIG_DESCRIPTION = 1,
        SERVICE_CONFIG_FAILURE_ACTIONS = 2,
        SERVICE_CONFIG_DELAYED_AUTO_START_INFO = 3,
        SERVICE_CONFIG_FAILURE_ACTIONS_FLAG = 4,
        SERVICE_CONFIG_SERVICE_SID_INFO = 5,
        SERVICE_CONFIG_REQUIRED_PRIVILEGES_INFO = 6,
        SERVICE_CONFIG_PRESHUTDOWN_INFO = 7,
        SERVICE_CONFIG_TRIGGER_INFO = 8,
        SERVICE_CONFIG_PREFERRED_NODE = 9,
        SERVICE_CONFIG_LAUNCH_PROTECTED = 12
    }

这是创建服务后使用的代码,但在开始之前,sHandle是从creatservice派生的:

    ChangeServiceConfig2(sHandle, 
    NativeMethods.SERVICE_INFO_LEVEL.SERVICE_CONFIG_DESCRIPTION, 
    ServiceDescription.ToString());

我正在使用此功能:https://msdn.microsoft.com/en-us/library/windows/desktop/ms681988(v=vs.85).aspx

我得到的错误是:

System.AccessViolationException:'尝试读取或写入受保护的内存。这通常表明其他内存已损坏。'

EDITED: 不再出错,但现在我只添加了描述的第一个字母,不知道为什么。以下是我所做的更改:

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool ChangeServiceConfig2(ServiceControlHandle hService, int dwInfoLevel, [MarshalAs(UnmanagedType.Struct)] ref SERVICE_DESCRIPTION lpInfo);


    public enum SERVICE_INFO_LEVEL:int
    {
        SERVICE_CONFIG_DESCRIPTION = 0X01,
        SERVICE_CONFIG_FAILURE_ACTIONS = 0X02,
        SERVICE_CONFIG_DELAYED_AUTO_START_INFO = 0X03,
        SERVICE_CONFIG_FAILURE_ACTIONS_FLAG = 0X04,
        SERVICE_CONFIG_SERVICE_SID_INFO = 0X05,
        SERVICE_CONFIG_REQUIRED_PRIVILEGES_INFO = 0X06,
        SERVICE_CONFIG_PRESHUTDOWN_INFO = 0X07,
        SERVICE_CONFIG_TRIGGER_INFO = 0X08,
        SERVICE_CONFIG_PREFERRED_NODE = 0X09,
        SERVICE_CONFIG_LAUNCH_PROTECTED = 0X12
    }


    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct SERVICE_DESCRIPTION
    {
        public string lpDescription;
    }

通话方法

    SERVICE_DESCRIPTION DESC = new NativeMethods.SERVICE_DESCRIPTION()
                        {
                            lpDescription = ServiceDescription.ToString()
                        };
    ChangeServiceConfig2(sHandle,
                            (int)NativeMethods.SERVICE_INFO_LEVEL.SERVICE_CONFIG_DESCRIPTION,
                            ref DESC);

0 个答案:

没有答案