How to run regsvr32 as Administrator on Windows 7

Posted on 2011-03-10
Medium Priority
Last Modified: 2012-05-11
I have the following line of code in a small vb6 app that works great on XP.  On Windows 7, the regsvr32 will not work unless permissions are elevated to an Administrator.  Is there any way in code to elevate to administrator and then run my code?  Please give specific example.  And I don't want my end users to have to launch the exe any special way other than double-clicking it. I am not opposed to hardcoding an administrator user name and password in the vb6 code.
Here's what works in XP

VBRegSvr32 ("c:\Netmanagerdd\MyFile.dll")
Question by:BradleyCleveland
  • 3
  • 2
  • 2
  • +1

Expert Comment

ID: 35094301
I've not tried this for regsvr32 but I hope it helps.
I use the code for checking remote registry settings.
Good Luck!

Private Declare Function LogonUser Lib "advapi32.dll" Alias _
"LogonUserA" _
(ByVal lpszUsername As String, _
ByVal lpszDomain As String, _
ByVal lpszPassword As String, _
ByVal dwLogonType As Long, _
ByVal dwLogonProvider As Long, _
phToken As Long) As Long

Private Declare Function ImpersonateLoggedOnUser Lib "advapi32.dll" ( _
    ByVal hToken As Long) As Long
Private Declare Function RevertToSelf Lib "advapi32.dll" () As Long

Private Const LOGON32_LOGON_INTERACTIVE As Long = 9
Private Const LOGON32_LOGON_NETWORK As Long = 3
Private Const LOGON32_PROVIDER_DEFAULT As Long = 0
Private Const LOGON32_PROVIDER_WINNT50 As Long = 3
Private Const LOGON32_PROVIDER_WINNT40 As Long = 2
Private Const LOGON32_PROVIDER_WINNT35 As Long = 1

Public Function Logon _
    (ByVal strAdminUser As String, _
     ByVal strAdminPassword As String, _
     ByVal strAdminDomain As String)
     Dim lngTokenHandle, lngLogonType, lngLogonProvider As Long
     Dim blnResult As Boolean
     lngLogonProvider = LOGON32_PROVIDER_DEFAULT
     blnResult = RevertToSelf
     blnResult = LogonUser( _
        strAdminUser, _
        strAdminDomain, _
        strAdminPassword, _
        lngLogonType, _
        lngLogonProvider, _
     Logon = ImpersonateLoggedOnUser(lngTokenHandle)
End Function
Public Sub Logoff()
   Dim blnResult As Boolean
   blnResult = RevertToSelf()
End Sub

Open in new window


Expert Comment

ID: 35094782
Hi again,
Or, you can wrap everything within a autoit script.
Here's an example of an Autoit 3 script.

#include <process.au3>
#include <GUIConstants.au3> ; GUI

; Are we already admin?
If Not IsAdmin() Then
	RunAs('administrator', 'HQdomain', 'secretpassword',0,@AutoItExe,@WorkingDir)

$Width = 300
$Height = 140
GUICreate("Construction Service", $Width, $Height)
$editbox = GUICtrlCreateEdit("Registreraren laddad."& @CRLF,10,10,280,120)

GUISetState ()

GUICtrlSetData ($editbox, "Copy file to computer...",1)
	FileCopy (@ScriptDir & "\FuncList.dll" , @SystemDir)
	FileCopy (@ScriptDir & "\Construction.ini" , @AppDataDir)
If  FileExists (@SystemDir & "\FuncList.dll") Then
	GUICtrlSetData ($editbox, "...done, registering dll-file." & @CRLF,1)
	RunWait('regsvr32 /s  ' & '"' & @SystemDir & '\FuncList.dll"')
	GUICtrlSetData ($editbox, "Registering done." & @CRLF,1)
	$endmsg = "All done. You can now run the application."
	GUICtrlSetData ($editbox, "Error while installing." & @CRLF,1)
	$endmsg = "Error while installing."




Open in new window

LVL 41

Expert Comment

ID: 35098882
Most folks attempt to move those kinds of activities to the Install routine (that obvioulsy happens only once), rather than the application itself.  It's kinda normal to have an administrator perform the install of a new piece of software, so that part is pretty much taken care of.   Now, all you'd need to do is perform a custom step in your installer that registers the DLL.
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions


Author Comment

ID: 35398006
Some other issues have taken priorty and I haven't had a chance to test this solution. Was actually hoping for something simpler, but will try to test it this weekend.  I had to submit this response so that the EE account would unlock
LVL 23

Accepted Solution

OP_Zaharin earned 2000 total points
ID: 35398048
hi bradley,
- you can create a shortcut for your application. right-click and select 'Properties' of the shortcut. under 'Shortcut' tabs, click the 'Advanced' button. you'll find a checkbox that tells it to always 'Run as administrator'.
- so made the shortcut available to your user, not the .exe file.

LVL 23

Expert Comment

ID: 35398131
- maybe you can also look into Manifest to make your exe file run as Administrator:
http://www.bigresource.com/Tracker/Track-vb-bQQLOflX7x/ (scroll down to Jemand solutions)

Author Comment

ID: 35482838
I must have hit something wrong by mistake. My intent was to accept  Op Zaharin's response of creating a shortcut (or right-mouse clicking on the exe) to select the option of "Run as Administrator"
LVL 23

Expert Comment

ID: 35482887
hi bradley, thank you and glad it works for you :)

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question