Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15728

VS 2017 Trying to restart service in VB.NET console app

$
0
0
Is there a way to specify the other privileges that may be required in code? Or does something need to be changed on the server? My goal is to allow certain users to stop and start a particular service because often we run out of licenses.

Thanks!

This is my error:
Code:

System.InvalidOperationException: 'Cannot open Service Control Manager on computer 'ENGSERV'. This operation might require other privileges.'
This is my code:
Code:

Imports System
Imports System.ServiceProcess
Imports System.IO
Imports System.Threading

Imports System.Runtime.InteropServices
Imports System.Security.Principal
Imports System.Security.Permissions
Imports Microsoft.Win32.SafeHandles
Imports System.Runtime.ConstrainedExecution
Imports System.Security

Module Module1
    Private Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal un As String, ByVal domain As String, ByVal pw As String, ByVal LogonType As Integer, ByVal LogonProvider As Integer, ByRef Token As IntPtr) As Boolean
    Public Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Boolean

    Sub Main()
        Dim tokenHandle As New IntPtr(0)

        If LogonUser("Administrator", "WACONIAMFG", "12345678", 2, 0, tokenHandle) Then
            Dim newId As New WindowsIdentity(tokenHandle)
            Using impersonatedUser As WindowsImpersonationContext = newId.Impersonate()
                'perform impersonated commands
                Console.WriteLine("Connected.")

                Dim sc As System.ServiceProcess.ServiceController = New System.ServiceProcess.ServiceController("SolidWorks SolidNetWork License Manager", "ENGSERV")
                sc.Stop()
                Thread.Sleep(5000)
                sc.Start()

                Console.WriteLine("Service restarted for SolidNetWork License Manager...")
                Console.ReadLine()

            End Using
            CloseHandle(tokenHandle)

        Else
            Console.WriteLine("Not Connected.")
            Console.ReadLine()

        End If

    End Sub

End Module


Viewing all articles
Browse latest Browse all 15728

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>