Click here to Skip to main content
15,029,162 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello

I have written an ActiveX control in C#. See code below:

C#
namespace ActiveXCSharp
{
	[ComVisible(true)]
	[ProgId("ActiveXCSharp.ButtonAx2")]
	[ComSourceInterfaces(typeof(IButtonAx2))]
	[ClassInterface(ClassInterfaceType.AutoDual)]
	public class ButtonAx2 : Button, IButtonAxInterface
	{
		// ptr to dll adapter
		private IntPtr _adapterDllPtr;
		private const uint DotNetButtonPressed = 0x0400;

		public ButtonAx2()
		{
			Application.EnableVisualStyles();

			InitComEvents();
			Click += ButtonAx2Click;
		}

		#region eventhandling
		
		private void InitComEvents()
		{
			ButtonClicked = ComEventButtonClicked;
		}

		public delegate void ButtonClickedEventHandler();
		public event ButtonClickedEventHandler ButtonClicked;
		// Dummy Method to use when firing the event
		private void ComEventButtonClicked() { }

		private void FireButtonClickedEvent()
		{
			if (ButtonClicked != null)
			{
				ButtonClicked();
			}
		}

		private void ButtonAx2Click(object sender, EventArgs e)
		{
			SendMessage(_adapterDllPtr.ToInt32(),
			            DotNetButtonPressed, IntPtr.Zero, IntPtr.Zero);
			FireButtonClickedEvent();
		}

		#endregion eventhandling

		#region IButtonAxInterface Member

		public void SetButtonCaption(String strNewCaption)
		{
			Text = strNewCaption;
		}

		public void SetAdapterDllPtr(IntPtr iAdapterDllPtr)
		{
			_adapterDllPtr = iAdapterDllPtr;
		}

		#endregion IButtonAxInterface Member

		[ComRegisterFunction]
		public static void RegisterClass(string key)
		{
			...
		}

		[ComUnregisterFunction]
		public static void UnregisterClass(string key)
		{
			...
		}

		#region MAPPING_OF_USER32_DLL_SECTION

		...

		#endregion

	}

}


I want to use it in an MFC-Dialog. I can drop the control on the designer and it will proper shown on design- and runtime. But i'am not able to create an instance of it.

When i rightclick the control on the designer and choose create variable a Messagebox appears that says "Access to the type library for ActiveX controls isn't possible."

The control is registered and at the same place as the .dll there is also the .tlb.

My question is how can i create an instance of my activeX for the use in the C++ code?

Best regards Michael
Posted
Updated 14-Sep-11 0:11am
v2

1 solution

I found a solution.
Maybe someone else is interested. I use .NET Framwork 2.0, and in this the method with the attribute ComRegisterFunctionmust have the signature void methodName(Type). In later frameworks the signature void methodName(String) is also allowed.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900