从 C++ 调用 C# .NET 服务应用?
我有一个用 Visual Studio 6 编写的旧 MFC 应用程序。这将在某个时候用 C# .NET 重写。然而,在此之前我必须为现有应用程序编写几个新的 Windows 服务。其他的都是用ATL写的。我更愿意做的是用 C# .NET 编写这些新服务,这样当重写应用程序的其余部分时,就不需要这些了。
是否可以从旧应用程序调用 .NET Windows 服务中托管的库上的接口?如果是这样,您能解释一下如何做吗?
I have an old MFC app written in Visual Studio 6. This will at some point be rewritten in C# .NET. However, before then I have to write a couple of new Windows services for the existing application. The others were written in ATL. What I would prefer to do is write these new services in C# .NET so that when the rest of the application is rewritten, these don't need to be.
Is it going to be possible to call the interfaces on the libraries hosted in a .NET windows service from the old application? If so, could you please explain how.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
绝对地。您正在寻找名为 COM-Interop 的 .NET 功能。
http://msdn.microsoft.com/en -us/library/kew41ycz%28v=vs.71%29.aspx
http://msdn.microsoft.com/en-us/magazine/cc163494.aspx
第二个链接有一个 ATL 示例。
编辑:
根据您在评论中的反馈,让我对此进行扩展...
啊 - 您对该页面上的示例的看法是正确的。
第一个链接实际上是您想要开始了解所有详细信息的地方。如果您点击链接,您将找到此页面:
“将 .NET Framework 组件暴露给 COM”
http://msdn.microsoft.com/en -us/library/zsfww439%28v=vs.71%29.aspx
本质上,这只是将一系列属性应用于您的类和属性,然后在客户端计算机上生成适当的注册表项(.NET 有一个工具可以执行此操作 - 请参阅:http://msdn.microsoft.com/en-us/library/bctyca52%28v=vs.71%29.aspx)
我自己已经为 .NET 项目这样做过好几次了人们需要从 VC++ 和/或 VB6 进行调用。
其他一些可能感兴趣的链接:
http://www.codeproject.com/KB/ COM/nettocom.aspx <-- 您想要做的事情的完美示例。
http://www.codeproject.com/KB/COM/Universal_CCW.aspx
Absolutely. You're looking for a feature of .NET called COM-Interop.
http://msdn.microsoft.com/en-us/library/kew41ycz%28v=vs.71%29.aspx
http://msdn.microsoft.com/en-us/magazine/cc163494.aspx
The second link has an ATL example.
EDIT:
Based on your feedback in the comments, let me expand on this...
Ah - you're right about the sample on that page.
The first link is really where you want to start for all the details. If you follow the links, you'll find this page:
"Exposing .NET Framework Components to COM"
http://msdn.microsoft.com/en-us/library/zsfww439%28v=vs.71%29.aspx
Essentially, it's just a matter of applying a series of attributes to your classes and properties, and then generating the appropriate registry entries on the client machine (which .NET has a tool to do - see: http://msdn.microsoft.com/en-us/library/bctyca52%28v=vs.71%29.aspx)
I've done this several times myself for .NET projects people needed to call from VC++ and/or VB6.
Some other links that might be of interest:
http://www.codeproject.com/KB/COM/nettocom.aspx <-- Perfect example of what you're trying to do.
http://www.codeproject.com/KB/COM/Universal_CCW.aspx
我已经使用 Visual Studio 2008 中基于 MFC 的 C++ 应用程序和基于 .NET 的 C# Windows 服务完成了这件事。
首先,如果您还没有创建 C# Windows 服务,我有一些创建基本框架的教程。 第一个教程提供了分步说明创建服务并将事件写入特定于应用程序的事件日志的过程。 第二个教程< /a> 显示了如何修改服务以从命令行安装和卸载自身,我发现这很有用。
其次,您需要决定如何在 MFC 应用程序和 Windows 服务之间进行通信。任何类型的进程间通信 (IPC) 模型都可以工作 - 套接字、管道、共享内存、WCF 等。由于您无论如何都想迁移到 .NET,所以我建议使用 Windows Communication Foundation (WCF),它是我就是这样做的。具体来说,我根据显示的图表 这里。
如果您采用 WCF 路线,您将受益于应用程序和服务之间的通信是基于 .NET 的事实。因此,当您将应用程序移至.NET 时,无需重写通信机制。与此同时,技巧是让您的 MFC 应用程序使用 WCF 代码。为此,请使用 C# 在 .NET 程序集中编写 WCF 客户端代码。然后,使用 C++ dll 来弥合 MFC 代码和 .NET 程序集之间的差距。我有另一个教程,分步说明有关如何执行此操作的步骤说明。
希望这有帮助。
I've done this exact thing with an MFC-based C++ application in Visual Studio 2008 and a .NET-based C# Windows service.
First, if you have not created the C# Windows services yet, I've got a couple of tutorials for creating the basic framework. The first tutorial provides a step-by-step procedure for creating the service and writing events to an application-specific event log. The second tutorial shows how to modify the service to install and uninstall itself from the command line, which I find of great use.
Second, you need to decide how you are going to communicate between your MFC application and your Windows service. Any kind of inter-process communication (IPC) model will work - sockets, pipes, shared memory, WCF, etc. Since you are wanting to migrate to .NET anyway, I would recommend using Windows Communication Foundation (WCF), which is the way I've done it. Specifically, I chose the named pipe aspect of WCF for my communication method based on the chart shown here.
If you go down the WCF route, you'll benefit from the fact that the communication between application and service is .NET-based. Thus, when you move your application to .NET, the communication mechanism won't have to be rewritten. The trick in the meantime is getting your MFC application to use the WCF code. To do this, write the WCF client code in a .NET assembly using C#. Then, use a C++ dll to bridge the gap between your MFC code and the .NET assembly. I've got another tutorial with step-by-step instructions for how to do this.
Hope this helps.