Register winform user control as a COM server

I am trying to register a winform user control as a COM server so that my MFC applications can use them. The winform control is written in C ++ / CLI and uses an unmanaged native DLL. I want to use the .net awesome Interop services to register this user control as a COM server.

[ProgId("MyAx.MyAxControl")] [ClassInterface(ClassInterfaceType::AutoDual)] [Guid("612EAF58-ADCC-4e87-BC9E-FA208B037414")] public ref class MyAxControl: public System::Windows::Forms::UserControl 

MSDN said that I can use regasm to achieve what I am doing, so I went and registered it

 regasm MyAx.dll /tlb:MyAx.tlb 

I even generated a registry entry

 regasm MyAx.dll /regfile:MyAx.reg 

and combined it with my registry

At this point, I expected this control to appear in my COM components when I select "Elements" for the toolbar. However, he does not appear there. Is this expected behavior? If so, how will I use this control in my MFC application, in fact, any language that uses the ActiveX control (say, Java).

+4
source share
1 answer

What you are missing tells the system that your COM object is a control. Invalid information is the "Implemented Categories" entry in the registry. To provide this information during COM registration, you will need to create a custom COM (un) registration function.

 private const string ControlsCategoryId = "{40FC6ED4-2438-11CF-A3DB-080036F12502}"; [ComRegisterFunction] internal static void ComRegister(string key) { key = key.Replace("HKEY_CLASSES_ROOT\\", ""); RegistryKey reg = Registry.ClassesRoot.CreateSubKey(key); reg.SetValue("", "Your.ProgId.Here"); reg = reg.CreateSubKey("Implemented Categories"); reg.CreateSubKey(ControlsCategoryId); } [ComUnregisterFunction] internal static void ComUnregisterFunction(string key) { key = key.Replace("HKEY_CLASSES_ROOT\\", ""); RegistryKey reg = Registry.ClassesRoot.OpenSubKey(key, true); reg = reg.OpenSubKey("Implemented Categories", true); reg.DeleteSubKey(ControlsCategoryId); } 

Com (un) registration procedures must be static, return void, accept a single line or type argument, and have the corresponding attribute. MSDN now claims that only a type argument is accepted, but for compatibility reasons, the string version also works (from version 1.1). The string passed to this function is the registry key HKCR \ CLSID {YOUR-CLASS_GUID}.

Hope this helps.

+1
source

All Articles