【问题标题】:change windows service password修改windows服务密码
【发布时间】:2010-10-06 20:54:03
【问题描述】:

我在获取更改所提供 Windows 服务密码的代码时遇到问题。

有没有简单的方法来做到这一点。

我得到了这个链接,但它似乎不完整link text

这里他们没有声明 SC_MANAGER_ALL_ACCESS, m_pServiceHandle

有什么建议吗??谢谢

【问题讨论】:

    标签: c# asp.net windows-services


    【解决方案1】:

    在托管代码中使用 WMI,每 this site

    using System.Management;
    
    string objPath = string.Format("Win32_Service.Name='{0}'", serviceName);
    using (ManagementObject service = new ManagementObject(new ManagementPath(objPath)))
    {
       object[] wmiParams = new object[11];
       wmiParams[6] = username;
       wmiParams[7] = password;
       service.InvokeMethod("Change", wmiParams);
    }
    

    【讨论】:

      【解决方案2】:

      这很完美......

          private const int SC_MANAGER_ALL_ACCESS = 0x000F003F;      
          string serviceName;
          private const uint SERVICE_NO_CHANGE = 0xffffffff; //this value is found in winsvc.h
          private const uint SERVICE_QUERY_CONFIG = 0x00000001;
          private const uint SERVICE_CHANGE_CONFIG = 0x00000002;
          private const uint SERVICE_QUERY_STATUS = 0x00000004;
          private const uint SERVICE_ENUMERATE_DEPENDENTS = 0x00000008;
          private const uint SERVICE_START = 0x00000010;
          private const uint SERVICE_STOP = 0x00000020;
          private const uint SERVICE_PAUSE_CONTINUE = 0x00000040;
          private const uint SERVICE_INTERROGATE = 0x00000080;
          private const uint SERVICE_USER_DEFINED_CONTROL = 0x00000100;
          private const uint STANDARD_RIGHTS_REQUIRED = 0x000F0000;
          private const uint SERVICE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED |
                              SERVICE_CHANGE_CONFIG |
                              SERVICE_QUERY_STATUS |
                              SERVICE_ENUMERATE_DEPENDENTS |
                              SERVICE_START |
                              SERVICE_STOP |
                              SERVICE_PAUSE_CONTINUE |
                              SERVICE_INTERROGATE |
                              SERVICE_USER_DEFINED_CONTROL);
      
      
          [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
          public static extern Boolean ChangeServiceConfig(IntPtr hService, UInt32 nServiceType, UInt32 nStartType, UInt32 nErrorControl, String lpBinaryPathName, String lpLoadOrderGroup, IntPtr lpdwTagId, [In] char[] lpDependencies, String lpServiceStartName, String lpPassword, String lpDisplayName);
      
          [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
          static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, uint dwDesiredAccess);
      
          [DllImport("advapi32.dll", EntryPoint = "OpenSCManagerW", ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)]
          public static extern IntPtr OpenSCManager(string machineName, string databaseName, uint dwAccess);      
      
      
      
          private bool ServicePasswordChange(string changePassword, string strServiceName)
          {
              try
              {
                  IntPtr databaseHandle = OpenSCManager(null, null, SC_MANAGER_ALL_ACCESS);
                  if (databaseHandle == IntPtr.Zero)
                      throw new System.Runtime.InteropServices.ExternalException("Open Service Manager Error");
      
                  IntPtr pServiceHandle = OpenService(databaseHandle, strServiceName, SERVICE_QUERY_CONFIG | SERVICE_CHANGE_CONFIG);
                  if (pServiceHandle == IntPtr.Zero)
                      throw new System.Runtime.InteropServices.ExternalException("Open Service Error");
      
                  //This code is changing the password for the service.
      
                  if (!ChangeServiceConfig(pServiceHandle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, null, null,
                      IntPtr.Zero, null, null, changePassword, null))
                  {
                      int nError = Marshal.GetLastWin32Error();
                      Win32Exception win32Exception = new Win32Exception(nError);
                      throw new System.Runtime.InteropServices.ExternalException("Could not change password : " + win32Exception.Message);
                  }
                  return true;
              }
              catch (Exception ex)
              {
                  ErrFromApi_Label.Text = ex.ToString();
                  return false;
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-12-07
        • 2011-05-20
        • 2012-06-03
        • 1970-01-01
        • 2016-07-18
        • 2021-05-22
        • 2012-03-12
        相关资源
        最近更新 更多