1
votes

The current projects are all written in VC6 and VB6. It has been proved that .Net can write Active X controls for MFC use. So we'd like to write some active x controls using .Net with minimum changes in the legacy code.

Here is the requirements:

  1. The Host application (written in VC6) must be able to find and load the active x control from the registered category.
  2. The active x control must support property pages.
  3. The active x control must support property persistence.

Here is my questions:

  1. Is it possible to write Active X property pages using .Net WinForm Controls? And how to communicate between controls and corresponding properties in C#?
  2. How to register the Active X control under a certain category in C#? The following is a piece of sample code in MFC regarding to control registration.

    STDAPI DllRegisterServer(void)
    {
    
    HRESULT hr;  
    
    AFX_MANAGE_STATE(_afxModuleAddrThis);
    
    if (!AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _tlid))
      return ResultFromScode(SELFREG_E_TYPELIB);
    
    if (!COleObjectFactoryEx::UpdateRegistryAll(TRUE))
      return ResultFromScode(SELFREG_E_CLASS); 
    
    hr = CreateComponentCategory(CATID_CTRL_CAT, 
         L"XXXX");
    if (FAILED(hr))
      return hr;
    
    hr = RegisterCLSIDInCategory(CATID_CONTROL, 
         CATID_CTRL_CAT);
    if (FAILED(hr))
        return hr; 
    
    return NOERROR;
    }
    

    The above registration has 3 steps, AfxOleRegisterTypeLib, CreateComponentCategory and RegisterCLSIDInCategory. How can we implement them in C#?

  3. What is the equivalent way in C# to achieve property persistence?

Thank you in advance for any comments and helps!!!

1

1 Answers

2
votes

I had to write a .NET ActiveX component to be consumed by a legacy application, and I initially explored implementing this in C#. While it's not terribly difficult to implement a component in C# that provides the bare minimum to satisfy the modern definition of "ActiveX component" (i.e. implement the COM IUnknown interface), building a component in C# that supports all of the drag-and-drop ease of use that was expected by the legacy application developers consuming my component required that it implement many additional COM interfaces.

My understanding is that it's technically possible to do this yourself in C#, provided that you import the right COM interfaces and then implement them properly within your component, this would be a lot of work.

I ended up creating a normal C# component (no ActiveX involved) to contain all of my actual logic, and then I created a C++/CLI (A.K.A. Managed C++) wrapper around my C# component that used MFC to provide the necessary ActiveX functionality.

Disclaimer: I did not have to deal with property pages or property persistence.