Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem with ActiveX UserControl event mechanism

6 views
Skip to first unread message

Vladislav

unread,
Feb 16, 2006, 6:39:50 AM2/16/06
to
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/cpconRaisingEventsHandledByCOMSink.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/vamsgobjdoesnotsupportevents.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.

unread,
Feb 27, 2006, 6:03:51 AM2/27/06
to
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?

0 new messages