我需要在VB6应用程序中获得提升的凭据(启动服务),但前提是用户需要重新启动服务(即,我不想在应用程序启动时获取提升的凭据,仅在用户选择重启)。我怎么能在VB6中做到这一点?
答案 0 :(得分:3)
相当简单,但首选方法涉及新的升级过程。此示例使用自身运行的开关来了解执行服务启动而不是正常操作:
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
答案 1 :(得分:1)
一种解决方案是使用COM高程名字对象http://msdn.microsoft.com/en-us/library/ms679687(VS.85).aspx。
如果您的目标是VB6 http://www.vbforums.com/showthread.php?t=459643。
,此链接应该非常有用答案 2 :(得分:0)
您需要调用WinAPI - CoImpersonateClient或LogonUser。 请记住之后降低您的权限,并且要小心提升时所做的事情(例如,不要对用户输入做任何事情)。
我认为另一种选择(如果可用)是使用COM +配置的对象。您可以让COM +子系统管理凭据,并根据需要限制访问以调用对象。这样做的好处是可以在低特权代码和高特权代码之间创建和ACTUAL信任边界。