What is the best way to create a COM component today? - c#

I want to create a deskband COM object for my pet project. I don't have any experience with COM and a quick search revealed that ATL will simplify things. I was wondering if there are any better ways to create a COM component today. Better in the sense less boiler plate, use of C# instead of C++ and any other things you may think of.

If deploying or relying on a .NET framework installation on the client machine is not an issue for you, than C# is much easier than C++ (although you will probably have to redeclare interfaces, IID, etc... in C#, using P/Invoke). If reducing dependencies is an issue, than C++ with ATL is better.

Just create it in C# and expose as a COM component, see this guide:
http://msdn.microsoft.com/en-us/library/zsfww439.aspx

The only reason I would consider C++/ATL is if I were connecting to any C/C++ libraries. Other than than I can't think of a strong reason to use C++ over C# (assuming your skill level is equivalent in both).

I can only recommend C++ in conjunction with Microsoft's ATL library.
I used some code generation tool written in C++ that helped me to get rid of quite a lot of boilerplate code. This tool generated code that produced more C++ friendly interfaces (like the code that gets generated in the tlh/tli files when you #import a type library in Visual Studio. My code generator produces similar code, only for COM servers.
If you are interested, send a mail to DerTopper at web dot de. Put something like "COM code generator" in the subject line, so that you won't fall through my spam filter.
Regards,
Stuart

Related

Up to date solution to expose C# .net assemblies to C++

I have C++ Qt application together with a lot of .net C# assemblies, that I would need to use in the C++. Before doing any research I was expecting there is existing solution to generate C++/CLI wrapper DLL for usage in pure C++ app, but to my surprise I wasn't able to find anything except SharpCpp, but that's just experiment.
As the amount of classes that need to be exposed might be pretty extensive, I was looking for as much as automatic solution possible ( like what I'm used to when wrapping C++ for Python using shiboken)
As far as I can tell my possibilities are (as just compiling the app with /clr is not an option):
Manually write C++/CLI bridge DLLs, but this could take a lot of time.
use COM, but that would also mean a need to implement the COM exposure to all C# assemblies also. Or is there any automated solution?
use CLR Hosting. Not an option without automatic generation of the wrapper also.
try using something like Alter-Native or cs2cpp, but I don't know how much production ready this is.
So I was wondering, if someone was facing similar problem, and what turned out to be the best, and most maintainable solution.

Wrapping code/classes for another program

At the moment, I am working in a C++ environment. There are many in house programs, SDKs, and DLLs. What I want to know is, moving forward, would I be able to wrap that code into something C# could use? I'm still learning about wrapping code for other languages, and haven't found a decent source for going from C++ to C#.
As far as cross-language interop goes, the C#-C++ interop is actually quite usable. Check out the official documentation. "Managed" is Microsoft-speak for everything in the .NET runtime, i.e. your C# code; "unmanaged" or "native" means your C++ code.
Microsoft has extensions to C++ called C++/CLI that make it easier to build wrappers for .NET languages.

Read C# functions in C++ project

Good day,
I want to take an existing C# project and wrap functions into a C++/CLI DLL. I need to be able to read this DLL from VB 6. I choose this route because I won't have to register a .net DLL in order to use it with VB 6. Frankly I have no experience with this kind of thing so I would greatly appreciate a good example. I know there are plenty of similar questions like this but I haven't been able to find anything simple enough for me to understand.
This is a fairly unwise route to pursue, making C# code [ComVisible] is pretty trivial and Regasm.exe should not scare you. The usual mistake with Regasm is to forget to use its /codebase option on your dev machine.
If you insist on not taking advantage of this then you'll need to find a way to get the CLR loaded yourself so it can execute your C++/CLI and C# code. There are three basic ways to do so. You've written off COM interop and hosting the CLR yourself isn't very practical if the host app is VB6. You however can take advantage of the C++/CLI compiler's ability to generate unmanaged DLL entrypoint stubs that load the CLR for you and switches to managed code execution. Do so by writing a static function that you decorate with __declspec(dllexport). The technique is shown in this answer. Beware that it isn't particularly performant and you'll have to live with the restrictions imposed by the VB6 Declare statement.
Also check this post-processing tool that can inject these stubs directly into a C# assembly. Not sure how reliable it is, I haven't used it myself.
Be a man and make that DLL under a couple of different compilation/linking/function-call-protocol configurations. Then go ahead and try a couple of different referencing configurations in your VB6 project. Post what works. Contribute, go forth!
Take a look at Export Managed Code as Unmanaged
and C# Project Template for Unmanaged Exports

Can you run C# Code from c++?

Can you run C# code from c++?
and How?
If you're C++ code is "managed" C++ that's built on the .NET common language run-time (CLR), then it's easy to reference a C# assembly and invoke public classes and methods. If, however, your C++ code is "native" (not built on the CLR), then you'll want to register your C# assembly for COM interop and invoke the COM object from your C++ code. There's an MSDN article that covers all the gory details:
http://msdn.microsoft.com/en-us/library/w29wacsy(VS.80).aspx
There's also a good article on CodeProject by Nick Parker called "Exposing .NET Components to COM" that you may find useful.
You can use unmanaged C++ to run a .NET application, but how difficult it will be will depend on which version of .NET you are using.
When I did this with .NET 2.0 it took me two solid weeks to get it working.
The answer in this page gives guidance as to which programs are needed to do this.
http://www.pcreview.co.uk/forums/thread-1225474.php
The other option that you have, depending on what you're trying to do, is to host the CLR in your application which allows you to more tightly integrate the C# code than is possible by going through COM. You can read an overview of doing this in this MSDN Magazine article.
With C++/CLI, yes (formerly Managed C++ -- which had different extensions to the language).
yes, you can use COM to call .NET
You can call .NET code without COM by using the mscoree.dll ClrCreateManagedInstance Function. You need to supply the assembly qualified name of the type name you want to create, in the pTypeName parameter.
From memory, in some cases you may need to add the ComVisible attribute to the interfaces or classes you wish to access using ClrCreateManagedInstance(). However this does not require you to also register your class or any of the other messy deployment issues that go with COM.
I'm guessing the answer that would really be useful to Chad is:
Yes, it is most definitely technically possible to run C# code from C++. However, if the other answers on this page sound like they are going over your head, then you are nowhere near experienced enough to actually do this. It is pretty difficult to pull off, all things considered, so unless you really need to be running C# code from C++, it might be best to just rewrite the C# code as C++.

Calling C# code from Java?

Does anyone have a good solution for integrating some C# code into a java application?
The code is small, so I could re-write in java, but I would rather reuse the code if possible. Don't repeat yourself, etc.
Also, I know I can expose the C# as a web service or whatever, but it has some security/encryption stuff in there, so I would rather keep it tightly integrated if possible.
Edit: It's going to be on a server-based app, so "downloading" another runtime is irrelevant.
You would use the Java Native Interface to call your C# code compiled into a DLL.
If its a small amount of C#, it would be much easier to port it to Java. If its a lot, this might be a good way to do it.
Here is a highlevel overview of it:
http://en.wikipedia.org/wiki/Java_Native_Interface
Your other option would be to create a COM assembly from the C# code and use J-Interop to invoke it.
http://sourceforge.net/projects/j-interop/
I am author of jni4net, open source intraprocess bridge between JVM and CLR. It's build on top of JNI and PInvoke. No C/C++ code needed. I hope it will help you.
If it's short, I think you're better off re-writing the code in java. Downloading one 50Mb runtime is bad enough.
There is an IL to Java Bytecode compiler GrassHopper which may be of use to you. I've never tried it though.
I'd look at rewriting your code in Java though
EDIT: Note that Grasshopper seems to be no longer available.
We used JNBridge for this, and it worked great. It handles Java->.NET and vice versa, all in-proc.
If you do not want to rewrite hadle it as an Inter-process communication and choose one of following:
Named pipes
Sockets
SOAP
I would rewrite it if it's not too much trouble.
The web service would work, but it seems like that would be a lot of overhead just to reuse a little code.
http://www.infoq.com/articles/in-process-java-net-integration suggests running CLR and JVM in the same process space and passing calls back and forth. It sounds very efficient. I'm going to give it a try and integrate it into Jace if it works well.
If it is a piece of code that is exposable as a command line utility, I just make the other host language use a system call to execute the utility.
If your C# app needs to call Java, compile a special Java main that takes appropriate command line args and returns text output.
It the oldest, simplest method.
You can call your c# classes (compiled in a dll) via a bridging library, various libraries are available, every one with his characteristics. JNBridge generate proxy classes that you can call to manage the code in java classes. JCOBridge let you load your c# classes and use it from java using the invoke mechanism, also javonet let you import java classes and call java code using the invoke mechanism. All the explored solutions are commercial solutions that let you call java code from .NET and vice-versa with graphical user interface integration and other amenities.
Links:
jnbridge java-.NET bridge Developer and Deployment license schema with 30 day free trial
jcobridge java-.NET bridge Developer and Deployment license schema with unlimited Trial
javonet java-.NET bridge Research and Professional license schema with 30-day unlimited Trial after sign-up

Categories

Resources