From becb2d249ab3d9b55607bbd736f5fcf65ba3f802 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Sat, 20 Apr 2013 17:38:49 -0700 Subject: [PATCH] now it works --- Advapi32.cs | 44 ++++++++++++++++---------------------------- Main.cs | 7 +++++-- winsw.csproj | 1 - 3 files changed, 21 insertions(+), 31 deletions(-) diff --git a/Advapi32.cs b/Advapi32.cs index aaaf2c0..7ea99fe 100755 --- a/Advapi32.cs +++ b/Advapi32.cs @@ -26,7 +26,6 @@ namespace Advapi32 { 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); } @@ -47,41 +46,30 @@ namespace Advapi32 Handle = service; } - public void ChangeConfig(TimeSpan failureResetPeriod) + public void ChangeConfig(TimeSpan failureResetPeriod, SC_ACTION[] actions) { SERVICE_FAILURE_ACTIONS sfa = new SERVICE_FAILURE_ACTIONS(); sfa.dwResetPeriod = failureResetPeriod.Seconds; sfa.lpRebootMsg = ""; // delete message sfa.lpCommand = ""; // delete the command to run - sfa.cActions = 0; - SC_ACTION[] lpsaActions = new SC_ACTION[2]; - 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); + int len = Marshal.SizeOf(typeof(SC_ACTION)); - sfa.lpsaActions = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(SC_ACTION)) * 2); + sfa.cActions = actions.Length; + sfa.lpsaActions = Marshal.AllocHGlobal(len * actions.Length); 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))); - int x = Marshal.GetLastWin32Error(); - - sfa.lpsaActions = IntPtr.Zero; - - if (!Advapi32.ChangeServiceConfig2(Handle, SERVICE_CONFIG_INFOLEVEL.SERVICE_CONFIG_FAILURE_ACTIONS, sfa)) + if (!Advapi32.ChangeServiceConfig2(Handle, SERVICE_CONFIG_INFOLEVEL.SERVICE_CONFIG_FAILURE_ACTIONS, ref sfa)) throw new Exception("Failed to change the failure actions", new Win32Exception()); - - throw new Exception("OK:" + x + "/" + Marshal.GetLastWin32Error(), new Win32Exception()); - } finally { - Marshal.FreeCoTaskMem(sfa.lpsaActions); + Marshal.FreeHGlobal(sfa.lpsaActions); } } @@ -99,18 +87,18 @@ namespace Advapi32 /// internal class Advapi32 { - [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)] + [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)] [return: MarshalAs(UnmanagedType.Bool)] 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)] - 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); - [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); [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 - [StructLayout(LayoutKind.Sequential)] + [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)] public struct SERVICE_FAILURE_ACTIONS { /// @@ -305,9 +293,9 @@ namespace Advapi32 /// public int dwResetPeriod; - [MarshalAs(UnmanagedType.LPTStr)] + [MarshalAs(UnmanagedType.LPWStr)] public string lpRebootMsg; - [MarshalAs(UnmanagedType.LPTStr)] + [MarshalAs(UnmanagedType.LPWStr)] public string lpCommand; public int cActions; public IntPtr/*SC_ACTION[]*/ lpsaActions; diff --git a/Main.cs b/Main.cs index 67cf8cf..f095088 100644 --- a/Main.cs +++ b/Main.cs @@ -12,6 +12,7 @@ using WMI; using System.Xml; using System.Threading; using Microsoft.Win32; +using Advapi32; namespace winsw { @@ -421,7 +422,7 @@ namespace winsw }); } - public static int _Main(string[] args) + public static int Main(string[] args) { try { @@ -556,7 +557,9 @@ namespace winsw { 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); } } } diff --git a/winsw.csproj b/winsw.csproj index ce336ce..cfbb8db 100644 --- a/winsw.csproj +++ b/winsw.csproj @@ -53,7 +53,6 @@ Component -