#4 Enable required services on startup

Open
opened 7 months ago by Styris · 0 comments
Styris commented 7 months ago
Owner

LanmanServer and LanmanWorkstation must both be enabled for amecs to function. This is due to the use of DirectoryServices.

C# code necessary for implementation, adapt as needed:

try
{
    var server = new ServiceController("LanmanServer");
    ChangeStartMode(server, ServiceStartMode.Automatic);
    var workstation = new ServiceController("LanmanWorkstation");
    ChangeStartMode(workstation, ServiceStartMode.Automatic);
    
    server.Start();
    workstation.Start();
    
    server.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromMilliseconds(10000));
    workstation.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromMilliseconds(10000));
}
catch (Exception e)
{
    ErrorLogger.WriteToErrorLog("Service set error: " + e.Message, e.StackTrace, "Account error", User);
}



[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);

[DllImport("advapi32.dll", EntryPoint = "CloseServiceHandle")]
public static extern int CloseServiceHandle(IntPtr hSCObject);

private const uint SERVICE_NO_CHANGE = 0xFFFFFFFF;
private const uint SERVICE_QUERY_CONFIG = 0x00000001;
private const uint SERVICE_CHANGE_CONFIG = 0x00000002;
private const uint SC_MANAGER_ALL_ACCESS = 0x000F003F;

public static void ChangeStartMode(ServiceController svc, ServiceStartMode mode)
{
    var scManagerHandle = OpenSCManager(null, null, SC_MANAGER_ALL_ACCESS);
    if (scManagerHandle == IntPtr.Zero)
    {
        throw new ExternalException("Open Service Manager Error");
    }

    var serviceHandle = OpenService(
        scManagerHandle,
        svc.ServiceName,
        SERVICE_QUERY_CONFIG | SERVICE_CHANGE_CONFIG);

    if (serviceHandle == IntPtr.Zero)
    {
        throw new ExternalException("Open Service Error");
    }

    var result = ChangeServiceConfig(
        serviceHandle,
        SERVICE_NO_CHANGE,
        (uint)mode,
        SERVICE_NO_CHANGE,
        null,
        null,
        IntPtr.Zero,
        null,
        null,
        null,
        null);

    if (result == false)
    {
        var nError = Marshal.GetLastWin32Error();
        var win32Exception = new Win32Exception(nError);
        throw new ExternalException("Could not change service start type: "
            + win32Exception.Message);
    }

    CloseServiceHandle(serviceHandle);
    CloseServiceHandle(scManagerHandle);
}
LanmanServer and LanmanWorkstation must both be enabled for amecs to function. This is due to the use of [DirectoryServices](https://www.nuget.org/packages/System.DirectoryServices/). C# code necessary for implementation, adapt as needed: ```csharp try { var server = new ServiceController("LanmanServer"); ChangeStartMode(server, ServiceStartMode.Automatic); var workstation = new ServiceController("LanmanWorkstation"); ChangeStartMode(workstation, ServiceStartMode.Automatic); server.Start(); workstation.Start(); server.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromMilliseconds(10000)); workstation.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromMilliseconds(10000)); } catch (Exception e) { ErrorLogger.WriteToErrorLog("Service set error: " + e.Message, e.StackTrace, "Account error", User); } [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); [DllImport("advapi32.dll", EntryPoint = "CloseServiceHandle")] public static extern int CloseServiceHandle(IntPtr hSCObject); private const uint SERVICE_NO_CHANGE = 0xFFFFFFFF; private const uint SERVICE_QUERY_CONFIG = 0x00000001; private const uint SERVICE_CHANGE_CONFIG = 0x00000002; private const uint SC_MANAGER_ALL_ACCESS = 0x000F003F; public static void ChangeStartMode(ServiceController svc, ServiceStartMode mode) { var scManagerHandle = OpenSCManager(null, null, SC_MANAGER_ALL_ACCESS); if (scManagerHandle == IntPtr.Zero) { throw new ExternalException("Open Service Manager Error"); } var serviceHandle = OpenService( scManagerHandle, svc.ServiceName, SERVICE_QUERY_CONFIG | SERVICE_CHANGE_CONFIG); if (serviceHandle == IntPtr.Zero) { throw new ExternalException("Open Service Error"); } var result = ChangeServiceConfig( serviceHandle, SERVICE_NO_CHANGE, (uint)mode, SERVICE_NO_CHANGE, null, null, IntPtr.Zero, null, null, null, null); if (result == false) { var nError = Marshal.GetLastWin32Error(); var win32Exception = new Win32Exception(nError); throw new ExternalException("Could not change service start type: " + win32Exception.Message); } CloseServiceHandle(serviceHandle); CloseServiceHandle(scManagerHandle); } ```
Styris added the
bug
label 7 months ago
Styris added this to the Kanban project 7 months ago
Xyueta was assigned by Styris 7 months ago
he3als was assigned by Styris 7 months ago
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date

No due date set.

Dependencies

This issue currently doesn't have any dependencies.

Loading…
There is no content yet.