now it works

pull/18/head
Kohsuke Kawaguchi 2013-04-20 17:38:49 -07:00
parent 4caa17f921
commit becb2d249a
3 changed files with 21 additions and 31 deletions

View File

@ -26,7 +26,6 @@ namespace Advapi32
{ {
throw new Exception(String.Format("Error opening service for modifying. Error returned was: 0x{0:X}", Marshal.GetLastWin32Error())); throw new Exception(String.Format("Error opening service for modifying. Error returned was: 0x{0:X}", Marshal.GetLastWin32Error()));
} }
Console.WriteLine("Opened " + serviceName);
return new Service(svcHandle); return new Service(svcHandle);
} }
@ -47,41 +46,30 @@ namespace Advapi32
Handle = service; Handle = service;
} }
public void ChangeConfig(TimeSpan failureResetPeriod) public void ChangeConfig(TimeSpan failureResetPeriod, SC_ACTION[] actions)
{ {
SERVICE_FAILURE_ACTIONS sfa = new SERVICE_FAILURE_ACTIONS(); SERVICE_FAILURE_ACTIONS sfa = new SERVICE_FAILURE_ACTIONS();
sfa.dwResetPeriod = failureResetPeriod.Seconds; sfa.dwResetPeriod = failureResetPeriod.Seconds;
sfa.lpRebootMsg = ""; // delete message sfa.lpRebootMsg = ""; // delete message
sfa.lpCommand = ""; // delete the command to run sfa.lpCommand = ""; // delete the command to run
sfa.cActions = 0;
SC_ACTION[] lpsaActions = new SC_ACTION[2]; int len = Marshal.SizeOf(typeof(SC_ACTION));
lpsaActions[0] = new SC_ACTION(SC_ACTION_TYPE.SC_ACTION_RESTART, 1000);
lpsaActions[1] = new SC_ACTION(SC_ACTION_TYPE.SC_ACTION_RESTART, 2000);
lpsaActions[1] = new SC_ACTION(SC_ACTION_TYPE.SC_ACTION_RESTART, 5000);
sfa.lpsaActions = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(SC_ACTION)) * 2); sfa.cActions = actions.Length;
sfa.lpsaActions = Marshal.AllocHGlobal(len * actions.Length);
try try
{ {
for (int i = 0; i < lpsaActions.Length; i++) for (int i = 0; i < actions.Length; i++)
{ {
Marshal.StructureToPtr(lpsaActions[i], sfa.lpsaActions/* new IntPtr(sfa.lpsaActions.ToInt64() + i * Marshal.SizeOf(typeof(SC_ACTION)))*/, false); Marshal.StructureToPtr(actions[i], new IntPtr(sfa.lpsaActions.ToInt64() + i * len), false);
} }
Console.WriteLine("Changing config to 2: sizeof(x)"+Marshal.SizeOf(typeof(SC_ACTION))); if (!Advapi32.ChangeServiceConfig2(Handle, SERVICE_CONFIG_INFOLEVEL.SERVICE_CONFIG_FAILURE_ACTIONS, ref sfa))
int x = Marshal.GetLastWin32Error();
sfa.lpsaActions = IntPtr.Zero;
if (!Advapi32.ChangeServiceConfig2(Handle, SERVICE_CONFIG_INFOLEVEL.SERVICE_CONFIG_FAILURE_ACTIONS, sfa))
throw new Exception("Failed to change the failure actions", new Win32Exception()); throw new Exception("Failed to change the failure actions", new Win32Exception());
throw new Exception("OK:" + x + "/" + Marshal.GetLastWin32Error(), new Win32Exception());
} }
finally finally
{ {
Marshal.FreeCoTaskMem(sfa.lpsaActions); Marshal.FreeHGlobal(sfa.lpsaActions);
} }
} }
@ -99,18 +87,18 @@ namespace Advapi32
/// </summary> /// </summary>
internal class Advapi32 internal class Advapi32
{ {
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)] [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)] [return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool ChangeServiceConfig2(IntPtr hService, SERVICE_CONFIG_INFOLEVEL dwInfoLevel, IntPtr lpInfo); internal static extern bool ChangeServiceConfig2(IntPtr hService, SERVICE_CONFIG_INFOLEVEL dwInfoLevel, IntPtr lpInfo);
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)] [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)] [return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool ChangeServiceConfig2(IntPtr hService, SERVICE_CONFIG_INFOLEVEL dwInfoLevel, SERVICE_FAILURE_ACTIONS sfa); internal static extern bool ChangeServiceConfig2(IntPtr hService, SERVICE_CONFIG_INFOLEVEL dwInfoLevel, ref SERVICE_FAILURE_ACTIONS sfa);
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)] [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern IntPtr OpenSCManager(string machineName, string databaseName, uint dwAccess); internal static extern IntPtr OpenSCManager(string machineName, string databaseName, uint dwAccess);
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)] [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, uint dwDesiredAccess); internal static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, uint dwDesiredAccess);
[DllImport("advapi32.dll", SetLastError = true)] [DllImport("advapi32.dll", SetLastError = true)]
@ -296,7 +284,7 @@ namespace Advapi32
} }
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms685939(v=vs.85).aspx // http://msdn.microsoft.com/en-us/library/windows/desktop/ms685939(v=vs.85).aspx
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]
public struct SERVICE_FAILURE_ACTIONS public struct SERVICE_FAILURE_ACTIONS
{ {
/// <summary> /// <summary>
@ -305,9 +293,9 @@ namespace Advapi32
/// </summary> /// </summary>
public int dwResetPeriod; public int dwResetPeriod;
[MarshalAs(UnmanagedType.LPTStr)] [MarshalAs(UnmanagedType.LPWStr)]
public string lpRebootMsg; public string lpRebootMsg;
[MarshalAs(UnmanagedType.LPTStr)] [MarshalAs(UnmanagedType.LPWStr)]
public string lpCommand; public string lpCommand;
public int cActions; public int cActions;
public IntPtr/*SC_ACTION[]*/ lpsaActions; public IntPtr/*SC_ACTION[]*/ lpsaActions;

View File

@ -12,6 +12,7 @@ using WMI;
using System.Xml; using System.Xml;
using System.Threading; using System.Threading;
using Microsoft.Win32; using Microsoft.Win32;
using Advapi32;
namespace winsw namespace winsw
{ {
@ -421,7 +422,7 @@ namespace winsw
}); });
} }
public static int _Main(string[] args) public static int Main(string[] args)
{ {
try try
{ {
@ -556,7 +557,9 @@ namespace winsw
{ {
using (Advapi32.Service sc = scm.Open(d.Id)) using (Advapi32.Service sc = scm.Open(d.Id))
{ {
sc.ChangeConfig(TimeSpan.FromHours(48)); SC_ACTION[] lpsaActions = new SC_ACTION[1];
lpsaActions[0] = new SC_ACTION(SC_ACTION_TYPE.SC_ACTION_RESTART, 1000);
sc.ChangeConfig(TimeSpan.FromHours(48),lpsaActions);
} }
} }
} }

View File

@ -53,7 +53,6 @@
<Compile Include="Main.cs"> <Compile Include="Main.cs">
<SubType>Component</SubType> <SubType>Component</SubType>
</Compile> </Compile>
<Compile Include="NotGettingAnywhere.cs" />
<Compile Include="PeriodicRollingCalendar.cs" /> <Compile Include="PeriodicRollingCalendar.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ServiceDescriptor.cs" /> <Compile Include="ServiceDescriptor.cs" />