我QueryServiceStatus
用来接收当前服务状态,后来将其设置waitHint
为 0,因为 Process Hacker 显示默认值为 180000。我想改变其他的东西,比如controlsAccepted
. QueryServiceStatus
工作正常,但SetServiceStatus
返回 false,并Marshal.GetLastWin32Error
没有帮助。它返回 183,即 ERROR_ALREADY_EXISTS(当该文件已存在时无法创建文件。)因为我假设这是来自我计算机上运行的另一个应用程序。
IntPtr scm = WinAPI.Service.OpenSCManager(null, null, WinAPI.Service.SCM_ACCESS.SC_MANAGER_ALL_ACCESS);
IntPtr service = WinAPI.Service.OpenService(scm, "mySvc", WinAPI.Service.SERVICE_ACCESS.SERVICE_ALL_ACCESS);
var lpServiceStatus = new WinAPI.Service.SERVICE_STATUS();
bool queryStatus = WinAPI.Service.QueryServiceStatus(service, ref lpServiceStatus);
Svc.debugWr("queryStatus: " + queryStatus + (!queryStatus ? "LastWin32Error: " + Marshal.GetLastWin32Error() : ""));
Svc.debugWr("checkPoint: " + lpServiceStatus.checkPoint);
Svc.debugWr("controlsAccepted: " + lpServiceStatus.controlsAccepted);
Svc.debugWr("currentState" + lpServiceStatus.currentState);
Svc.debugWr("exit code: " + lpServiceStatus.serviceSpecificExitCode);
Svc.debugWr("serviceType: " + lpServiceStatus.serviceType);
Svc.debugWr("Wait hint: " + lpServiceStatus.waitHint);
Svc.debugWr("Win32 exit code: " + lpServiceStatus.win32ExitCode);
lpServiceStatus.waitHint = 0;
Svc.debugWr("Wait hint: " + lpServiceStatus.waitHint);
bool setStatus = WinAPI.Service.SetServiceStatus(service, ref lpServiceStatus);
Svc.debugWr("setStatus: " + setStatus + (!setStatus ? "LastWin32Error: " + Marshal.GetLastWin32Error() : ""));
这是SetServiceStatus
,SERVICE_STATUS
和的声明QueryServiceStatus
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool SetServiceStatus(IntPtr hServiceStatus, ref SERVICE_STATUS lpServiceStatus);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool QueryServiceStatus(IntPtr hService, ref SERVICE_STATUS dwServiceStatus);
[StructLayout(LayoutKind.Sequential)]
public struct SERVICE_STATUS {
public int serviceType;
public int currentState;
public int controlsAccepted;
public int win32ExitCode;
public int serviceSpecificExitCode;
public int checkPoint;
public int waitHint;
}