UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Run As Administrator?, Access 2016    
 
   
ADezii
post May 23 2020, 09:02 AM
Post#1



Posts: 3,089
Joined: 4-February 07
From: USA, Florida, Delray Beach


  1. I apologize if I am in the wrong Forum but this has been my home for years and I am very comfortable with the expertise and knowledge of all those involved. The problem also involves VBA which I assume would make it somewhat appropriate. What I would like to know is if it is possible to pro-grammatically set an Executable File to 'Run as Administrator'? I need the following Code Execution to be seamless and transparent. I can easily accomplish this manually, but that is not a viable option for me. The Code segment below should execute Dism.exe (Deployment Image Servicing and Management Tool) which will output an *.xml File listing all the Default Programs for File Extensions.
  2. Code Definition:
    CODE
    Dim strWinDir As String
    Dim strPathToDism As String
    Dim varRetVal As Variant
    Dim strTempFldr As String

    strWinDir = Environ("windir")
    strTempFldr = Environ("TEMP")

    'Dism.exe resides in System32 Folder of Windows
    strPathToDism = strWinDir & "\System32\Dism.exe"

    varRetVal = Shell(strPathToDism & " /online /Export-DefaultAppAssociations:" & strTempFldr & _
                      "\AppAssoc.xml", vbMaximizedFocus)

    Debug.Print varRetVal
  3. The Code will execute and a valid TaskID will be assigned to varRetVal which normally indicates success for the Shell() Command, but the actual File is never created.
  4. The problem appears to be the following:
    QUOTE
    Elevated permissions are required to run DISM.
    Use an elevated command prompt to complete these tasks.
  5. I'm assuming that if I can set Dism.exe to 'Run as Administrator', the Code will run properly, but I'm not 100% sure.
  6. Although irrelevant, here is some partial Output of the Dism.exe Command solely for reference.
    CODE
    <?xml version="1.0" encoding="UTF-8"?>
    <DefaultAssociations>
      <Association Identifier=".avi" ProgId="AppX6eg8h5sxqq90pv53845wmnbewywdqq5h" ApplicationName="Movies &amp; TV" />
      <Association Identifier=".bas" ProgId="Applications\notepad.exe" ApplicationName="Notepad" />
      <Association Identifier=".bmp" ProgId="AppX43hnxtbyyps62jhe9sqpdzxn1790zetc" ApplicationName="Photos" />
      <Association Identifier=".cda" ProgId="WMP11.AssocFile.CDA" ApplicationName="Windows Media Player" />
      <Association Identifier=".cr2" ProgId="AppX43hnxtbyyps62jhe9sqpdzxn1790zetc" ApplicationName="Photos" />
      <Association Identifier=".dsn" ProgId="Applications\notepad.exe" ApplicationName="Notepad" />
      <Association Identifier=".erf" ProgId="AppX43hnxtbyyps62jhe9sqpdzxn1790zetc" ApplicationName="Photos" />
      <Association Identifier=".fdf" ProgId="AcroExch.FDFDoc" ApplicationName="Adobe Acrobat Reader DC" />
    </DefaultAssociations>
  7. Thanks in advance for any assistance that you may provide.
Go to the top of the page
 
theDBguy
post May 23 2020, 09:18 AM
Post#2


UA Moderator
Posts: 78,487
Joined: 19-June 07
From: SunnySandyEggo


Hi. Just a thought, but have you tried using RunAs instead of Shell? Or maybe use it in conjunction with Shell.

--------------------
Just my 2 cents... "And if I claim to be a wise man, it surely means that I don't know" - Kansas
Access Website | Access Blog | Email
Go to the top of the page
 
AlbertKallal
post May 23 2020, 11:42 AM
Post#3


UtterAccess VIP
Posts: 3,102
Joined: 12-April 07
From: Edmonton, Alberta Canada


Well, one way?

If you have a shortcut to access, you can right click and choose run as administrator. This will in turn cause any shell() or just about anything else you run to also have admin rights. So, if for example you launch the ODBC connection wizards (linked table manager), then the ODBC panels etc. ALSO will run as admin.

So, the above trick would mean you don't have to modify your shell commands.

But, yes, there is a way to launch the Shell() command with elevated rights.

This:
CODE
   ShellExecute 0, "runas", "cmd", strP, CurrentProject.path, 0


Note the addition of "runas". This is what will get you "admin" rights, and I even believe that if YAC prompts are turned on (windows intercepts and ask do you want to do this will occur).

So, you can add "runas" to get admin, but another way is to right click on Access shortcut, and choose run as admin,, and that will cause everything to be admin, and this includes shell() outs, or anything else that you launch from Access.

Regards,
Albert D. Kallal
Edmonton, Alberta Canada
Go to the top of the page
 
ADezii
post May 23 2020, 12:02 PM
Post#4



Posts: 3,089
Joined: 4-February 07
From: USA, Florida, Delray Beach


Thanks for the pointer theDBGuy, but I have tried every switch that there is with RunAs.exe, and the closest that I can get is to be prompted for an Administrator password (no good). Going back to the drawing board.
Go to the top of the page
 
ADezii
post May 23 2020, 12:06 PM
Post#5



Posts: 3,089
Joined: 4-February 07
From: USA, Florida, Delray Beach


Thanks Albert, I will try your suggestions. I can get it to work with the ways that you describe, but again this process will be on End User PCs and must be transparent. Thanks again.
Go to the top of the page
 
AlbertKallal
post May 23 2020, 12:10 PM
Post#6


UtterAccess VIP
Posts: 3,102
Joined: 12-April 07
From: Edmonton, Alberta Canada


the "runas" we are referring to only works if you are using the ShellApi() as per my example.

This one:

http://access.mvps.org/access/api/api0018.htm

There might be some other way - but I was under the impression that "runas" only works with above.

As noted, if right clicking and choosing run-as administrator fires up a prompt/request for a logon, then above will not work.

However, quite such if you "can" and are "able" to use run as administrator, then above will work. But, as noted, I do believe that the UAC prompt will trigger.

I mean, if you can get 100% by-pass the UAC prompt, then the UAC prompts are of zero use!!

R
Albert
Go to the top of the page
 
ADezii
post May 23 2020, 01:31 PM
Post#7



Posts: 3,089
Joined: 4-February 07
From: USA, Florida, Delray Beach


  1. Thanks to Albert and theDBguy, I almost have the Code executing perfectly. The Command Prompt becomes 'elevated', but the old UAC Dialog kicks in as illustrated below. Once I confirm and select Yes, the File is generated as it should be.
  2. Code Definition:
    CODE
    Dim lngErr As Long
    Const conMAXIMIZED = 3


    lngErr = ShellExecute(0, "runas", "cmd", "/c Dism.exe /online /Export-DefaultAppAssociations:" & _
                          CurrentProject.Path & "\DefaultAppsAssoc.xml", "", conMAXIMIZED)
  3. I know that I am asking a lot, but do you have one more trick up your sleeve to eliminate the UAC Prompt? Manually disabling UAC is not an Option. Thanks in advance to both you and theDBguy.

This post has been edited by ADezii: May 23 2020, 01:32 PM
Attached File(s)
Attached File  cmd.jpg ( 5.19K )Number of downloads: 0
 
Go to the top of the page
 
AlbertKallal
post May 23 2020, 01:45 PM
Post#8


UtterAccess VIP
Posts: 3,102
Joined: 12-April 07
From: Edmonton, Alberta Canada


Turn it off?? ;-)

No, you can't - if you could then as noted UAC would not really be of any use.

The only suggest? Well, you run Access as admin, and then once you answer "yes" to UAC, then Access, and any shells() it does will not prompt you (at least I don't think it does). So launching Access as admin would at least limit the prompt to a one time nag.


So, no way out of this.

R
Albert
Go to the top of the page
 
ADezii
post May 23 2020, 02:34 PM
Post#9



Posts: 3,089
Joined: 4-February 07
From: USA, Florida, Delray Beach


Thanks Albert. I am actually doing this Project for someone and they want absolutely no User Intervention whatsoever. My only other option, a rather drastic one, is to Disable UAC, run the previous Code that I posted, then re-enable it via the System Registry pro-grammatically. Not something I really want to do, but it is now up to the Client. Thanks again for all your help.
Go to the top of the page
 
jleach
post May 23 2020, 03:58 PM
Post#10


UtterAccess Administrator
Posts: 10,587
Joined: 7-December 09
From: St. Augustine, FL


Not sure if it's been mentioned yet, but if you can start Access itself in elevated permission, I think you should be able to forego the prompt upon running that script (you should be able to have it inherit the parent process security level). You would still have the UAC prompt located at the start of the main application (you can use some launcher or script to fire off the process in elevated mode), but it may be a little more user friendly to get it once at the start of the application, and not later, mid-workflow.

However, as usual user-friendliness and security are at odds with each other: security best practices would say don't allow the Access app to run elevated if it's not necessary: maintain the security boundaries as narrowly as possible (only to that shell cmd) to prevent potential malicious use of the elevated permission of Access (perhaps via automation hijacking...).

I'm curious what this external tool does that requires this security context...

--------------------
Jack D. Leach
Founder & CEO
Dymeng Services Inc.
Business Software Solutions
Go to the top of the page
 
ADezii
post May 23 2020, 05:07 PM
Post#11



Posts: 3,089
Joined: 4-February 07
From: USA, Florida, Delray Beach


@jleach:
Your approach is probably the best one. If you Run Access as Administrator, open the Database and execute the Code below, it works flawlessly. No UAC prompt is encountered and somehow the Dism.exe executable inherits this elevted state. Doesn't make sense to me bit it does work. I completely abandoned the idea of Disabling UAC, executing the Code, then re-enabling UAC especially since it would require Reboots both before and after Code Execution. Thanks to all.
CODE
Dim lngErr As Long
Const conMAXIMIZED = 3


lngErr = ShellExecute(0, "runas", "cmd", "/c Dism.exe /online /Export-DefaultAppAssociations:" & _
                      CurrentProject.Path & "\DefaultAppsAssoc.xml", "", conMAXIMIZED)

Go to the top of the page
 
ADezii
post May 24 2020, 07:05 AM
Post#12



Posts: 3,089
Joined: 4-February 07
From: USA, Florida, Delray Beach


Thanks to Albert who originally put me on this Path.
Go to the top of the page
 


Custom Search


RSSSearch   Top   Lo-Fi    12th July 2020 - 05:39 PM