【发布时间】:2013-03-03 23:41:48
【问题描述】:
我的 C# 代码通过 P/Invoke 调用 Win32 函数来使用模拟
internal class Win32Native
{
[DllImport("advapi32.dll", SetLastError = true)]
public static extern int ImpersonateLoggedOnUser(IntPtr token);
[DllImport("advapi32.dll", SetLastError = true)]
public static extern int RevertToSelf();
}
try {
var token = obtainTokenFromLogonUser();
Win32Native.ImpersonateLoggedOnUser( token );
throw new Exception(); // this is for simulation
Win32Native.RevertToSelf()
} catch( Exception e ) {
LogException( e );
throw;
}
我还安装了AppDomain.CurrentDomain.UnhandledException 处理程序,它还会记录所有未处理的异常。
我确信记录异常的代码无论有没有模拟都可以正常工作。
现在的问题是,在上面的代码中,似乎没有输入catch,也没有调用UnhandledException。该异常的唯一痕迹是事件查看器中的一个条目。
如果我像这样添加finally:
try {
var token = obtainTokenFromLogonUser();
Win32Native.ImpersonateLoggedOnUser( token );
try {
throw new Exception(); // this is for simulation
} finally {
Win32Native.RevertToSelf()
}
} catch( Exception e ) {
LogException( e );
throw;
}
然后从catch 和UnhandledException 处理程序都可以正常记录异常。
发生了什么事?被模拟的线程是否会阻止通常的异常处理?
【问题讨论】:
-
如果你在
LogException设置断点:你到达那里了吗? -
@MarcGravell:我不知道,没有调试器可以复制它。我意识到
LogException()本身可能存在一些问题,但到目前为止,它所依赖的代码无论是模拟还是非模拟都运行良好。 -
在这里猜测一下:由于 .NET 会跟踪执行上下文,因此异常处理可能会意识到上下文已更改并阻止异常处理程序执行,除非首先执行还原。如果代码与模拟用户一起运行,不这样做会导致特权提升...
-
出于兴趣 - 你为什么选择原生路线而不是使用
WindowsIdentity中的内置支持? -
@Damien_The_Unbeliever:这是我碰巧在 Google 上找到的最好的。
标签: c# .net exception exception-handling impersonation