Pregunta

Necesito obtener credenciales elevadas (para iniciar un servicio) en una aplicación de Visual Basic 6, pero sólo si el usuario tiene que reiniciar el servicio (es decir que no quiero para obtener credenciales elevadas cuando se inicia la aplicación, sólo cuando el usuario selecciona reinicio). ¿Cómo puedo hacer esto en Visual Basic 6?

¿Fue útil?

Solución

bastante fácil, pero la forma preferida implica un nuevo proceso elevada. En este ejemplo se utiliza en sí funcionan con un interruptor saber para realizar el servicio de inicio en lugar de las operaciones normales:

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

Otros consejos

Una solución es utilizar la elevación moniker COM http :. //msdn.microsoft.com/en-us/library/ms679687 (VS.85) .aspx

Este enlace debe ser útil si su objetivo es VB6 http: //www.vbforums .com / showthread.php? t = 459643 .

Tendrá que llamar a la API de Windows -. CoImpersonateClient o LogonUser
Sólo recuerde que debe reducir sus privilegios después, y ser ZURZA cuidado con lo que haces cuando están elevadas (por ejemplo, no se hace nada con la entrada del usuario).

Otra opción, que creo que es preferible (si está disponible), es utilizar un objeto COM + configurado. Puede hacer que el COM + subsistema de gestionar credenciales, y justo limitar el acceso para llamar al objeto como sea necesario. Esto tiene la ventaja de crear y de límite de confianza real entre pocos privilegios de código y código de alta privilegiada.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top