PRB: ActiveX Error When You Sink Managed Events in Internet Explorer Script (814664)



The information in this article applies to:

  • Microsoft .NET Framework 1.0
  • Microsoft Internet Explorer (Programming) 5.5
  • Microsoft Internet Explorer (Programming) 6.0
  • Microsoft .NET Framework 1.1

SYMPTOMS

When you sink managed events in Internet Explorer script, you may receive the following warning message:
An ActiveX control on this page is not safe. Your current security settings prohibit running unsafe controls on this page. As a result this page may not display as intended

WORKAROUND

To work around this problem, create a dummy interface and then implement the newly created interface in a MyWindowControl class that exists already. Make the following modifications to the code:
...
 public interface MyDummyInterface
 {
 }
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None),System.Runtime.InteropServices.ComSourceInterfaces(typeof(ControlEvents))]
   	public class MyWindowControl : System.Windows.Forms.UserControl,MyDummyInterface
{
...
} 

STATUS

Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Steps to Reproduce the Behavior:

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. In the New Project dialog box, click Visual C# Projects under Project Types, and then click Windows Control Library under Templates.
  4. Name the project ActiveXSourcing and then select ..\Inetpub\wwwroot as the Location. Click OK.
  5. Copy and then paste the following code in the control window:
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    namespace ActiveXSourcing
    {
        public delegate void ClickEventHandler(int x, int y); 
    
        // Source interface for events to be exposed
        // Add GuidAttribute to the source interface to supply an explicit System.Guid.
        // Add InterfaceTypeAttribute to indicate that interface is the IDispatch interface.
    
        [System.Runtime.InteropServices.GuidAttribute("0422D916-C11A-474e-947D-45A107038D12") ]
    
        [System.Runtime.InteropServices.InterfaceTypeAttribute(System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIDispatch)]
        public interface ControlEvents 
    
            // Add a DisIdAttribute to any members in the source interface to         // specify the COM DispId.
        {
            [System.Runtime.InteropServices.DispIdAttribute(0x60020000)]
            void ClickEvent(int x, int y);
        }
    
        // Add a ComSourceInterfaces attribute to the control to identify        //the list of interfaces that are exposed as COM event sources. 
    
        [System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None),System.Runtime.InteropServices.ComSourceInterfaces(typeof(ControlEvents))]
        public class MyWindowControl : System.Windows.Forms.UserControl //, ComInteropControlInterface
        {
    
            System.Windows.Forms.TextBox tx = new TextBox();
    
            private void InitializeComponent()
            {
    
                this.Name = "MyWindowControl";
    
            }
    
            event ActiveXSourcing.ClickEventHandler ClickEvent;
    
            public MyWindowControl() : base()
            {                              
    
                initMyWindowControl();
    
            }
    
            private void initMyWindowControl() 
            {
    
                Size = new System.Drawing.Size(300, 50);
                tx.Text = "Click the text box to invoke  'ClickEvent'";
                tx.Size = this.Size;                  
                tx.Click += new System.EventHandler(ClickHandler);
                this.Controls.Add(tx);
    
            }
    
    
            private void ClickHandler(object sender, System.EventArgs e)
            {
                if (ClickEvent != null) 
                {
                    ClickEvent(0, 0);
                }
            }
        }
    }
  6. To create a test HTML page, right-click the project node in Solution Explorer ,and then click Add New Item.
  7. Select HTML page under Template, and then name the file Default.htm .
  8. Paste the following code in Default.htm:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
       <META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=iso-8859-1' />
    
       <HTML>
       	<HEAD>
       		<TITLE>Sink managed event in Internet Explorer</TITLE>
       		
       		
       	</HEAD>
       	
       	<BODY>
       		
       		<OBJECT id="ctrl" classid="http://localhost/ActiveXSourcing/bin/debug/ActiveXSourcing.dll#ActiveXSourcing.MyWindowControl">
       		</OBJECT>
       		<SCRIPT LANGUAGE="JScript">
                   function ctrl::ClickEvent(a,b)
                   {
                       alert("MyWindowControl_ClickEvent");
                   }
       		</SCRIPT>
       		
       	</BODY>
       </HTML>
    
  9. Compile the control as a DLL.
  10. Use the following code to turn off the security on the control:
    caspol -s off
  11. Open Internet Explorer. Locate the following Web page: http://localhost/ActiveXSourcing/default.htm

REFERENCES


For additional information, click the following article numbers to view the articles in the Microsoft Knowledge Base:

313891 HOW TO: Sink Managed C# Events in Internet Explorer Script

316510 BUG: Security Exception When You Use Event Handlers in Internet Explorer


For more information, visit the following Microsoft Developer Network Web sites:


Modification Type:MajorLast Reviewed:6/5/2003
Keywords:kbCOMInterop kbprb KB814664 kbAudITPRO