用 C# 编写 ActiveX 控件,但在 MFC 中使用它

发布于 2025-01-05 06:01:35 字数 1144 浏览 1 评论 0原文

目前的项目都是用VC6VB6编写的。已经证明.Net可以编写Active X控件供MFC使用。因此,我们希望使用 .Net 编写一些 Active x 控件,并对遗留代码进行最小的更改。

这里是要求

  1. 主机应用程序(用VC6编写)必须能够从注册的类别中找到并加载active x控件。
  2. Active x 控件必须支持属性页。
  3. Active x 控件必须支持属性持久性。

这是我的问题

  1. 是否可以使用 .Net WinForm 控件编写 Active X 属性页?以及C#中控件和相应属性之间如何通信?
  2. 如何在C#中注册某个类别下的Active X控件?下面是一段MFC中关于控件注册的示例代码。

    STDAPI DllRegisterServer(void)
    {
    
    HRESULT 小时;  
    
    AFX_MANAGE_STATE(_afxModuleAddrThis);
    
    if (!AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _tlid))
      返回 ResultFromScode(SELFREG_E_TYPELIB);
    
    if (!COleObjectFactoryEx::UpdateRegistryAll(TRUE))
      返回 ResultFromScode(SELFREG_E_CLASS); 
    
    hr = 创建组件类别(CATID_CTRL_CAT, 
         L“XXXX”);
    如果(失败(小时))
      返回小时;
    
    hr = RegisterCLSIDInCategory(CATID_CONTROL, 
         CATID_CTRL_CAT);
    如果(失败(小时))
        返回小时; 
    
    返回无错误;
    }
    

    上面的注册有3个步骤,AfxOleRegisterTypeLibCreateComponentCategoryRegisterCLSIDInCategory。如何在C#中实现它们?

  3. C# 中实现属性持久化的等效方法是什么?

预先感谢您的任何评论和帮助!

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!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

§对你不离不弃 2025-01-12 06:01:35

我必须编写一个 .NET ActiveX 组件以供遗留应用程序使用,并且我最初探索在 C# 中实现此组件。虽然在 C# 中实现一个组件来满足“ActiveX 组件”的现代定义(即实现 COM IUnknown 接口)并不是非常困难,但在 C# 中构建一个支持所有拖放操作的组件使用我的组件的遗留应用程序开发人员所期望的易用性要求它实现许多附加的 COM 接口。

我的理解是,从技术上讲,您可以自己在 C# 中执行此操作,前提是导入正确的 COM 接口,然后在组件中正确实现它们,这将是一项艰巨的工作。

我最终创建了一个普通的 C# 组件(不涉及 ActiveX)来包含所有实际逻辑,然后我在 C# 组件周围创建了一个 C++/CLI(又名托管 C++)包装器,该包装器使用 MFC 提供必要的 ActiveX 功能。

免责声明:我不必处理属性页或属性持久性。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文