使用GUI制作COM服务器的最佳实践是什么?

问题描述 投票:0回答:1

语境:

我正在使用一些旧的机器人软件,如果它实现了一些预定义的接口,它能够从COM对象获取信息。由于这个旧软件在Windows 2000上运行,我想使用DCOM(试图)避免为Windows 2000编译一些东西。(我知道必须调用的程序可能需要工作,或者至少存在,在Windows 2000计算机上也是如此)

另一台计算机将通过DCOM调用,并将使用一组配置的步骤处理图像。我想要一个GUI来显示传入和传出的图像以及更改它所执行的步骤的能力。


问题

图像处理器需要实现两个接口,但是Interface1包含Load函数,Form也是如此,但两者都需要保持可访问状态,所以我认为我不能使用new关键字。因此,下面的代码将无法正常工作。

public class ServerForm : Form, Interface1, Interface2 { }

我可以像这样分开它们:

public class ServerForm : Form { }

public class MyImageProcessor : Interface1, Interface2 { }

但我仍然需要从MyImageProcessor类访问Form。我尝试通过构造函数将Form传递给MyImageProcessor,如下所示:

static class Program {
        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            ServerForm sv = new ServerForm();
            MyImageProcessor mip = new MyImageProcessor(sv);
            Application.Run(sv);
        }
    } 

但是当我使用regasm CSharpServer.exe /regfile:CSharpServer.reg时,要检查哪些键被添加到注册表中,只显示ServerForm而不是ImageProcessor。

我怎么解决这个问题?

c# user-interface com dcom regasm
1个回答
0
投票

这个解决方案解决了上面提到的两个问题:两个load函数都将保持可用并生成正确的注册表项,但是如果它稍后会给COM带来任何问题我也不会。 (它可能会)

我把它分成了两个类。 ServerForm是从MyImageProcessor的构造函数生成并运行的。

public class MyImageProcessor : Interface1, Interface2{
    private static ServerForm sv;      

    public MyImageProcessor () {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        sv = new ServerForm();
        Application.Run(sv);
    }
}

public class ServerForm : Form {
        public ServerForm() {
            InitializeComponent();
        }
}

我想强调,这解决了现在的问题,如果我把一切都搞定了,我会尝试更新这篇文章。

© www.soinside.com 2019 - 2024. All rights reserved.