Increased Authority for VB6

I need to get elevated credentials (to start the service) in a VB6 application, but only if the user needs to restart the service (i.e. I don’t want to get elevated credentials every time the application starts, only when the user chooses to restart). How to do it in VB6?

+1
source share
3 answers

Quite easily, but the preferred method is associated with a new elevated process. In this example, he himself uses run with the key to know in order to execute the Service Start instead of the usual operations:

VERSION 5.00
Begin VB.Form Form1 
   BorderStyle     =   1  'Fixed Single
   Caption         =   "Form1"
   ClientHeight    =   3060
   ClientLeft      =   45
   ClientTop       =   345
   ClientWidth     =   4560
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   3060
   ScaleWidth      =   4560
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton Command1 
      Caption         =   "Start Service"
      Height          =   495
      Left            =   1448
      TabIndex        =   0
      Top             =   1283
      Width           =   1665
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private Const BCM_SETSHIELD As Long = &H160C&

Private Declare Function SendMessage Lib "user32" _
    Alias "SendMessageA" ( _
    ByVal hWnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    lParam As Any) As Long

Private Declare Function ShellExecute Lib "shell32.dll" _
    Alias "ShellExecuteA" ( _
    ByVal hWnd As Long, _
    ByVal lpOperation As String, _
    ByVal lpFile As String, _
    ByVal lpParameters As String, _
    ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) As Long

Private Sub Command1_Click()
    ShellExecute hWnd, "runas", App.EXEName & ".exe", "-start", CurDir$(), vbNormalFocus
End Sub

Private Sub Form_Load()
    If UCase$(Trim$(Command$())) = "-START" Then
        Caption = "Starting Service"
        Command1.Visible = False
        'Service starting functionality goes here.
    Else
        Caption = "Service Starter"
        'For Shield to work you must have a Common Controls v. 6
        'manifest and call InitCommonControls before loading
        'this form (i.e. preferably from Sub Main).
        SendMessage Command1.hWnd, BCM_SETSHIELD, 0&, 1&
        Command1.Visible = True
    End If
End Sub
+3
source

WinAPI - CoImpersonateClient LogonUser.
, , (, ).

Another option, which in my opinion is preferred (if available), is to use a COM + configured object. You can have a COM + subsystem for credential management and simply restrict access to call an object as needed. This has the advantage of creating and ACTUAL a boundary of trust between low-priority code and highly-privileged code.

0
source

Source: https://habr.com/ru/post/1703449/


All Articles