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. SQL服務器更改登錄密碼
- 3. Django窗體更改密碼
- 4. 更改服務名稱後窗口服務無法啓動
- 5. Powershell - 更改SQL Server服務密碼並啓動服務
- 6. 加密appsettings app.config窗口服務
- 7. 窗口服務器r2 2008管理密碼忘記
- 8. 對窗口服務
- 9. 從窗口服務
- 10. 在窗口服務
- 11. 更改密碼
- 12. 更改密碼
- 13. 更改密碼
- 14. 解密XML文件與不對稱關鍵窗口窗體和窗口服務
- 15. 更新從服務窗口小部件
- 16. 從服務中的活動更改窗口參數
- 17. 如何更改訂單服務正在窗口中加載?
- 18. MVVM光導航服務 - 更改主窗口標題和尺寸
- 19. Powerbuilder數據窗口密碼加密
- 20. 對話窗口更改主窗口
- 21. 從c#代碼運行窗口服務,無需安裝服務
- 22. 密鑰庫更改密碼
- 23. Team Foundation Server 2008密碼更改中斷Team System Sharepoint服務
- 24. 如何更改服務器的管理員名稱和密碼
- 25. 如何爲SQL服務器登錄腳本更改密碼?
- 26. 在websphere應用程序服務器中更改密碼
- 27. Jenkins通過SSH服務器發佈密碼更改超時
- 28. 應用程序更改JIRA/SVN服務器的Ldap密碼
- 29. Team Foundation Server 2010在服務帳戶密碼更改後中斷
- 30. Powershell服務帳戶密碼更改登錄失敗