0
Q
更改窗口服务密码
A
回答
1
这完美的作品...
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;
}
}
3
使用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);
}
相关问题
- 1. 更改服务帐户密码
- 2. 更改服务名称后窗口服务无法启动
- 3. Powershell - 更改SQL Server服务密码并启动服务
- 4. 更改密码
- 5. 在窗口服务
- 6. 更新从服务窗口小部件
- 7. 如何更改订单服务正在窗口中加载?
- 8. MVVM光导航服务 - 更改主窗口标题和尺寸
- 9. Powerbuilder数据窗口密码加密
- 10. 对话窗口更改主窗口
- 11. 从c#代码运行窗口服务,无需安装服务
- 12. 密钥库更改密码
- 13. 应用程序更改JIRA/SVN服务器的Ldap密码
- 14. Jenkins通过SSH服务器发布密码更改超时
- 15. web2py通过服务电话更改密码
- 16. IIS管理员可以更改Windows服务帐户的密码
- 17. Team Foundation Server 2010在服务帐户密码更改后中断
- 18. 通过ssh在Linux服务器上更改密码的脚本
- 19. 通过php脚本更改samba服务器的密码
- 20. 如何为SQL服务器登录脚本更改密码?
- 21. 更改服务使用的帐户的密码
- 22. 更改旧密码
- 23. 更改密码Django
- 24. Spring引导更改服务器端口
- 25. 更改TeamCity web服务器的端口
- 26. 更改Apache Web服务器的端口
- 27. npm如何更改服务器端口
- 28. 如何更改窗口操作系统上的JIRA服务端口号
- 29. 更改iframe url更改父窗口url
- 30. 更改密码时验证旧密码