diff --git a/LVNC-RS/LVNC-RS-Svc.vb b/LVNC-RS/LVNC-RS-Svc.vb index 8a317d2..daa8dd8 100644 --- a/LVNC-RS/LVNC-RS-Svc.vb +++ b/LVNC-RS/LVNC-RS-Svc.vb @@ -32,11 +32,22 @@ ' 2 = Покси недоступен. Public bProxyStatus As Byte = 0 + ' Режим и права доступа + ' Возможны следующие значения: + ' 0 = Неопределённое состояние (при старте приложения); + ' 1 = От имени обычного пользователя. VNC еще не запущен; + ' 2 = От имени обычного пользователя. VNC работает в режиме приложения; + ' 3 = От имени обычного пользователя. VNC работает как служба; + ' 4 = От имени администратора. VNC еще не запущен; + ' 5 = От имени администратора. VNC работает как служба; + Public bExecMode As Byte = 0 + ' Настройки приложения Private oConfigFile As New IniFile ' Процесс winvnc.exe Private VNCProc As Process = Nothing + Private sVNCServiceName As String = "uvnc_service" ''' ''' Инициализация приложения @@ -64,6 +75,8 @@ If sProxyHost.Length = 0 Or sProxyPort.Length = 0 Then Return False + bExecMode = GetExecutionMode() + ' Генерируем ID и пароль sSupportID = GetSupportID() sSupportPasswd = GetSupportPasswd() @@ -131,14 +144,28 @@ Return False End Try + AddHandler VNCProc.Exited, AddressOf VNCProcessExited + Return True End Function - Public Function StopVNC() As Boolean - If VNCProc IsNot Nothing And VNCProc.Id <> 0 Then - VNCProc. + Public Sub StopVNC() + If VNCProc IsNot Nothing And Not VNCProc.HasExited Then + VNCProc.CloseMainWindow() + VNCProc.WaitForExit(1000) + + If Not VNCProc.HasExited Then + VNCProc.Kill() + End If End If - End Function + + VNCProc = Nothing + bExecMode = + End Sub + + Private Sub VNCProcessExited(ByVal sender As Object, ByVal e As System.EventArgs) + VNCProc.Close() + End Sub Private Function GetAllUsersDir() As String Dim sRegShellPath As String = "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" @@ -246,10 +273,110 @@ Private Function GetSupportPasswd() As String ' Проверяем наличие сохранённого пароля на случай перезапуска от имени администратора Dim pwd As String = oConfigFile.GetKeyValue("lvnc", "passwd") - If pwd IsNot Nothing And pwd <> String.Empty Then Return pwd + If pwd IsNot Nothing And pwd <> String.Empty Then + If bExecMode = 3 Then + Return pwd + End If + End If Dim rndGen As New System.Random - Return (rndGen.Next(1000, 10000).ToString) + pwd = rndGen.Next(1000, 10000).ToString + + Dim vncSetPasswd As New ProcessStartInfo + With vncSetPasswd + .Arguments = pwd + .FileName = sRunTimeDir & "\UltraVNC\setpasswd.exe" + .UseShellExecute = True + .WindowStyle = ProcessWindowStyle.Hidden + .WorkingDirectory = sRunTimeDir & "\UltraVNC" + End With + + Try + Dim p As Process = Process.Start(startInfo) + p.WaitForExit() + Catch ex As System.ComponentModel.Win32Exception + Return String.Empty + End Try + + Return pwd + End Function + + Private Function GetExecutionMode() As Byte + Dim bMode As Byte = 0 + + If My.User.IsInRole(ApplicationServices.BuiltInRole.Administrator) Then + bMode = 4 + + If IsServiceInstalled(sVNCServiceName) Then + If GetServiceStatus(sVNCServiceName) = "running" Then + bMode = 5 + End If + End If + Else + bMode = 1 + + If IsServiceInstalled(sVNCServiceName) Then + If GetServiceStatus(sVNCServiceName) = "running" Then + bMode = 3 + End If + Else + If VNCProc Is Nothing Then + For Each p As Process In Process.GetProcesses() + If p.ProcessName.Contains("winvnc") Then + VNCProc = p + End If + Next + + If VNCProc IsNot Nothing Then + StopVNC() + End If + Else + bMode = 2 + End If + End If + End If + + Return bMode + End Function + + Private Function IsServiceInstalled(ByVal sServiceName As String) As Boolean + Dim bResult As Boolean = False + Dim oServiceArray() As ServiceProcess.ServiceController = ServiceProcess.ServiceController.GetServices + + For Each oServiceController As ServiceProcess.ServiceController In oServiceArray + If oServiceController.ServiceName.Trim.ToUpper = sServiceName.Trim.ToUpper Then + Dim i As New ServiceProcess.ServiceControllerPermissionAttribute(Security.Permissions.SecurityAction.Demand) + Dim d As New ServiceProcess.ServiceControllerPermission + Try + If d.Any Then + d.ToString() + End If + Catch ex As Exception + End Try + + bResult = True + + Exit For + End If + Next + + Return bResult + End Function + + Public Function GetServiceStatus(ByVal sServiceName As String) As String + Dim sStatus As String = "NA" + Dim oWinSvc As New ServiceProcess.ServiceController + + oWinSvc.ServiceName = sServiceName + + Select Case oWinSvc.Status + Case ServiceProcess.ServiceControllerStatus.Stopped + sStatus = "stoped" + Case ServiceProcess.ServiceControllerStatus.Running + sStatus = "running" + End Select + + Return sStatus End Function End Module diff --git a/LVNC-RS/LVNC-RS.vbproj b/LVNC-RS/LVNC-RS.vbproj index 139a362..ad8a109 100644 --- a/LVNC-RS/LVNC-RS.vbproj +++ b/LVNC-RS/LVNC-RS.vbproj @@ -28,6 +28,8 @@ false false true + + AnyCPU @@ -72,6 +74,7 @@ + diff --git a/LVNC-RS/My Project/Settings.Designer.vb b/LVNC-RS/My Project/Settings.Designer.vb index 5fcc406..6e772f5 100644 --- a/LVNC-RS/My Project/Settings.Designer.vb +++ b/LVNC-RS/My Project/Settings.Designer.vb @@ -1,10 +1,10 @@ '------------------------------------------------------------------------------ ' -' This code was generated by a tool. -' Runtime Version:4.0.30319.42000 +' Этот код создан программой. +' Исполняемая версия:4.0.30319.42000 ' -' Changes to this file may cause incorrect behavior and will be lost if -' the code is regenerated. +' Изменения в этом файле могут привести к неправильной работе и будут потеряны в случае +' повторной генерации кода. ' '------------------------------------------------------------------------------ @@ -13,42 +13,42 @@ Option Explicit On Namespace My - - _ + + _ Partial Friend NotInheritable Class MySettings Inherits Global.System.Configuration.ApplicationSettingsBase - - Private Shared defaultInstance As MySettings = CType(Global.System.Configuration.ApplicationSettingsBase.Synchronized(New MySettings), MySettings) - -#Region "My.Settings Auto-Save Functionality" + + Private Shared defaultInstance As MySettings = CType(Global.System.Configuration.ApplicationSettingsBase.Synchronized(New MySettings()),MySettings) + +#Region "Функция автоматического сохранения My.Settings" #If _MyType = "WindowsForms" Then - Private Shared addedHandler As Boolean + Private Shared addedHandler As Boolean - Private Shared addedHandlerLockObject As New Object + Private Shared addedHandlerLockObject As New Object - _ - Private Shared Sub AutoSaveSettings(ByVal sender As Global.System.Object, ByVal e As Global.System.EventArgs) - If My.Application.SaveMySettingsOnExit Then - My.Settings.Save() - End If - End Sub + _ + Private Shared Sub AutoSaveSettings(sender As Global.System.Object, e As Global.System.EventArgs) + If My.Application.SaveMySettingsOnExit Then + My.Settings.Save() + End If + End Sub #End If #End Region - + Public Shared ReadOnly Property [Default]() As MySettings Get - + #If _MyType = "WindowsForms" Then - If Not addedHandler Then - SyncLock addedHandlerLockObject - If Not addedHandler Then - AddHandler My.Application.Shutdown, AddressOf AutoSaveSettings - addedHandler = True - End If - End SyncLock - End If + If Not addedHandler Then + SyncLock addedHandlerLockObject + If Not addedHandler Then + AddHandler My.Application.Shutdown, AddressOf AutoSaveSettings + addedHandler = True + End If + End SyncLock + End If #End If Return defaultInstance End Get @@ -57,13 +57,13 @@ Namespace My End Namespace Namespace My - - _ + + _ Friend Module MySettingsProperty - - _ + + _ Friend ReadOnly Property Settings() As Global.LVNC_RS.My.MySettings Get Return Global.LVNC_RS.My.MySettings.Default diff --git a/LVNC-RS/app.config b/LVNC-RS/app.config index 3652831..9851f3b 100644 --- a/LVNC-RS/app.config +++ b/LVNC-RS/app.config @@ -1,4 +1,4 @@ - + @@ -12,14 +12,12 @@ - + - + - + diff --git a/LVNC-RS/frmLVNC-RS-Main.vb b/LVNC-RS/frmLVNC-RS-Main.vb index 98652ac..ec03538 100644 --- a/LVNC-RS/frmLVNC-RS-Main.vb +++ b/LVNC-RS/frmLVNC-RS-Main.vb @@ -1,4 +1,6 @@ -Public Class frmLVNCRSMain +Imports System.ComponentModel + +Public Class frmLVNCRSMain Private Sub frmLVNCRSMain_Load(sender As Object, e As EventArgs) Handles Me.Load txtLVNCSupportID.Text = String.Empty txtLVNCSupportPasswd.Text = String.Empty @@ -16,12 +18,6 @@ Svc.CheckProxy() - 'If My.User.IsInRole(ApplicationServices.BuiltInRole.Administrator) Then - ' tsButtonRunAs.Image = Global.LVNC_RS.My.Resources.is_admin_on - 'Else - ' tsButtonRunAs.Image = Global.LVNC_RS.My.Resources.is_admin_off - 'End If - If Svc.bProxyStatus = 1 Then tsLabelSrvStatus.Image = My.Resources.online txtLVNCSupportID.Text = Format(Convert.ToInt32(Svc.sSupportID), "### ### ##0") @@ -80,6 +76,7 @@ tsLabelSrvStatus.Text = String.Empty txtLVNCSupportID.Text = Format(Convert.ToInt32(Svc.sSupportID), "### ### ##0") txtLVNCSupportPasswd.Text = Svc.sSupportPasswd + Else tsLabelSrvStatus.Image = My.Resources.offline If bProxyOldStatus = 1 Then @@ -89,4 +86,10 @@ End If End If End Sub + + Private Sub frmLVNCRSMain_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing + tsLabelSrvStatus.Text = "Завершение работы..." + + Svc.StopVNC() + End Sub End Class