XP Pro 上使用 WebBrowser 控件绘制 ToolStripComboBox 时出现访问冲突

2024-01-28

我正在调试一个问题,即当用户锁定/解锁计算机或用户按 ctrl-alt-delete 然后按 escape(不一定锁定)时,.NET 4.0(WinForm,而不是 WPF)应用程序在 Windows XP 上崩溃这种情况——但他们可以选择锁定、启动任务管理器等)。这是非常可重复的。

与画画有关ToolStripComboBox。这正在生成一个AccessViolationException在一些gdiplus引擎盖下的例程。

我见过它崩溃的几种不同方式,但都在绘制此控件的同一区域中。这是一个堆栈跟踪:

System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at System.Drawing.SafeNativeMethods.Gdip.GdipFillRectangleI(HandleRef graphics, HandleRef brush, Int32 x, Int32 y, Int32 width, Int32 height)
at System.Drawing.Graphics.FillRectangle(Brush brush, Int32 x, Int32 y, Int32 width, Int32 height)
at System.Drawing.Graphics.FillRectangle(Brush brush, Rectangle rect)
at System.Windows.Forms.ToolStripComboBox.ToolStripComboBoxControl.ToolStripComboBoxFlatComboAdapter.DrawFlatComboDropDown(ComboBox comboBox, Graphics g, Rectangle dropDownRect)
at System.Windows.Forms.ComboBox.FlatComboAdapter.DrawFlatCombo(ComboBox comboBox, Graphics g)
at System.Windows.Forms.ComboBox.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
...

有谁对如何解决这个问题有建议,或者机器锁定/解锁或 ctrl-alt-delete 屏幕的重要性是什么?

EDIT:

我将其归结为粘贴在下面的一个简单应用程序,它在 XP Pro 上可以很好地重现。这是非常普通的——大约是我们能得到的最简单的。一切都是在 UI 线程上创建/操作的。

namespace Test
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }

    public class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            webBrowser2.Navigate("http://www.cnn.com");
        }

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.toolStripContainer1 = new System.Windows.Forms.ToolStripContainer();
            this.webBrowser2 = new System.Windows.Forms.WebBrowser();
            this.toolStrip1 = new System.Windows.Forms.ToolStrip();
            this.toolStripLabel1 = new System.Windows.Forms.ToolStripLabel();
            this.toolStripComboBox1 = new System.Windows.Forms.ToolStripComboBox();
            this.toolStripContainer1.ContentPanel.SuspendLayout();
            this.toolStripContainer1.TopToolStripPanel.SuspendLayout();
            this.toolStripContainer1.SuspendLayout();
            this.toolStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // toolStripContainer1
            // 
            this.toolStripContainer1.BottomToolStripPanelVisible = false;
            // 
            // toolStripContainer1.ContentPanel
            // 
            this.toolStripContainer1.ContentPanel.Controls.Add(this.webBrowser2);
            this.toolStripContainer1.ContentPanel.Size = new System.Drawing.Size(555, 268);
            this.toolStripContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.toolStripContainer1.LeftToolStripPanelVisible = false;
            this.toolStripContainer1.Location = new System.Drawing.Point(0, 0);
            this.toolStripContainer1.Name = "toolStripContainer1";
            this.toolStripContainer1.RightToolStripPanelVisible = false;
            this.toolStripContainer1.Size = new System.Drawing.Size(555, 296);
            this.toolStripContainer1.TabIndex = 1;
            this.toolStripContainer1.Text = "toolStripContainer1";
            // 
            // toolStripContainer1.TopToolStripPanel
            // 
            this.toolStripContainer1.TopToolStripPanel.Controls.Add(this.toolStrip1);
            // 
            // webBrowser2
            // 
            this.webBrowser2.Dock = System.Windows.Forms.DockStyle.Fill;
            this.webBrowser2.Location = new System.Drawing.Point(0, 0);
            this.webBrowser2.MinimumSize = new System.Drawing.Size(20, 20);
            this.webBrowser2.Name = "webBrowser2";
            this.webBrowser2.Size = new System.Drawing.Size(555, 268);
            this.webBrowser2.TabIndex = 0;
            // 
            // toolStrip1
            // 
            this.toolStrip1.Dock = System.Windows.Forms.DockStyle.None;
            this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.toolStripLabel1,
            this.toolStripComboBox1});
            this.toolStrip1.Location = new System.Drawing.Point(3, 0);
            this.toolStrip1.Name = "toolStrip1";
            this.toolStrip1.Size = new System.Drawing.Size(173, 28);
            this.toolStrip1.TabIndex = 0;
            // 
            // toolStripLabel1
            // 
            this.toolStripLabel1.Name = "toolStripLabel1";
            this.toolStripLabel1.Size = new System.Drawing.Size(38, 25);
            this.toolStripLabel1.Text = "blah";
            // 
            // toolStripComboBox1
            // 
            this.toolStripComboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.toolStripComboBox1.Items.AddRange(new object[] {
            "a b",
            "c",
            "d",
            "e",
            "f"});
            this.toolStripComboBox1.Name = "toolStripComboBox1";
            this.toolStripComboBox1.Size = new System.Drawing.Size(121, 28);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(555, 296);
            this.Controls.Add(this.toolStripContainer1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.toolStripContainer1.ContentPanel.ResumeLayout(false);
            this.toolStripContainer1.TopToolStripPanel.ResumeLayout(false);
            this.toolStripContainer1.TopToolStripPanel.PerformLayout();
            this.toolStripContainer1.ResumeLayout(false);
            this.toolStripContainer1.PerformLayout();
            this.toolStrip1.ResumeLayout(false);
            this.toolStrip1.PerformLayout();
            this.ResumeLayout(false);
        }
        #endregion

        private ToolStripContainer toolStripContainer1;
        private ToolStrip toolStrip1;
        private ToolStripLabel toolStripLabel1;
        private ToolStripComboBox toolStripComboBox1;
        private WebBrowser webBrowser2;
    }
}

EDIT 2:

我无法在 Windows XP Media Center 版本上重现这一点,到目前为止只能在 XP Pro 上重现。


我们遇到这个问题已经有一段时间了。相同的调用堆栈,当用户执行 ctrl-alt-del 和 escape 时会发生。 场景有点不同,因为它发生在我们的 WinForm 应用程序中,该应用程序是 WinForm 和 WPF 控件的混合体。在应用程序中,我们有一个托管在 ElementHost 中的 WPF UserControl,当该组件位于浮动窗口中、悬停在正在重新绘制的 WinForm 控件上并且屏幕被锁定和解锁时,会引发 AccessViolationException 并且应用程序崩溃。我应该提到的是,我们也在使用 Infragistics UltraDockWorkspace。

我们还没有找到解决方案,但是最近使用 MS 源进行调试时,我注意到抛出异常的方法处有这样的注释(在 System.Drawing 中的 Graphics.cs 中):

         /// <devdoc>
    ///     GDI+ will return a 'generic error' with specific win32 last error codes when
    ///     a terminal server session has been closed, minimized, etc...  We don't want 
    ///     to throw when this happens, so we'll guard against this by looking at the
    ///     'last win32 error code' and checking to see if it is either 1) access denied 
    ///     or 2) proc not found and then ignore it. 
    ///
    ///     The problem is that when you lock the machine, the secure desktop is enabled and 
    ///     rendering fails which is expected (since the app doesn't have permission to draw
    ///     on the secure desktop). Not sure if there's anything you can do, short of catching
    ///     the desktop switch message and absorbing all the exceptions that get thrown while
    ///     it's the secure desktop. 
    /// </devdoc>
    private void CheckErrorStatus(int status) { 
        if (status != SafeNativeMethods.Gdip.Ok) { 
            // Generic error from GDI+ can be GenericError or Win32Error.
            if (status == SafeNativeMethods.Gdip.GenericError || status == SafeNativeMethods.Gdip.Win32Error) { 
                int error = Marshal.GetLastWin32Error();
                if (error == SafeNativeMethods.ERROR_ACCESS_DENIED || error == SafeNativeMethods.ERROR_PROC_NOT_FOUND ||
                        //here, we'll check to see if we are in a term. session...
                        (((UnsafeNativeMethods.GetSystemMetrics(NativeMethods.SM_REMOTESESSION) & 0x00000001) != 0) && (error == 0))) { 
                        return;
                    } 
                } 

            //legitimate error, throw our status exception 
            throw SafeNativeMethods.Gdip.StatusException(status);
        }
    }

根据我的理解,由于安全桌面模式下的渲染,AccessViolationException 应在此处被抑制,但在某些情况下,此异常正在传播。

抱歉,我无法提供解决方案,但希望这些信息有用。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

XP Pro 上使用 WebBrowser 控件绘制 ToolStripComboBox 时出现访问冲突 的相关文章

随机推荐