Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[VB6] How to get an UAC prompt?


erkro1
 Share

Recommended Posts

Hello, I've noticed the updater isn't updating the files when the UAC is active because it gets blocked by it, to resolve this I need to get an UAC prompt up asking the user that the application needs more privileges.

A bit like this:

![](http://origin.arstechnica.com/journals/microsoft.media/uac-password.png)

But I will I acomplish this in VB6, or is there another way of doing it?

Thanks for reading. ![;)](http://www.touchofdeathforums.com/community/public/style_emoticons/<#EMO_DIR#>/wink.png)
Link to comment
Share on other sites

In order to elevate your process during run-time, you have to import "ShellExecuteEx" from shell32.dll and declare a structure that looks exactly like [this](http://msdn.microsoft.com/en-us/library/windows/desktop/bb759784%28v=vs.85%29.aspx). You will then have to set up that structure before calling the function, these are required to set up an elevated process:

* Set lpVerb to "runas".
* Set lpFile to the path of the executable (which can be found by using GetModuleFileName, which can be imported from kernel32.dll).

You can then use this code to create new processes (http://stackoverflow.com/a/4762460):

```
Public Enum ShowConstants

SW_HIDE = 0

SW_NORMAL = 1

SW_SHOWMINIMIZED = 2

SW_SHOWMAXIMIZED = 3

SW_SHOWNOACTIVATE = 4

SW_SHOW = 5

SW_MINIMIZE = 6

SW_SHOWMINNOACTIVE = 7

SW_SHOWNA = 8

SW_RESTORE = 9

SW_SHOWDEFAULT = 10

End Enum

Private Type SHELLEXECUTEINFO

cbSize As Long

fMask As Long

hwnd As Long

lpVerb As String

lpFile As String

lpParameters As String

lpDirectory As String

nShow As Long

hInstApp As Long

lpIDList As Long

lpClass As String

hkeyClass As Long

dwHotKey As Long

hIcon As Long

hProcess As Long

End Type

Private Declare Function ShellExecuteEx Lib "shell32.dll" Alias "ShellExecuteExA" (pExecInfo As

SHELLEXECUTEINFO) As Long

Public Function ExecuteProcess(ByVal FilePath As String, ByVal hwnd As Long,

ByVal nShow As ShowConstants, Optional lpParameters As String = "",

Optional LaunchElevated As Boolean = False)

Dim ExecInfo As SHELLEXECUTEINFO

On Error Goto Hell

With ExecInfo

' Set up the structure.

.cbSize = Len(ExecInfo)

.fMask =

.lpFile = FilePath

.nShow = nShow

.lpDirectory = ExtractDirectoryFromPath(FilePath)

.lpParameters = lpParameters

.hwnd = hwnd

' On Microsoft Windows Vista and later, one can use runas instead of Open, in order to execute the

' process as an elevated process. In that case, the user will be asked whether he or she wants to

' run the process as an administrator.

If LaunchElevated = True Then

.lpVerb = "runas"

Else

.lpVerb = "Open"

End If

End ExecInfo

ExecuteProcess = ShellExecuteEx(ExecInfo)

Exit Function

Hell:

ExecuteProcess = False

End Function

Private Function ExtractDirectoryFromPath(Path As String) As String

On Error Resume Next

Dim Position As Long

' Find the right-most slash.

Position = InStrRev(Path, "\")

' Extract everything on the left.

ExtractDirectoryFromPath = Left$(Path, Position - 1)

End Function
```

Additionally, you will have to check if the running process does have administrator rights, as you won't

have to elevate the process if it does. For this you will have to use AllocateAndInitializeSid, FreeSid and CheckTokenMembership which can all be imported from advapi32.dll (http://stackoverflow.com/a/4762460):

```
Private Const SECURITY_BUILTIN_DOMAIN_RID As Long = &H20

Private Const DOMAIN_ALIAS_RID_ADMINS As Long = &H220

Private Declare Function AllocateAndInitializeSid Lib "advapi32.dll" (pIdentifierAuthority As Any,

ByVal nSubAuthorityCount As Byte, ByVal nSubAuthority0 As Long, ByVal nSubAuthority1 As Long,

ByVal nSubAuthority2 As Long, ByVal nSubAuthority3 As Long, ByVal nSubAuthority4 As Long,

ByVal nSubAuthority5 As Long, ByVal nSubAuthority6 As Long, ByVal nSubAuthority7 As Long,

lpPSid As Long) As Long

Private Declare Sub FreeSid Lib "advapi32.dll" (ByVal pSid As Long)

Private Declare Function CheckTokenMembership Lib "advapi32.dll" (ByVal hToken As Long,

ByVal pSidToCheck As Long, pbIsMember As Long) As Long

Private Type SID_IDENTIFIER_AUTHORITY

Value(0 To 5) As Byte

End Type

Private Function IsAdministrator() As Boolean

Dim NtAuthority As SID_IDENTIFIER_AUTHORITY

Dim pAdministratorGroup As Long

Dim lResult As Long

NtAuthority.Value(5) = 5

If AllocateAndInitializeSid(NtAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_AlIAS_RID_ADMINS, 0, 0,

0, 0, 0, 0, pAdministratorGroup) <> 0 Then

If CheckTokenMembership(0, pAdministratorGroup, lResult) <> 0 Then

IsAdministrator = (lResult <> 0)

Exit Function

End If

Call FreeSid(pAdministratorGroup)

End If

IsAdministrator = False

End Function
```

You can then use this code to elevate your own process:

```
Public Const MAX_PATH = 206

Private Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (

ByVal lpModuleName As String) As Long

Private Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (

ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long

Public Function SelfElevate() As Boolean

Dim FilePath As String

Dim hModule As Long

Dim lResult As Long

FilePath = Space$(MAX_PATH)

hModule = GetModuleHandle(App.EXEName)

lResult = GetModuleFileName(hModule, FilePath, MAX_PATH)

If lResult = 0 Then

SelfElevate = False

Exit Function

End If

If IsAdministrator() Then

SelfElevate = True

Exit Function

End If

SelfElevate = ExecuteProcess(FilePath, 0, SW_NORMAL, "", True)

End Function
```

Yours faithfully,

S.J.R. van Schaik.
Link to comment
Share on other sites

> Oh I'm sorry I didn't saw the difference. xD
>
> EDIT: So it did worked now but is this thesame as running a programm as admin, because when I run it as admin it also does it wrong.

Basically, it should ask the user whether he or she wants to run the programme as an administrator or not, because that's how UAC works.

Yours faithfully,

S.J.R. van Schaik.
Link to comment
Share on other sites

> Basically, it should ask the user whether he or she wants to run the programme as an administrator or not, because that's how UAC works.
>
> Yours faithfully,
>
> S.J.R. van Schaik.

I know, it does ask it then, but it still doesn't correctly update the files then.

I though doing it this way would make it do it correctly, but is that true?
Link to comment
Share on other sites

> I know, it does ask it then, but it still doesn't correctly update the files then.
>
> I though doing it this way would make it do it correctly, but is that true?

You might want to check what kind of errors you are getting when updating the files. As I can't be entirely sure whether the source of the problem is the lack of administrator rights or not. Although, in this specific case, it doesn't seem to be.

Yours faithfully,

S.J.R. van Schaik.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...