我需要在 VB6 应用程序中获取提升的凭据(启动服务),但前提是用户需要重新启动服务(即我不想在应用程序启动时获取提升的凭据,只有当用户选择重新启动时)。我怎样才能在 VB6 中做到这一点?
Kris Erickson
问问题
1710 次
3 回答
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
于 2009-02-05T22:05:53.817 回答
1
一种解决方案是使用 COM 提升名字对象http://msdn.microsoft.com/en-us/library/ms679687(VS.85).aspx。
如果您的目标是 VB6 http://www.vbforums.com/showthread.php?t=459643 ,这个链接应该很有用。
于 2009-02-05T21:59:05.920 回答
0
您需要调用 WinAPI - CoImpersonateClient 或 LogonUser。
请记住在之后降低您的权限,并在提升时小心您所做的事情(例如,不要对用户输入做任何事情)。
另一个我认为更可取的选项(如果可用)是使用 COM+ 配置对象。您可以让 COM+ 子系统管理凭据,并根据需要限制调用对象的访问权限。这具有在低特权代码和高特权代码之间创建和实际信任边界的好处。
于 2009-02-05T22:03:59.750 回答