将 Visual Studio 的权限提升到 Post-构建事件批处理脚本

Elevate Permissions to Visual Studio For Post-Build Event Batch Script

我有一个批处理文件,可以将一个目录复制到一个新位置,创建 2 个其他 .bat 文件、2 个 .json 文件,以及 插入注册表项 。当 运行 为命令行使用批处理脚本时,我没有收到任何错误和所有想要的结果。 运行从 post-build 事件命令行中启用它时,我没有收到任何错误,但我缺少注册表项。同事建议这可能是由于 Visual Studio 没有管理员权限,而命令行可以。我的问题是:有没有办法 (没有 运行ning Visual Studio 作为管理员) 提升 post-build 事件的权限?如果你想知道为什么不只是 运行 VS 作为管理员,那是因为这个解决方案是通过 TFS 共享的,并不是我所有的同事都会知道 运行 他们的 VS 实例作为这个的管理员特定的解决方案。我已经用谷歌搜索了,但没有任何结果。在此先感谢您的帮助!这是我正在使用的 Post-build 事件命令行。我再次知道它有效……它只是不会破坏注册表值。 CMD 确实如此。 PS。在 Windows 7

上使用 VS 2013
if $(ConfigurationName) == Debug call "$(ProjectDir)BatchFiles\DebugHelper.bat" "$(TargetDir)" C:\CEC\Batch\Test\

本文来自Windows关于重定向的SDK(也有反映和分享)

Registry Redirector

The registry redirector isolates 32-bit and 64-bit applications by providing separate logical views of key portions of the registry on WOW64. The registry redirector intercepts 32-bit registry calls to each logical registry view and maps them to the corresponding physical registry location. The redirection process is transparent to the application. Therefore, a 32-bit application can access registry data as if it were running on 32-bit Windows even if the data is stored in a different location on 64-bit Windows.

Redirection is enabled for the following registry keys:

HKEY_LOCAL_MACHINE\Software 
HKEY_USERS\*\Software\Classes 
HKEY_USERS\*_Classes 
Note  * indicates a match for all user security IDs (SID).

The following scenario illustrates the use of these logical views:

A 32-bit application checks for the existance of the following registry key: HKEY_LOCAL_MACHINE\Software\Hello. If the key does not exist, it creates it with a default value of "Hello 32-bit world"; otherwise, it reads and displays the value. The same application is modified to write "Hello 64-bit world" instead of "Hello 32-bit world" and recompiled as a 64-bit application.

When the 32-bit application is run on 64-bit Windows, it displays "Hello 32-bit world". When the 64-bit application is run, it displays "Hello 64-bit world". Both applications call the same registry functions with the same predefined handle and the same key name; the difference is that each application operates on its logical view of registry, and each view is mapped to a separate physical location of the registry, which keeps both versions of the string intact.

To help applications that write REG_EXPAND_SZ keys containing %ProgramFiles% to the registry, WOW64 intercepts these writes and replaces them with "%ProgramFiles(x86)%". This environment variable is defined for all processes. For example, if the Program Files directory is on the C drive, then "%ProgramFiles(x86)%" expands to "C:\Program Files (x86)".

To enable application interoperability through COM and other mechanisms, WOW64 uses registry reflection, which copies specific registry keys and values between the two registry views to keep them in synch. The reflector is intelligent and copies COM activation data for Local servers between the views, but not in-process data, because 32/64 in-process data mixing is not permitted on 64-bit Windows.

做什么

在节目中

KEY_WOW64_64KEY 0x0100 Access a 64-bit key from either a 32-bit or 64-bit application.
Windows 2000:  This flag is not supported. 

KEY_WOW64_32KEY 0x0200 Access a 32-bit key from either a 32-bit or 64-bit application.
Windows 2000:  This flag is not supported. 

在命令提示符下看到 reg flags /? 允许您的密钥选择退出重定向。

提醒一下,UAC 也可能正在对其进行虚拟化。因为它将一些写入 HKLM 的内容重定向到 HKCU。允许用户写入您的密钥将解决这个问题。