Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / February 2006

Tip: Looking for answers? Try searching our database.

Problem with ActiveX UserControl event mechanism

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Vladislav - 16 Feb 2006 11:39 GMT
I wrote an ActiveX UserControl in C# language. My problem is write a
Managed Server as the event source and for example write a COM Client
as the event sink. The managed server declares SampleEvents as an event
sink interface and connects the interface to the SampleControl class.
The unmanaged client creates an instance of the SomeControl class and
implements the event sink interface ( VB.6 client for example). I have
no problem if I wrote simple class library in c# ( ActiveX ) like in
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cp
conRaisingEventsHandledByCOMSink.asp

msdn article, but when I made a UserControl from this class library the
unmanaged client can't see event interface ( handle of event can't
register ), in VB6 I have 459 error, that means "This component doesn't
support the set of events"
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbenlr98/html/v
amsgobjdoesnotsupportevents.asp
).
My client is simple: In VB6 I create a standart exe project with form
on wich I place my .NET ActiveX control with event interface. After
that I wrote a simple handle of control event that generated for
example by timer ( even 1 second ).
Below are my server code (C#) that is simple control with TextBox and
two methods: GetText and SetText.

Managed server (event source)
[C#]
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace EventSource
{
    public delegate void SomeEventDelegate();

    [Guid("C5365803-25EE-4ebc-883B-DB20A4072400"),
    InterfaceType(ComInterfaceType.InterfaceIsIDispatch),
    ComVisible(true)]
    public interface ISampleEvents
    {
           [DispId(1)]
           void SomeEvent();
    };
    [Guid("D4F3706D-A238-4216-BA45-1C11255A7F44"),
    InterfaceType(ComInterfaceType.InterfaceIsIDispatch),
    ComVisible(true)]
    public interface ISampleControl
    {
           [DispId(1)]
           void SetText( string s );
           [DispId(2)]
           String GetText();
           [DispId(3)]
           void OnSomeEvent();
    };
    [Guid("000C9F0F-48C1-4642-879A-28C7B1ABDDBD"),
    ClassInterface(ClassInterfaceType.None),
    ProgId("MST.SampleControl"),
    ComSourceInterfaces(typeof(ISampleEvents))]
    public class SampleControl : UserControl, ISampleControl
    {
           public event SomeEventDelegate SomeEvent;
           private TextBox textBox1;
           private Timer timer1;
           private IContainer components;

           public SampleControl()
           {
        InitializeComponent();
           }
           protected override void Dispose( bool disposing )
           {
        if( disposing )
        {
              if( components != null )
              components.Dispose();
        }
        base.Dispose( disposing );
           }
   #region Component 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.components = new System.ComponentModel.Container();
     this.textBox1 = new System.Windows.Forms.TextBox();
     this.timer1 = new System.Windows.Forms.Timer(this.components);
     this.SuspendLayout();
     //
     // textBox1
     //
     this.textBox1.AutoSize = false;
     this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;
     this.textBox1.Location = new System.Drawing.Point(0, 0);
     this.textBox1.Multiline = true;
     this.textBox1.Name = "textBox1";
     this.textBox1.ReadOnly = true;
     this.textBox1.Size = new System.Drawing.Size(208, 208);
     this.textBox1.TabIndex = 0;
     this.textBox1.Text = "textBox1";
     //
     // timer1
     //
     this.timer1.Enabled = true;
     this.timer1.Interval = 1000;
     this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
     //
     // SampleControl
     //
     this.Controls.Add(this.textBox1);
     this.Name = "SampleControl";
     this.Size = new System.Drawing.Size(208, 208);
     this.ResumeLayout(false);

   }
   #endregion

   #region [Un]Register Class for COM Interop
   [ComRegisterFunction()]
   public static void
   AxRegisterClass( String key )
   {
     // Strip off HKEY_CLASSES_ROOT\ from the passed key as I don't
need it
     StringBuilder sb = new StringBuilder(key);
     sb.Replace("HKEY_CLASSES_ROOT\\", "");
     // Open the CLSID\{guid} key for write access
     RegistryKey rk =
Registry.ClassesRoot.OpenSubKey(sb.ToString(),true);

     // Next create the CodeBase entry - needed if not string named
and GACced.
     RegistryKey inprocServer32 = rk.OpenSubKey("InprocServer32",
true);
     inprocServer32.SetValue("",
"C:\\WINDOWS\\system32\\mscoree.dll");
     inprocServer32.Close() ;

     // And create the 'Control' key - this allows it to show up in
     // the ActiveX control container
     rk.CreateSubKey("Control");
     rk.CreateSubKey("Insertable");
     rk.CreateSubKey("Programmable");
     rk.CreateSubKey("TypeLib").SetValue("",
"{09CC5676-60CB-41bf-B405-09F205845EC4}");
     rk.CreateSubKey("Version").SetValue("", "1.0");

     // Finally close the main key
     rk.Close();
   }
   [ComUnregisterFunction()]
   public static void
   AxUnregisterClass( String key )
   {
     // Strip off HKEY_CLASSES_ROOT\ from the passed key as I don't
need it
     StringBuilder sb = new StringBuilder(key);
     sb.Replace("HKEY_CLASSES_ROOT\\", "");
     // Open the CLSID\{guid} key for write access
     RegistryKey rk =
Registry.ClassesRoot.OpenSubKey(sb.ToString(),true);

     // Delete the 'Control' key, but don't throw an exception if it
does not exist
     rk.DeleteSubKey("Control", false);
     rk.DeleteSubKeyTree("InprocServer32");
     rk.DeleteSubKey("Insertable");
     rk.DeleteSubKey("Programmable", false);
     rk.DeleteSubKey("TypeLib", false);
     rk.DeleteSubKey("Version", false);

     // Finally close the main key
     rk.Close();
   }
   #endregion
   #region ISampleControl Members
   public
   void SetText( string s )
   {
     textBox1.Text = s;
   }

   public String GetText()
   {
     return textBox1.Text;
   }

   public void OnSomeEvent()
   {
     try
     {
       if( SomeEvent != null )
       {
         SomeEvent();
         textBox1.Text += " SomeEvent called";
       }
       else
       {
         textBox1.Text += " SomeEvent == null";
       }
     }
     catch( Exception e )
     {
       MessageBox.Show( "My exception:" + e.Message );
     }
   }

   #endregion

   private void timer1_Tick(object sender, EventArgs e)
   {
     OnSomeEvent();
   }
 }
}
Vladislav Ts. - 27 Feb 2006 11:03 GMT
Hi all, again!
Also I tested my ActiveX control with TestContainer and it is intrested
that event "SomeEvent" happened by timer, may be anyone knows what's
wrong with my code?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.