IsRemoteAdmin function

K

Kroese, Ramon

Hi,

I'm trying to (re)write a function in VB.NET (VS2008) which determines if
the current user has administrator privileges on a remote computer and
returns a boolean. I want to determine this before calling an external
program. If possible I do not want to enumerate the members of the local
administrators group with nesting to one or more active directory security
groups.

In VB6 I used the returncode of an NetWkstaGetInfo API call to determine the
administrator privileges but I could not get this to work in vb.net.

Declare Function NetWkstaGetInfo Lib "Netapi32" (ByVal servername As
String, ByVal level As Long, ByVal lpBuf As Long) As Long
Declare Function NetApiBufferFree Lib "NETAPI32.DLL" (ByVal Ptr As Long)
As Long

Public Function IsRemoteAdmin(ByVal strComputername As String) As
Boolean
Dim lResult As Long
Dim pWrkInfo As Long
IsRemoteAdmin = False

Dim strServername As String = "\\" & strComputername & vbNullString

lResult = NetWkstaGetInfo(strServername , 102, pWrkInfo)
If lResult = 0 Then
NetApiBufferFree(pWrkInfo)
IsRemoteAdmin = True
ElseIf lResult = 5 Then
IsRemoteAdmin = False
End If
End Function

To determine if the current user has administrative rights on the local
computer I now use WindowsPrincipal and
IsInRole(WindowsBuiltInRole.Administrator).

Public Function IsAdmin() As Boolean
Dim wp As New WindowsPrincipal(WindowsIdentity.GetCurrent())
IsAdmin = wp.IsInRole(WindowsBuiltInRole.Administrator)
End Function

Probably there is an better method in VB.NET to detect if the current user
has administrator privileges on a remote computer but I can not find it. Can
anyone provide me an working function?

Regards,
R. Kroese
 
K

Kroese, Ramon

Hi,

I solved my problem with the following module:

Option Explicit On
Imports System.Security.Principal
'Imports System.Runtime.InteropServices

Module modIsAdminCheck

Public blnIsAdminFailed As Boolean

Private Declare Auto Function NetServerGetInfo Lib "netapi32.dll"
(ByVal ServerName As String, ByVal Level As Integer, ByRef ptrBuff As
IntPtr) As Integer
Private Declare Function NetApiBufferFree Lib "NETAPI32.DLL" (ByVal
Ptr As Long) As Long

'<StructLayout(LayoutKind.Sequential)> _
'Private Structure SERVER_INFO_102
' Dim sv102_platform_id As Integer
' <MarshalAs(UnmanagedType.LPWStr)> Dim sv102_name As String
' Dim sv102_version_major As Integer
' Dim sv102_version_minor As Integer
' Dim sv102_type As Integer
' <MarshalAs(UnmanagedType.LPWStr)> Dim sv102_comment As String
' Dim sv102_users As Integer
' Dim sv102_disc As Integer
' Dim sv102_hidden As Boolean
' Dim sv102_announce As Integer
' Dim sv102_anndelta As Integer
' Dim sv102_licenses As Integer
' <MarshalAs(UnmanagedType.LPWStr)> Dim sv102_userpath As String
'End Structure

Public Function IsAdmin() As Boolean
Dim wp As New WindowsPrincipal(WindowsIdentity.GetCurrent())
IsAdmin = wp.IsInRole(WindowsBuiltInRole.Administrator)

End Function

Public Function IsRemoteAdmin(ByVal strComputername As String) As
Boolean
Dim ptrBuff As IntPtr
'Dim strServerInfo As SERVER_INFO_102
Dim lRetCode As Integer
lRetCode = NetServerGetInfo(strComputername, 102, ptrBuff)
'strServerInfo = CType(Marshal.PtrToStructure(ptrBuff,
GetType(SERVER_INFO_102)), SERVER_INFO_102)
If lRetCode = 0 Then
'Debug.WriteLine(strServerInfo.sv102_version_major)
'Debug.WriteLine(strServerInfo.sv102_version_minor)
NetApiBufferFree(ptrBuff)
IsRemoteAdmin = True
Else
IsRemoteAdmin = False
End If
End Function


End Module


Regards,
R. Kroese
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top