[英]Detect if another process is started as “Run as Administrator” for Visual Basic
I am asking the same question as this post " Detect if another process is started as “Run as Administrator” "我在问与这篇文章相同的问题“ 检测另一个进程是否以“以管理员身份运行”启动”
I tried converting the code to Visual Basic myself, but I am getting a lot of errors.我自己尝试将代码转换为 Visual Basic,但出现了很多错误。 As far as code, this is what I have so far:
就代码而言,这是我迄今为止所拥有的:
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Runtime.InteropServices
Imports System.Diagnostics
Imports System.Security.Principal
Imports System.Reflection
Namespace WindowsFormsApplication2
Public Class ProcessHelper
<DllImport("advapi32.dll", SetLastError:=True)>
Private Shared Function OpenProcessToken(ByVal ProcessHandle As IntPtr, ByVal DesiredAccess As UInt32, <Out> ByRef TokenHandle As IntPtr) As Boolean
<DllImport("kernel32.dll", SetLastError:=True)>
Private Shared Function CloseHandle(ByVal hObject As IntPtr) As Boolean
Private Const STANDARD_RIGHTS_REQUIRED As Integer = &HF0000
Private Const TOKEN_ASSIGN_PRIMARY As Integer = &H1
Private Const TOKEN_DUPLICATE As Integer = &H2
Private Const TOKEN_IMPERSONATE As Integer = &H4
Private Const TOKEN_QUERY As Integer = &H8
Private Const TOKEN_QUERY_SOURCE As Integer = &H10
Private Const TOKEN_ADJUST_GROUPS As Integer = &H40
Private Const TOKEN_ADJUST_PRIVILEGES As Integer = &H20
Private Const TOKEN_ADJUST_SESSIONID As Integer = &H100
Private Const TOKEN_ADJUST_DEFAULT As Integer = &H80
Private Const TOKEN_ALL_ACCESS As Integer = (STANDARD_RIGHTS_REQUIRED Or TOKEN_ASSIGN_PRIMARY Or TOKEN_DUPLICATE Or TOKEN_IMPERSONATE Or TOKEN_QUERY Or TOKEN_QUERY_SOURCE Or TOKEN_ADJUST_PRIVILEGES Or TOKEN_ADJUST_GROUPS Or TOKEN_ADJUST_SESSIONID Or TOKEN_ADJUST_DEFAULT)
Public Shared Function IsProcessOwnerAdmin(ByVal processName As String) As Boolean
Dim proc As Process = Process.GetProcessesByName(processName)(0)
Dim ph As IntPtr = IntPtr.Zero
OpenProcessToken(proc.Handle, TOKEN_ALL_ACCESS, ph)
Dim iden As WindowsIdentity = New WindowsIdentity(ph)
Dim result As Boolean = False
For Each role As IdentityReference In iden.Groups
If role.IsValidTargetType(GetType(SecurityIdentifier)) Then
Dim sid As SecurityIdentifier = TryCast(role, SecurityIdentifier)
If sid.IsWellKnown(WellKnownSidType.AccountAdministratorSid) OrElse sid.IsWellKnown(WellKnownSidType.BuiltinAdministratorsSid) Then
result = True
Exit For
End If
End If
Next
CloseHandle(ph)
Return result
End Function
End Class
Module Program
<STAThread>
Private Sub Main()
Dim isAdmin As Boolean = ProcessHelper.IsProcessOwnerAdmin("outlook")
End Sub
End Module
End Namespace
Any idea on what I may be doing wrong here?知道我在这里可能做错了什么吗? I am trying to check to see if other processes are administrative level or not.
我正在尝试检查其他进程是否为管理级别。 I did research to see if there were any other vb.net threads for this on here.
我做了研究,看看这里是否还有其他 vb.net 线程。 Plus- I did some simple google searching and couldn't find anything that wasn't in C#.
另外 - 我做了一些简单的谷歌搜索,找不到任何不在 C# 中的东西。
Most of my errors have to do with the dll importing and the private functions following those.我的大部分错误都与 dll 导入和紧随其后的私有函数有关。
Thanks you guys in advanced!谢谢各位前辈!
^^^EDITTTTT 7:06 PM So I applied "RobertBaron"'s code and this is the error that I receive? ^^^EDITTTTT 7:06 PM所以我应用了“RobertBaron”的代码,这是我收到的错误? Was unable to find any threads related to this error.
无法找到与此错误相关的任何线程。
Error relating to edittt 7:06 PM与 edittt 相关的错误 7:06 PM
^^^EDITTTTT 7:15 PM ^^^编辑 7:15 PM
Sorry, I was able to find the answer to my edit.抱歉,我能够找到我的编辑的答案。 I found the answer here: " https://docs.microsoft.com/en-us/dotnet/visual-basic/misc/bc31529 " and the functions now look like this.
我在这里找到了答案:“ https://docs.microsoft.com/en-us/dotnet/visual-basic/misc/bc31529 ”,现在功能看起来像这样。
<DllImport("advapi32.dll", SetLastError:=True)>
Private Shared Function OpenProcessToken(ByVal ProcessHandle As IntPtr, ByVal DesiredAccess As Integer, ByRef TokenHandle As IntPtr) As Boolean
End Function
<DllImport("kernel32.dll", SetLastError:=True)>
Public Shared Function CloseHandle(ByVal hObject As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
Here is the converted function.这是转换后的函数。
Imports System.Runtime.InteropServices
Imports System.Security.Principal
Module Module1
Sub Main(args As String())
Dim result As Boolean
result = IsProcessOwnerAdmin("calc")
End Sub
<DllImport("advapi32.dll", SetLastError:=True)>
Private Function OpenProcessToken(ByVal ProcessHandle As IntPtr, ByVal DesiredAccess As Integer, ByRef TokenHandle As IntPtr) As Boolean
End Function
<DllImport("kernel32.dll", SetLastError:=True)>
Public Function CloseHandle(ByVal hObject As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
Private Const STANDARD_RIGHTS_REQUIRED As Integer = 983040
Private Const TOKEN_ASSIGN_PRIMARY As Integer = 1
Private Const TOKEN_DUPLICATE As Integer = 2
Private Const TOKEN_IMPERSONATE As Integer = 4
Private Const TOKEN_QUERY As Integer = 8
Private Const TOKEN_QUERY_SOURCE As Integer = 16
Private Const TOKEN_ADJUST_GROUPS As Integer = 64
Private Const TOKEN_ADJUST_PRIVILEGES As Integer = 32
Private Const TOKEN_ADJUST_SESSIONID As Integer = 256
Private Const TOKEN_ADJUST_DEFAULT As Integer = 128
Private Const TOKEN_ALL_ACCESS As Integer = (STANDARD_RIGHTS_REQUIRED _
Or (TOKEN_ASSIGN_PRIMARY _
Or (TOKEN_DUPLICATE _
Or (TOKEN_IMPERSONATE _
Or (TOKEN_QUERY _
Or (TOKEN_QUERY_SOURCE _
Or (TOKEN_ADJUST_PRIVILEGES _
Or (TOKEN_ADJUST_GROUPS _
Or (TOKEN_ADJUST_SESSIONID Or TOKEN_ADJUST_DEFAULT)))))))))
Public Function IsProcessOwnerAdmin(ByVal processName As String) As Boolean
Dim proc As Process = Process.GetProcessesByName(processName)(0)
Dim ph As IntPtr = IntPtr.Zero
OpenProcessToken(proc.Handle, TOKEN_ALL_ACCESS, ph)
Dim iden As WindowsIdentity = New WindowsIdentity(ph)
Dim result As Boolean = False
For Each role As IdentityReference In iden.Groups
If role.IsValidTargetType(GetType(SecurityIdentifier)) Then
Dim sid As SecurityIdentifier = CType(role, SecurityIdentifier)
If (sid.IsWellKnown(WellKnownSidType.AccountAdministratorSid) OrElse sid.IsWellKnown(WellKnownSidType.BuiltinAdministratorsSid)) Then
result = True
Exit For
End If
End If
Next
CloseHandle(ph)
Return result
End Function
End Module
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.