- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
文件中存在类型为 SECURITY_IDENTIFIER 结构的对象。我需要从此结构中获取所有者 SID。为此,我调用 GetSecurityDescriptorOwner
WinAPI 函数并创建 System.Security.Principal.SecurityIdentifier
(它具有以 IntPtr 作为参数的重载)
问题是文件中的这个结构有时会被破坏,所以我从 GetSecurityDescriptorOwner 获得的指针无效。它不是 IntPtr.Zero,它是无效的,所以当我创建类型为 SecurityIdentifier
的对象时,我得到了 AccessViolationException
,这是不可能通过简单的 try- 捕捉到 .NET 4 的捕获。
我知道允许捕获此类异常的属性,所以我暂时使用它,但我不喜欢这个解决方案。不建议捕获损坏状态异常 (CSE),但我没有看到任何其他解决方案。这个 WinAPI 函数返回无效指针,我看不出有什么方法可以检查它的有效性。有什么想法吗?
更新
应用程序接口(interface)
BOOL WINAPI GetSecurityDescriptorOwner(
_In_ PSECURITY_DESCRIPTOR pSecurityDescriptor,
_Out_ PSID *pOwner,
_Out_ LPBOOL lpbOwnerDefaulted
);
外部定义
[DllImport("Advapi32.dll")]
static extern bool GetSecurityDescriptorOwner(
IntPtr pSecurityDescriptor,
out IntPtr owner,
out bool defaulted);
更新
private static SecurityIdentifier GetSecurityIdentifier()
{
// Allocate managed buffer for invalid security descriptor structure (20 bytes)
int[] b = new int[5] {1, 1, 1, 1, 1};
// Allocate unmanaged memory for security descriptor
IntPtr descriptorPtr = Marshal.AllocHGlobal(b.Length);
// Copy invalid security descriptor structure to the unmanaged buffer
Marshal.Copy(b, 0, descriptorPtr, b.Length);
IntPtr ownerSid;
bool defaulted;
if (GetSecurityDescriptorGroup(descriptorPtr, out ownerSid, out defaulted))
{
// GetSecurityDescriptorGroup returns true, but `ownerSid` is `1`
// Marshal.GetLastWin32Error returns 0 here
return new SecurityIdentifier(ownerSid);
}
return null;
}
此代码有时会从 SecurityIdentifier 构造函数中抛出损坏的状态异常。有什么解决办法吗?
最佳答案
您是否尝试调用 IsValidSecurityDescriptor
?
[DllImport("Advapi32.dll")]
static extern bool IsValidSecurityDescriptor(IntPtr pSecurityDescriptor);
if (IsValidSecurityDescriptor(descriptorPtr) &&
GetSecurityDescriptorOwner(descriptorPtr, out ownerSid, out defaulted))
{
return new SecurityIdentifier(ownerSid);
}
关于c# - 在 .NET 4 中捕获 AccessViolation。好还是坏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12306812/
此 WPF 应用以 .NET 3.5 为目标,并使用 Visual Studio 2010 的 4.0 工具构建(csc.exe 版本为 4.0.30319.1)。 我不确定我看到的这个异常是怎么回事
这个问题已经有答案了: error in Delphi loadlibrary() (1 个回答) 已关闭10 年前。 我正在尝试在我的 Delphi 项目中加载一个库,该库可以位于两个位置。因此,要
我有一个问题。我正在写入日志文件,但是当我写入时应用程序抛出: **** 中发生类型为“System.AccessViolationException”的未处理异常 附加信息:试图读取或写入 prot
我有一个 Windows 窗体,上面有一些数字(此时只有一个数字)并定期刷新它们(数字是随机生成的)。更新过程与应用程序在一个单独的线程中进行,因此表单可以接收用户单击按钮、调整窗口大小等事件。主要方
当非 UI 线程尝试将其输出附加到 RichTextBox 时,会发生难以跟踪的异常主线程中的 UI 控件。 此异常随机发生,主要是在线程快速连续调用此方法时。它甚至发生在 2 个非 UI 线程中。
我想使用 LockBits 方法更快地比较相似图像,如下所示 using System; using System.Drawing; using System.Drawing.Imaging; usi
我目前正在研究一种解决方案,使错误更易于调试。 所以我有一个已经定义好的 main 并且用户需要定义 wrappermain 来代替 main 的使用: int main(int ac, char**
在各种异步 TCP 操作期间,我不断收到未处理的 AccessViolationException。该异常仅出现在内置函数的反汇编窗口中System.Threading._IOCompletionCa
我正在开发 Windows Phone 8.1 通用应用程序并遇到以下问题。 该应用程序有许多 View 及其相应的 Caliburn.Micro ViewModel,其中两个包含一个 MapCont
我在我的应用程序中使用了第三方工具。在某些系统上,我收到 System.AccessViolation 错误。虽然,通过代码我无法重现该问题。但在生产环境中它有时会重现。 我对下面的代码有疑问 pub
我的表单上有一个 AxWMPLib.AxWindowsMediaPlayer。当我关闭表单时,我收到“尝试读取或写入 protected 内存。这通常表明其他内存已损坏。”异常(exception)。
文件中存在类型为 SECURITY_IDENTIFIER 结构的对象。我需要从此结构中获取所有者 SID。为此,我调用 GetSecurityDescriptorOwner WinAPI 函数并创建
我正在创建 box2d 多边形,我想在将夹具附加到主体的方法之外创建夹具,但是当我这样做时,我遇到了访问冲突。 我已经逐步完成了代码,box2d 肯定正在获取 fixture 对象。 当我在方法内将夹
到目前为止,我可以追踪我在 cocos2dx 应用程序中遇到的每一个错误/崩溃,但现在我觉得自己很笨。我有一个 CCSprite 指针数组,我想做的是在玩家得分为 x 时从缓存中更改它的显示帧。 所有
我在函数中设置两个 XMMATRIX** 类型的变量时遇到了一些问题。函数原型(prototype)如下所示: bool ViewportFactory::CreateViewport(CanvasH
如何在 try-catch 块中捕获 AccessViolation 异常: 这是下面的代码: public static BP GetBloodPressure(string vendorid, s
我在尝试使用 Delphi(Turbo Delphi 2006)程序中用 C++ 编写的 DLL 时遇到了一个奇怪的问题。 当我从命令行运行 Delphi 程序(见下文)时,一切正常。另外,当我在没有
我正在尝试运行以下代码 TCHAR* str1 = TEXT("C:\\Program Files\\Internet Explorer;"); const TCHAR* del = TEXT
我在从 C# 调用 C DLL 时遇到问题 C 函数是(我没有 c 头文件或一个很好的规范:( ) int knr12_read ( char *kn12, char *ik9, char *wok,
我尝试使用 Visual Studio 2013 为 x64 构建我的 Win32 API 项目。但是路由的 WindowProc 回调无法正常工作。我将 SetWindowLongPtr/GetWi
我是一名优秀的程序员,十分优秀!