How to set “Run this program as an administrator” programmatically

前端 未结 7 1299
梦谈多话
梦谈多话 2020-11-29 21:59

I\'m having a problem with good ol\' bdeadmin.exe in Vista. First, let\'s get the predictable responses out of the way:

\"You should not require your application to

相关标签:
7条回答
  • 2020-11-29 22:13

    Use a wrap program which uses ShellExcute that uses "runas" as its "verb" to run the program you want.

    0 讨论(0)
  • 2020-11-29 22:16

    Pack your app into WinRar SFX with silent mode + admin request mode.

    Much simpler than messing with .MSI variables.

    0 讨论(0)
  • 2020-11-29 22:18

    This is a long shot, but if you have the word "setup" or "install" in the name of the EXE, Windows will prompt for elevation when running it. I don't know if that'll work with a control panel applet, though.

    0 讨论(0)
  • 2020-11-29 22:20

    I'd be surprised if this was possible. It would be an ideal way for malicious code to abuse the system. You're probably going to have tell the user the administrator must install or they must have admin rights (like all the other programs on windows do).

    0 讨论(0)
  • 2020-11-29 22:25

    You can programmatically set the "Run this program as an administrator" flag (the option you find in the Compatibility tab of an EXE's properties), by setting a simple registry key. You need to create a string value (REG_SZ) under one of these keys (if you want the setting to be per user or per machine, respectively):

    HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers

    or

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers

    The name of the value needs to be the full path to your executable (if the path contains spaces, do not surround the path with quotes) and the data of the value must contain the string RUNASADMIN.

    For sample:

    reg.exe Add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v "C:\Program Files\MyApp\Test.exe" /d "PUT__VALUE__HERE"
    

    Compatibility Modes

    WIN95 Windows 95
    WIN98 Windows 98
    WIN4SP5 Windows NT 4.0 SP5
    WIN2000 Windows 2000
    WINXPSP2 Windows XP SP2
    WINXPSP3 Windows XP SP3
    VISTARTM Vista
    VISTASP1 Vista SP1
    VISTASP2 Vista SP2
    WIN7RTM Windows 7
    WINSRV03SP1 Windows Server 2003 SP1
    WINSRV08SP1 Windows Server 2008 SP1

    Privilege Level

    RUNASADMIN Run program as an administrator

    REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v "C:\temp\compatmodel\iconsext.exe" /t REG_SZ /d "WINXPSP3 RUNASADMIN" /f

    References: http://www.verboon.info/2011/03/running-an-application-as-administrator-or-in-compatibility-mode/

    0 讨论(0)
  • 2020-11-29 22:25

    Have you tried Microsoft's Application Compatibility Toolkit? It analyses your app and provides compatibility shims that might be able to help resolve your problem.

    0 讨论(0)
提交回复
热议问题