.NET Control to be used from MFC

Hello,

I am trying to get a .NET control running in a standard MFC dialog. However, I
don't get the events from it.

The ActiveX control container seems to have no problem as it displays the event from
the .NET control. Also, in Studio 6, I can connect to the event using the class wizard as if
everything would be ok. Still, the event handler is never called.

Here's the c# code of my sample control:

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Security;
using Microsoft.Win32;

[assembly:AllowPartiallyTrustedCallersAttribute]

namespace DotNetControl
{
// Source interface with "event handlers" for COM objects to implement
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IPhoneEvents
{
[DispId(1)] void Ring();
}

// Delegates for the events
public delegate void RingEventHandler();

/// <summary>
/// Summary description for UserControl1.
/// </summary>
[ComSourceInterfaces(typeof(IPhoneEvents))]
public class DotNetControl : System.Windows.Forms.UserControl
{
public event RingEventHandler Ring;

// Custom Registration Function
[ComRegisterFunction]
private static void ComRegisterFunction(Type t)
{
RegistryKey key = Registry.ClassesRoot.OpenSubKey("CLSID\\" +
t.GUID.ToString("B"), true);
// Step 1
key.CreateSubKey("Control");
// Step 2
key.CreateSubKey(
"Implemented Categories\\{40FC6ED4-2438-11CF-A3DB-080036F12502}");
// Step 3
key.CreateSubKey("MiscStatus").SetValue("", "131457");
// Step 4
key.CreateSubKey("TypeLib").SetValue("",
Marshal.GetTypeLibGuidForAssembly(t.Assembly).ToString("B"));
// Step 5
string version = t.Assembly.GetName().Version.Major.ToString() +
"." + t.Assembly.GetName().Version.Minor.ToString();
if (version == "0.0") version = "1.0";
key.CreateSubKey("Version").SetValue("", version);
key.Close();
}

// Custom Unregistration Function
[ComUnregisterFunction]
private static void ComUnregisterFunction(Type t)
{
try 
{
Registry.ClassesRoot.DeleteSubKeyTree(
"CLSID\\" + t.GUID.ToString("B"));
}
// Ignore exception thrown if key doesn't exist
catch (ArgumentException) {} 
}

private System.Windows.Forms.Button ring;


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

public DotNetControl()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();

// Hook up the class's private methods to the events
//Ring += new RingEventHandler(OnRing);
//CallerId += new CallerIdEventHandler(OnCallerId);

}

/// <summary>
/// Clean up any resources being used.
/// </summary>
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.ring = new System.Windows.Forms.Button();
this.SuspendLayout();
// 
// ring
// 
this.ring.Location = new System.Drawing.Point(32, 24);
this.ring.Name = "ring";
this.ring.TabIndex = 0;
this.ring.Text = "Ring";
this.ring.Click += new System.EventHandler(this.ring_Click);
// 
// DotNetControl
// 
this.Controls.AddRange(new System.Windows.Forms.Control[] {
  this.ring});
this.Name = "DotNetControl";
this.ResumeLayout(false);

}
#endregion

private void ring_Click(object sender, System.EventArgs e)
{
if(Ring!=null)
Ring();
}

public void CallMe()
{
System.Windows.Forms.MessageBox.Show("CallMe");
}
}
}

What is wrong

Thanx in advance,
  Toby.


Answer this question

.NET Control to be used from MFC

  • Frankie Ho

    Toby,
    I have had similar problems.
    I have a 3rd party app (written in MFC from what Spy++ tells me) that requires plug-in Active X objects.
    I have succesfully got my .NET controls to load but events are not sinked properly.
    I have created test clients for my .NET server both in VB6 and VB.NET and they seem to work fine so the problem seems to be the way MFC handled Active X objects.
    I hope we can get a resolution to this problem

  • .NET Control to be used from MFC