如何让VB.NET程序以自身权限启动uwfmgr.exe读取UWF配置?
问题
我有一台运行Windows IoT Enterprise的电脑,想要通过VB.NET应用检查位于%Windir%\system32的UWF(Unified Write Filter)。我使用System.Diagnostics.Process调用uwfmgr.exe,能启动但遇到问题:uwfmgr.exe输出“Access denied”。奇怪的是,双击uwfmgr.exe时它能正常运行,而双击仅负责启动该进程的编译后VB.NET应用时,却出现权限拒绝提示。
补充说明:uwfmgr.exe是用于控制统一写入筛选器(UWF)的小型命令行工具,修改设置需要管理员权限,但我仅读取信息,无需提升权限。
请问如何启动进程并使其权限与调用应用一致?
相关代码片段:
Dim psi As New ProcessStartInfo() With { .FileName = fi.FullName, .Arguments = "FILTER GET-CONFIG", .UseShellExecute = False, ' Required for redirection .RedirectStandardOutput = True, .RedirectStandardError = True, .RedirectStandardInput = False, .CreateNoWindow = True, .StandardOutputEncoding = Encoding.UTF8, .StandardErrorEncoding = Encoding.UTF8 } Using process As New Process() With {.StartInfo = psi, .EnableRaisingEvents = True} If Not process.Start() Then Return eUwfStates.UWF_PROCESS_COULD_NOT_START process.BeginOutputReadLine() process.WaitForExit() ' other code for reading the outputstream End Using
解决方案
解决文件系统重定向问题:Windows IoT Enterprise中32位应用会自动被系统重定向到
SysWOW64目录,而uwfmgr.exe是64位工具,直接调用system32路径会被指向不存在的位置,触发权限错误。可以通过两种方式解决:- 将VB.NET项目的平台目标设置为x64(项目属性→生成→平台目标选择x64),编译为64位应用后直接调用即可;
- 在调用
uwfmgr.exe前临时禁用文件系统重定向,代码示例:Imports System.Runtime.InteropServices ' 禁用Wow64文件系统重定向 <DllImport("kernel32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> Private Shared Function Wow64DisableWow64FsRedirect(ByRef oldState As IntPtr) As Boolean End Function ' 恢复Wow64文件系统重定向 <DllImport("kernel32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> Private Shared Function Wow64RevertWow64FsRedirect(oldState As IntPtr) As Boolean End Function ' 使用示例 Dim redirectState As IntPtr Wow64DisableWow64FsRedirect(redirectState) ' 此处执行调用uwfmgr.exe的代码逻辑 Wow64RevertWow64FsRedirect(redirectState)
使用绝对路径调用:不要依赖
fi.FullName的路径解析,直接构造绝对路径避免错误:.FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "system32", "uwfmgr.exe")确保权限继承:在
ProcessStartInfo中显式设置Verb为空字符串,确保子进程完全继承当前应用的权限上下文:.Verb = ""
内容的提问来源于stack exchange,提问作者Jaccog
相关产品推荐
相关产品推荐

