Check if my program is running as admin VB.NET - Takehito Morokuzu - May 4, 2018
Hi, I need to find out if my program is running as admin or not.
I do not want the program to force run as admin.
I don't want to see if the user is admin.
That's all I get when I google the issue.
Certain functions in my program need admin rights but I still want it to run without admin.
Thanks!
RE: Check if my program is running as admin VB.NET - Dalvik - May 4, 2018
You can use this function from the Windows API:
https://msdn.microsoft.com/en-us/library/windows/desktop/bb776463(v=vs.85).aspx
RE: Check if my program is running as admin VB.NET - Takehito Morokuzu - May 4, 2018
(May 4, 2018 12:29 PM)Dalvik Wrote: You can use this function from the Windows API:
https://msdn.microsoft.com/en-us/library/windows/desktop/bb776463(v=vs.85).aspx
1. This is not vb.net
2. I not want to know if the user is admin.
RE: Check if my program is running as admin VB.NET - sh@rp - May 4, 2018
Code Imports Microsoft.Win32
Imports System
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Imports System.Security.Principal
Module UacHelper
Private Const uacRegistryKey As String = "Software\Microsoft\Windows\CurrentVersion\Policies\System"
Private Const uacRegistryValue As String = "EnableLUA"
Private STANDARD_RIGHTS_READ As UInteger = 131072
Private TOKEN_QUERY As UInteger = 8
Private TOKEN_READ As UInteger = (STANDARD_RIGHTS_READ Or TOKEN_QUERY)
<DllImport("advapi32.dll", SetLastError:=True)>
Private Function OpenProcessToken(ByVal ProcessHandle As IntPtr, ByVal DesiredAccess As UInt32, <Out> ByRef TokenHandle As IntPtr) As Boolean
End Function
<DllImport("advapi32.dll", SetLastError:=True)>
Function GetTokenInformation(ByVal TokenHandle As IntPtr, ByVal TokenInformationClass As TOKEN_INFORMATION_CLASS, ByVal TokenInformation As IntPtr, ByVal TokenInformationLength As UInteger, <Out> ByRef ReturnLength As UInteger) As Boolean
End Function
Public Enum TOKEN_INFORMATION_CLASS
TokenUser = 1
TokenGroups
TokenPrivileges
TokenOwner
TokenPrimaryGroup
TokenDefaultDacl
TokenSource
TokenType
TokenImpersonationLevel
TokenStatistics
TokenRestrictedSids
TokenSessionId
TokenGroupsAndPrivileges
TokenSessionReference
TokenSandBoxInert
TokenAuditPolicy
TokenOrigin
TokenElevationType
TokenLinkedToken
TokenElevation
TokenHasRestrictions
TokenAccessInformation
TokenVirtualizationAllowed
TokenVirtualizationEnabled
TokenIntegrityLevel
TokenUIAccess
TokenMandatoryPolicy
TokenLogonSid
MaxTokenInfoClass
End Enum
Public Enum TOKEN_ELEVATION_TYPE
TokenElevationTypeDefault = 1
TokenElevationTypeFull
TokenElevationTypeLimited
End Enum
Public ReadOnly Property IsUacEnabled As Boolean
Get
Dim uacKey As RegistryKey = Registry.LocalMachine.OpenSubKey(uacRegistryKey, False)
Dim result As Boolean = uacKey.GetValue(uacRegistryValue).Equals(1)
Return result
End Get
End Property
Public ReadOnly Property IsProcessElevated(Hnd As IntPtr) As Boolean
Get
If IsUacEnabled Then
Dim tokenHandle As IntPtr
If Not OpenProcessToken(Hnd, TOKEN_READ, tokenHandle) Then
Throw New ApplicationException("Could not get process token. Win32 Error Code: " & Marshal.GetLastWin32Error())
End If
Dim elevationResult As TOKEN_ELEVATION_TYPE = TOKEN_ELEVATION_TYPE.TokenElevationTypeDefault
Dim elevationResultSize As Integer = Marshal.SizeOf(CInt(elevationResult))
Dim returnedSize As UInteger = 0
Dim elevationTypePtr As IntPtr = Marshal.AllocHGlobal(elevationResultSize)
Dim success As Boolean = GetTokenInformation(tokenHandle, TOKEN_INFORMATION_CLASS.TokenElevationType, elevationTypePtr, CUInt(elevationResultSize), returnedSize)
If success Then
elevationResult = CType(Marshal.ReadInt32(elevationTypePtr), TOKEN_ELEVATION_TYPE)
Dim isProcessAdmin As Boolean = elevationResult = TOKEN_ELEVATION_TYPE.TokenElevationTypeFull
Return isProcessAdmin
Else
Throw New ApplicationException("Unable to determine the current elevation.")
End If
Else
Dim identity As WindowsIdentity = WindowsIdentity.GetCurrent()
Dim principal As WindowsPrincipal = New WindowsPrincipal(identity)
Dim result As Boolean = principal.IsInRole(WindowsBuiltInRole.Administrator)
Return result
End If
End Get
End Property
End Module
https://stackoverflow.com/questions/1220213/detect-if-running-as-administrator-with-or-without-elevated-privileges
How to use :
Code Dim Islevated = UacHelper.IsProcessElevated(Process.GetCurrentProcess().Handle)
😉
RE: Check if my program is running as admin VB.NET - SL x TnT - May 4, 2018
(May 4, 2018 01:12 PM)therulerofall Wrote: 1. This is not vb.net
2. I not want to know if the user is admin.
I googled your thread title and the first link had the answer. You are looking to see if your program is running as an administrator and not the user account on windows, right?
RE: Check if my program is running as admin VB.NET - Takehito Morokuzu - May 4, 2018
(May 4, 2018 02:48 PM)SL x TnT Wrote: I googled your thread title and the first link had the answer. You are looking to see if your program is running as an administrator and not the user account on windows, right?
Yesall, the answers give me the user instead or force run as admin.
(May 4, 2018 02:40 PM)sh@rp Wrote: Code Imports Microsoft.Win32
Imports System
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Imports System.Security.Principal
Module UacHelper
Private Const uacRegistryKey As String = "Software\Microsoft\Windows\CurrentVersion\Policies\System"
Private Const uacRegistryValue As String = "EnableLUA"
Private STANDARD_RIGHTS_READ As UInteger = 131072
Private TOKEN_QUERY As UInteger = 8
Private TOKEN_READ As UInteger = (STANDARD_RIGHTS_READ Or TOKEN_QUERY)
<DllImport("advapi32.dll", SetLastError:=True)>
Private Function OpenProcessToken(ByVal ProcessHandle As IntPtr, ByVal DesiredAccess As UInt32, <Out> ByRef TokenHandle As IntPtr) As Boolean
End Function
<DllImport("advapi32.dll", SetLastError:=True)>
Function GetTokenInformation(ByVal TokenHandle As IntPtr, ByVal TokenInformationClass As TOKEN_INFORMATION_CLASS, ByVal TokenInformation As IntPtr, ByVal TokenInformationLength As UInteger, <Out> ByRef ReturnLength As UInteger) As Boolean
End Function
Public Enum TOKEN_INFORMATION_CLASS
TokenUser = 1
TokenGroups
TokenPrivileges
TokenOwner
TokenPrimaryGroup
TokenDefaultDacl
TokenSource
TokenType
TokenImpersonationLevel
TokenStatistics
TokenRestrictedSids
TokenSessionId
TokenGroupsAndPrivileges
TokenSessionReference
TokenSandBoxInert
TokenAuditPolicy
TokenOrigin
TokenElevationType
TokenLinkedToken
TokenElevation
TokenHasRestrictions
TokenAccessInformation
TokenVirtualizationAllowed
TokenVirtualizationEnabled
TokenIntegrityLevel
TokenUIAccess
TokenMandatoryPolicy
TokenLogonSid
MaxTokenInfoClass
End Enum
Public Enum TOKEN_ELEVATION_TYPE
TokenElevationTypeDefault = 1
TokenElevationTypeFull
TokenElevationTypeLimited
End Enum
Public ReadOnly Property IsUacEnabled As Boolean
Get
Dim uacKey As RegistryKey = Registry.LocalMachine.OpenSubKey(uacRegistryKey, False)
Dim result As Boolean = uacKey.GetValue(uacRegistryValue).Equals(1)
Return result
End Get
End Property
Public ReadOnly Property IsProcessElevated(Hnd As IntPtr) As Boolean
Get
If IsUacEnabled Then
Dim tokenHandle As IntPtr
If Not OpenProcessToken(Hnd, TOKEN_READ, tokenHandle) Then
Throw New ApplicationException("Could not get process token. Win32 Error Code: " & Marshal.GetLastWin32Error())
End If
Dim elevationResult As TOKEN_ELEVATION_TYPE = TOKEN_ELEVATION_TYPE.TokenElevationTypeDefault
Dim elevationResultSize As Integer = Marshal.SizeOf(CInt(elevationResult))
Dim returnedSize As UInteger = 0
Dim elevationTypePtr As IntPtr = Marshal.AllocHGlobal(elevationResultSize)
Dim success As Boolean = GetTokenInformation(tokenHandle, TOKEN_INFORMATION_CLASS.TokenElevationType, elevationTypePtr, CUInt(elevationResultSize), returnedSize)
If success Then
elevationResult = CType(Marshal.ReadInt32(elevationTypePtr), TOKEN_ELEVATION_TYPE)
Dim isProcessAdmin As Boolean = elevationResult = TOKEN_ELEVATION_TYPE.TokenElevationTypeFull
Return isProcessAdmin
Else
Throw New ApplicationException("Unable to determine the current elevation.")
End If
Else
Dim identity As WindowsIdentity = WindowsIdentity.GetCurrent()
Dim principal As WindowsPrincipal = New WindowsPrincipal(identity)
Dim result As Boolean = principal.IsInRole(WindowsBuiltInRole.Administrator)
Return result
End If
End Get
End Property
End Module
https://stackoverflow.com/questions/1220213/detect-if-running-as-administrator-with-or-without-elevated-privileges
How to use :
Code Dim Islevated = UacHelper.IsProcessElevated(Process.GetCurrentProcess().Handle)
😉
That's quite long code for what I though would be short, thanks though I'll give a try later 😏
RE: Check if my program is running as admin VB.NET - SL x TnT - May 4, 2018
(May 4, 2018 03:05 PM)therulerofall Wrote: Yesall, the answers give me the user instead or force run as admin.
That's quite long code for what I though would be short, thanks though I'll give a try later 😏
https://stackoverflow.com/questions/43494776/how-to-check-if-application-is-running-as-administrator-vb-net
That will show you process (Read Remarks). Make sure to read more than the first answer. It's the first result of searching your question on Google.
RE: Check if my program is running as admin VB.NET - Dalvik - May 4, 2018
(May 4, 2018 01:12 PM)therulerofall Wrote: 1. This is not vb.net
Its a Windows API function you can call from VB.net.
(May 4, 2018 01:12 PM)therulerofall Wrote: 2. I not want to know if the user is admin.
Well my mistake, but you can still check if the user is even an admin.
RE: Check if my program is running as admin VB.NET - Vyxn - May 5, 2018
Code Import System.Security.Principal
Public Function IsElevated() As Boolean
Return WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator)
End Function
usage:
Code If (IsElevated) Then
'' Your method that requires admin permision.
End If
RE: Check if my program is running as admin VB.NET - He B TeMy - May 5, 2018
(May 5, 2018 01:13 AM)Vyxn Wrote: Code Import System.Security.Principal
Public Function IsElevated() As Boolean
Return WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator)
End Function
usage:
Code If (IsElevated) Then
'' Your method that requires admin permision.
End If
That checks if the user is admin, it doesn't check if the current process has admin rights.
OP, i'm gonna give you c++ code, if you bitch about it idgaf, it works but i won't convert it for you.
Code void IsProcessAdmin() {
//Tested and working
HANDLE processToken;
OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &processToken);
DWORD tokenSize = 0;
GetTokenInformation(processToken, TokenElevation, NULL, tokenSize, &tokenSize);
TOKEN_ELEVATION elevated;
if (GetTokenInformation(processToken, TokenElevation, &elevated, tokenSize, &tokenSize)) {
OutputDebugStringW(L"GetTokenInformation SUCCESS\n");
}
if (elevated.TokenIsElevated) {
OutputDebugStringW(L"Admin\n");
}
else {
OutputDebugStringW(L"Not Admin\n");
}
CloseHandle(processToken);
}
|