COM Interop with IPC in C#

你离开我真会死。 提交于 2020-01-16 00:26:40

问题


I have a question about IPC with COM Interop objects. I want to create and fill with data COM Interop object in one process (for example Windows Service written in C#). Then I want to get it in another process (console application in C#). I know that COM objects don't serialize and they are unmanaged code. I tried realize solution by .Net Remoting (Windows Service is server, console application is client), but I couldn't get COM Interop object in client (I made .net class inherited from MarshalByRef in shared library, created public property with COM Interop object). Can anyone tell me, what can I do?


回答1:


COM objects are not serializable as such, but when writing a COM object, you're supposed to use sophisticated proxy / stub definition - althouh in general, undercover, you don't really have to think about it, especially if these objects are written using some framework (ATL, delphi, VB6, .NET, etc.).

These proxies / stubs will at compile time or runtime use powerful RPC mechanisms (which is more or less "DCOM") if the client-server communication works out-of-process or across threads (COM "apartments" in fact).

All this to say that if your COM objects are in-process servers and were correctly built, COM provides free services to host them as "COM+ applications".

By creating logical groups of COM components as COM+ applications, you can take advantage of the following benefits of COM+: Component dynamic-link libraries (DLLs) loaded into processes (DLLHost.exe) on demand and Managed (nothing to do with .NET here) server processes to host components.

If you put your object in a COM+ Application, then you should be able to use it from .NET code just like it was in-process, but now hosted in a special "surrogate" system-provided host process (which happens to be the famous dllhost.exe). You can even configure it to become a full Windows service as shown here in the "Activation" tab of the application configuration:




回答2:


You shouldn't use COM if both sides are .NET applications. It's like buying an expensive modern car, then pulling it by a horse!

First, You can use Remoting. It is easy to implement though not supported anymore and I'm advising not to use it. Anyway, remoting does not need COM Interops. Here is a simple 'Hello World' using remoting: http://msdn.microsoft.com/en-us/library/ms973864.aspx

You can also use WCF (windows Communication Foundation). This one is far more advanced and more suitable for modern applications. This is a Hello World for with WFC: http://www.codeproject.com/Articles/97204/Implementing-a-Basic-Hello-World-WCF-Service

and this one: http://blogs.msdn.com/b/jmeier/archive/2007/10/15/how-to-create-a-hello-world-wcf-service-using-visual-studio.aspx

Both technologies support IPC, TCP and HTTP



来源:https://stackoverflow.com/questions/24724784/com-interop-with-ipc-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!