-1

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. 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. Plus- I did some simple google searching and couldn't find anything that wasn't in C#.

Most of my errors have to do with the dll importing and the private functions following those.

Thanks you guys in advanced!

^^^EDITTTTT 7:06 PM So I applied "RobertBaron"'s code and this is the error that I receive? Was unable to find any threads related to this error.

Error relating to edittt 7:06 PM

^^^EDITTTTT 7:15 PM

Sorry, I was able to find the answer to my edit. I found the answer here: "https://learn.microsoft.com/en-us/dotnet/visual-basic/misc/bc31529" and the functions now look like this.

 <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
Kory Haas
  • 1
  • 2
  • You know there are automatic online code converters which can help you go from c# to VB with less problems? – ADyson Jul 28 '19 at 15:48
  • 1
    Anyway if you've got errors and you expect us to help you with them, it would make sense to tell us what they are, then we don't have to guess... – ADyson Jul 28 '19 at 15:49
  • In most cases you just want to just require admin privileges and to force users to grant you them you can use https://stackoverflow.com/questions/2818179/how-do-i-force-my-net-application-to-run-as-administrator – Margus Jul 28 '19 at 19:06
  • RobertBaron was able to provide the revised code. I did do the automatic code converters but they failed. I used RobertBaron's revised code and it was all good except for a minor error. Would you guys mind to point me in the right direction? – Kory Haas Jul 28 '19 at 23:10
  • Sorry, I found the answer here "https://learn.microsoft.com/en-us/dotnet/visual-basic/misc/bc31529" – Kory Haas Jul 28 '19 at 23:14

1 Answers1

0

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
RobertBaron
  • 2,817
  • 1
  • 12
  • 19
  • I applied your revised code. And I was left with one minor error that I was unable to fix. I imported a picture of the error into my post above ^^^ – Kory Haas Jul 28 '19 at 23:09
  • @Kory Haas - You have to declared the `DllImport` functions as `Shared`. In my example, they were in a `Module` and so implicitly declared as `Shared`. – RobertBaron Jul 28 '19 at 23:15
  • @Kory Haas - If this answers your question, do not forget to accept it! – RobertBaron Jul 29 '19 at 15:57