I've been writing a program in C++ and noticed there is a library in C# that someone else wrote that I would like to link in to my code.... but I'm not sure how to do that. Can someone suggest something? Doubt this matters, but I'm using Windows 7 with MSVC2010.
Thanks in advance!
You can try compiling your C++ program in C++/CLI mode. Then the compiler will produce a .NET executable which can create C# objects and use their methods.
C++/CLI is discussed here: C++/CLI
If you're familiar with COM you could access the .NET library through COM. If the library doesn't provide COM interop out of the box you could write a wrapper around it using C# and expose that through COM.
If you're going to pull in a .NET library you should be aware that it requires a .NET runtime which may take up valuable resources. If you ware building the application in C++ for performance reasons, maybe you're better off porting the parts of the library you need to C++.
Related
I have a c# dll that needs to be called in Java.I see that there is a method using jni to call c++ dlls.How can I do it for a c# dll..Please help..I couldnt find any good material on this
From here:-
IKVM.NET is an implementation of Java for Mono and the Microsoft .NET
Framework. It includes the following components:
A Java Virtual Machine implemented in .NET
A .NET implementation of the Java class libraries
Tools that enable Java and .NET interoperability
You can use Java Native Interface. Or you can create a COM assembly from the C# code and use J-Interop to invoke it.
If you have C# dll sources you need to use maybe the better way will be to translate it to Java using some tools like GrassHopper.
According to GrassHopper key feature explanation it can convert MSIL to Java bite code. So can use without sources of c# dll
Check this: http://www.javonet.com
If you look for quick and easy solution then Javonet should work fine for you. It is light counterpart of IKVM and J-Integra works also as native bridge.
All you have to do is:
add Javonet.jar do your project call
call Javonet.addReference("yourlib.dll")
use your .NET library like it was almost JAVA package
Sample:
NObject obj = Javonet.New("yourDotNetClass");
obj.invoke("YourMethod","arg1", 2);
The syntax is not strongly-typed and works like reflection but gives you fastest access to any custom .NET code, third-party libs or .NET framework as no changes are needed on .NET side. If you need it is also possible to implement custom strongly-typed wrappers.
I do recommend this bridge as in my opinion it is easiest to quickly get things done but also other native bridges are worth checking as this is best approach for such case.
I would avoid going into custom JNI or COM unless you have a lot of time and you just want to learn, if you need quick and reliable solution take one of third-party bridges.
Currently we have couple projects that are written in Delphi 6. Because of specific components that use in these projects (components also written in Delphi 6) it is not easy to convert it in newer version.
As I prefer .NET development and our new products are developed in .NET, I would like to develop new functionalities using these technologies. C# will be programming language.
My question is: How to integrate new functionalities developed in C# with current code in Delphi? Is this good idea at all and what can be possible issues? If someone have similar experience it would be to hear advantages and disadvantages.
I heard for integration in way to develop .dll with C# and use it from Delphi code.
TnX in advance!
Nemanja
You can use COM (ActiveX) both ways.
So Yes, you can make a DLL in C# and mark it as COM-visible and import it into Delphi.
But you cannot use simple (not COM) DLLs this way.
My first port of call would probably be looking into WCF (written in C#) and have Delphi talk to it.
The dll is not a bad idea, but I just think putting it in WCF is more scalable + portable.
COM would probably be my choice. However, if for some reason you wanted to avoid COM you could take a look at the very nifty Unmanaged Exports by Robert Giesecke.
Unmanaged Exports is an MSBuild task that essentially allows you to export static functions from your .Net assemblies to be consumed as ordinary native DLL exports.
You may want to check out Hydra from RemObjects. It permits native and managed code to be used in the same application.
http://www.remobjects.com/hydra/default.aspx
If you had to decide between C# and Managed C++, which would you choose and why?
Are there benefits of Managed C++ over C#? Which language do you prefer? What decisions would you make under what circumstances?
I would use managed C++ if I:
Needed to integrate with existing C/C++ code
Needed to port existing C/C++ code to .net
Needed to use .NET objects from C++
Needed to expose .NET object over COM in a more complex way than what .net makes easy
Needed to access hardware directly
Needed to call lots of unmanaged APIs
And already had some skills in C++, as the above tasks will need a experienced C++ programmer. Most of the time I would only consider managed C++ if the company already had a C++ code base, otherwise who is going to maintain the managed C++ code.
In other cases I would always choose C#. Even if I choose managed C++ for part of the system, I would try to write as much of the system as possible in C#.
Think of manage C++ as a bridge building kit for going between the
unmanaged world of C/C++ and the
managed world of .NET.
If you only need to call a few simple APIs from C#, then see pinvoke.net and this overview to find how to call them from C#, as a few lines of complex pinvoke code (prebuilt bridge) in C# is normally better then introducing C++ to a project that is not already using it.
what is the benefit of managed C++
over C#?
C++.net is useful for interacting with C++ and C code (that is, calling external C or C++ libraries, providing callback functions to external modules written in C or C++ and so on.
what language of both would you
prefer?
I would prefer C# for all situations except the one described above (interacting with C and C++).
C# is easier to write, simpler, and geared specifically to use the .NET platform. C++ can do it also, but it has all the complexity of the C++ language plus the extensions needed to use the .NET platform.
Unless you need to interact with native C++ or C code, you're better off using C# in most cases (that is, if you're coding for the .NET platform).
Normally I prefer C++, but when needing to code for .NET, it doesn't beat C#.
Managed C++ is good for interop with C++: for example, if you have a .NET application and your assembly has to interact with a native interface that comes as C++ .lib files (which I had more than once), or with a nice C++ API.
Example: Rithmic (not that you ever heard of them) until recently ONLY supported a C++ API. If you try to access them from C# - good luck ;) Managed C++ allows me to access their API and expose nice .NET objects.
Basically interop. Managed C++ REALLY shines in interop with low level C / C++ API's.
I used managed C++ when I needed to build up new NET component with much ofC++ unmanaged code inside.
I did a specific class used to Marshall some objects forward and back from old C++ code.
I've encountered a problem which was transparent in managed C++, but made a big headache in C# - I had to send a callback function to a C++ unmanaged library, that defined this callback as __cdecl. In C#, the default calling convention is __stdcall, and it is pretty compilcated to move a C# delegate from __stdcall to __cdecl (Actually it requires either a 'shim' unmanaged DLL to do so or using some reflection classes).
In C++, (C++.Net as well), it is just a simple attribute.
I haven't personally written, or read for that matter, too many lines of code in managed C++, but from what I have seen it looks too convoluted and patchy. That said, you might want to use managed C++ if you are really good in C++, and when learning the idioms and patterns of a new language would be too much of a risk.
Use C# if you are quite competent in it. If you are only getting started with C++ and C#, I think, C# is the easier route to take.
I would prefer C#, or specifically .NET, over C++ because of the extensive .NET standard library.
I know this is a strange question but the idea is simple: I prefer C# syntax rather than C++:
-Setters and getters directly inside a property
-interfaces
-foreach statement
-possibility to declare an implicit cast operator
other small things...
What I really don't know is if is possible to import a c++ dll (expecially std libraries) in C# if I don't use any namespace (even System)
The idea is just to write a program using everything that you will normally use in C++ (nothing from CLR so), even printf for example
Thanks for any answer
No it is not possible to simply import existing C or C++ files into a C# project. They are very different languages and cannot be mixed at the source level.
The way to mix C# and C++/C applications is at the PInvoke or COM Interop level. This works by duplicating the signatures in C# and allowing the C# compiler to determine the binary layout of the native type in order to marshal between the languages.
There's now something close to this
.NET Native compiles C# to native machine code that performs like C++. You will continue to benefit from the productivity and familiarity of the .NET Framework with the great performance of native code.
It's for Windows Store apps only (desktop apps may come in the future):
Desktop apps are a very important part of our strategy. Initially, we are focusing on Windows Store apps with .NET Native. In the longer term we will continue to improve native compilation for all .NET applications.
And
apps will get deployed on end-user devices as fully self-contained natively compiled code (when .NET Native enters production), and will not have a dependency on the .NET Framework on the target device/machine
No; this is not directly possible. In particular, C++ templates are not supported by C#.
In my project I got a device which comes with C++ Sample codes. The codes are ok and the device is working as expected.
But I need it to talk with my C# interface because all other devices are currently using C# interface.
So I am planning to create a DLL Wrapper for the driver. I will create a C++ Library of my own (from source code with proper interface) and Call this C++ Library from C# using DLLImport (just call my interfaces there.).
I am pretty sure it can be done this way, but I have never created a C++ Library and used it from C# yet. So, can anyone refer me to some tutorial that goes with my problem?
I am using C++/C# int VS.NET 2008.
Regards,
Maksud
Have a look at
using a class defined in a c++ dll in c# code
Another useful tool you have at your disposal is C++ CLI.
You can use C++ CLI to create an intermediate library - one that exposes managed classes but runs unmanaged C++ code. You can actually mix managed and unmanaged C++ in the same DLL.
The unmanaged portion can accesses the unmanaged DLLs without having to use the PInvoke functions.
Your C# code can access the managed classes in this intermediate library.
Depending on the DLL and what you need to do you may not need to create a wrapper directly. You might be able to get away with P/Invoke for the functions. You will need to evaluate your specific needs and what is already available in the libraries/code provided.
For anyone who comes to this question and are looking for answers, you may want to try xInterop NGen++ , a C# wrapper generator for native C++ DLL, which has been just released to the public, the tool can generate C# wrapper for native C++ DLL automatically and instantly by using advanced P/Invoke technologies. Check out the current version and a free version will be out soon.
(I am the author of the tool)