简体   繁体   English

从C ++应用程序调用C#.NET服务?

[英]Calling C# .NET Service From C++ Application?

I have an old MFC app written in Visual Studio 6. This will at some point be rewritten in C# .NET. 我有一个用Visual Studio 6编写的旧MFC应用程序。这将在某些时候用C#.NET重写。 However, before then I have to write a couple of new Windows services for the existing application. 但是,在此之前,我必须为现有应用程序编写几个新的Windows服务。 The others were written in ATL. 其他人都是用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. 我更喜欢做的是在C#.NET中编写这些新服务,以便在重写应用程序的其余部分时,不需要这些服务。

Is it going to be possible to call the interfaces on the libraries hosted in a .NET windows service from the old application? 是否可以从旧应用程序调用.NET Windows服务中托管的库上的接口? If so, could you please explain how. 如果是的话,请你解释一下。

Absolutely. 绝对。 You're looking for a feature of .NET called COM-Interop. 您正在寻找一个名为COM-Interop的.NET功能。

http://msdn.microsoft.com/en-us/library/kew41ycz%28v=vs.71%29.aspx http://msdn.microsoft.com/en-us/library/kew41ycz%28v=vs.71%29.aspx

http://msdn.microsoft.com/en-us/magazine/cc163494.aspx http://msdn.microsoft.com/en-us/magazine/cc163494.aspx

The second link has an ATL example. 第二个链接有一个ATL示例。

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 “将.NET Framework组件暴露给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 ) 从本质上讲,它只是将一系列属性应用于您的类和属性,然后在客户端计算机上生成相应的注册表项(.NET有一个工具可以执行此操作 - 请参阅: 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. 我已经为自己需要从VC ++和/或VB6调用的.NET项目多次这样做了。

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/nettocom.aspx < - 您正在尝试做的完美示例。

http://www.codeproject.com/KB/COM/Universal_CCW.aspx http://www.codeproject.com/KB/COM/Universal_CCW.aspx

I've done this exact thing with an MFC-based C++ application in Visual Studio 2008 and a .NET-based C# Windows service. 我在Visual Studio 2008中使用基于MFC的C ++应用程序和基于.NET的C#Windows服务完成了这一切。

First, if you have not created the C# Windows services yet, I've got a couple of tutorials for creating the basic framework. 首先,如果您还没有创建C#Windows服务,我有几个用于创建基本框架的教程。 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. 其次,您需要决定如何在MFC应用程序和Windows服务之间进行通信。 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. 任何类型的进程间通信(IPC)模型都可以工作 - 套接字,管道,共享内存,WCF等。既然你想要迁移到.NET,我建议使用Windows Communication Foundation(WCF),这是方式我做到了。 Specifically, I chose the named pipe aspect of WCF for my communication method based on the chart shown here . 具体来说,我根据此处显示的图表选择了WCF的命名管道方面作为我的通信方法。

If you go down the WCF route, you'll benefit from the fact that the communication between application and service is .NET-based. 如果沿着WCF路线走下去,您将受益于应用程序和服务之间的通信是基于.NET的。 Thus, when you move your application to .NET, the communication mechanism won't have to be rewritten. 因此,当您将应用程序移动到.NET时,不必重写通信机制。 The trick in the meantime is getting your MFC application to use the WCF code. 与此同时,诀窍是让您的MFC应用程序使用WCF代码。 To do this, write the WCF client code in a .NET assembly using C#. 为此,请使用C#在.NET程序集中编写WCF客户端代码。 Then, use a C++ dll to bridge the gap between your MFC code and the .NET assembly. 然后,使用C ++ DLL来弥合MFC代码和.NET程序集之间的差距。 I've got another tutorial with step-by-step instructions for how to do this. 我有另一个教程 ,有关如何执行此操作的分步说明。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM