Programming Language for Creating DLL: C++ or C# - c#

This is NOT a Programming doubt!
I am going to write a DLL for some application.
I have two options to choose from: C++ or C# In which language I should write DLL?
Does that affects functionality?
I am a completely newbie and Unaware of both C++ and C# (but Some Small programs in C#).
What are Pros and Cons about Writting DLL in C++ or C#?
Thank You very much for your time!
Regards,
Swanand!

A DLL is best written in C :)
Let me explain:
DLL's were conceived before C++ came into mainstream use. They were created for the C language. You can write DLL's with C++ but you'll be able to easily use them only from applications that were written with the same version of the same compiler as the DLL. A C DLL can be used from .NET, unlike C++ (yeah, I know, technically it can, but it is a pain in the buttocks).
If you create DLL with C#(or any other .NET language), it's a completely other thing - it's not a windows DLL, it's just a .Net assembly without an entry point(Main), so it can be used from other .NET assemblies by referencing the DLL.
To summarize:
If you need to use your DLL from .NET languages - write it in C#, it won't be a windows dll, just an assembly. Very easy to use.
If you need to use your DLL from ONLY C++ and ONLY from applications written by the same compiler, write in C++. Not portable, easy to use.
If you want to create a general-purpose library that can be used from .NET, C, C++ and regardless of the compiler, use C, or C++ freestanding functions marked as extern "C" and having C-like parameters, like pointers and POD's.
HTH

It will depend upon your target application. If you are writing Win32 app, then C++ may be wise choice. If you are developing a reusable library in .NET chose C#.

When you say C++ are you referring to the Standard C++ or the "Managed" version?
If you are referring to the latter then you are no worse off than writing in C# as Managed C++ is an alternative .NET language, and actually I think you have more functionality available, although it is not as simple a language to write in as C#.

Pros and cons dont change for a library if you mean managed c++. But for the coding, ease of use and available libraries matters.
I would suggest c# since you say you are newbie. Its much more easy and you have LOTS of sources online.
But if you plan to use some native code and need CLR support then c++ is the only choice.
Good luck

Related

Dllimport/PInvoke binary compatibility

I'm learning about PInvoke to use a C++ library (with C-Style interface) in C#. After reading the documentation and searching Google/StackOverflow for additional information I was wondering about binary compatibility of the native library and .Net. I think I read something somewhere a while ago, but I couldn't find it anymore. And I could not find anything else on this.
When I compile a C/C++ to use in .Net, do I need to use certain configurations like compiler flags to make it compatible for PInvoke? Can I use native libraries made with different compilers in one .Net project, provided these libraries don't depend on each other?
I there anything else I need to know about C/C++ compilation for .Net PInvoke?
I would like to use GCC and CLang with CMake projects to create the native libraries.
P/Invoke has a lot of adaptation capabilities. Also P/Invoke is cross platform (with .NET Core).
However, it's not binary compatible with C/C++, but if a piece of C/C++ code can be used by P/Invoke, it's not dependent on the C/C++ compiler (MSVC or other), or said in another way, it will not be able to use any C++ construct, but it will see all C++ compilers as equal citizens

Using C# dll in C++ code

I need to integrate this C# dll
in my C++ code. I want to call some functions written in C# from dll and the rest of code write in C++. What is the easiest and quickest way to do it? The program will be executed only on Windows.
There are basically two cases to call a .NET DLL from unmanaged code:
The .NET DLL exposes a COM interface. In this case, you can use COM from your C++ code.
The .NET DLL does not expose a COM interface. In this case, you have two possibilities (to make it simple):
2.a. host the CLR as described here: Loading the Common Language Runtime into a Process
2.b. write a piece of managed C++ code (another DLL - written in C++/CLI) to wrap the .NET DLL and expose 'old way' DLL exports to unmanaged clients.
I don't specifically know the sharpbox system, but it looks like it's pure .NET and does not expose COM interfaces, so 2.b might be the best way to do it (not so easy...). Maybe it has a REST/Web easier API you could use.
PS: you can also add exports to a .NET DLL. This is described here: Is is possible to export functions from a C# DLL like in VS C++? but it's kinda hacky.

I have a c sharp dll. How can I make it usable in other languages, such as PHP or Delphi?

I have a dll developed in C Sharp. How can I make it usable in other languages, such as PHP or Delphi?
I cannot find any solution to this problem yet. Isn't there any easy way of doing this?
If Delphi can understand COM, then you can make a COM wrapper around the .NET DLL.
I am doing this now for an older IDE which doesn't understand .NET, but it understands COM so I am able to use the latest .NET features in my older applications.
In addition to COM, you could try these (from my Delphi Win32 to WCF answer) for Delphi Win32:
Use Delphi Prism with the UnmanagedExport attribute to create a .NET wrapper that you can call from native Delphi Win32
Use Managed VCL to do .NET interop from native Delphi Win32
From another Stackoverflow question: create a C++ DLL crossing the bridge this used "mixed mode C++"
Use RemObjects Hydra on both the C# or Delphi Prism and native Delphi Win32 side (RemObjects wrote Delphi Prism, so this works like a charm)
The last one might be the real one you are after.
For PHP it is a different story;
PHP might run on a non-Windows box,
there are .NET implementations of PHP that would allow for direct linking of your C# assembly DLL
--jeroen

How to create native DLL in Visual Studio from C# code?

I have the source code of a C# program. I want to create a DLL out of it which I want to use in C++.
Is it possible to create a native DLL in Visual Studio 2008 which can be used in C++?
native <-> .Net interop is one of my pet disciplines, which is why I needed this as straightforward and reliable as possible.
The result was that I made me an MSBuild task which I just need to drag into a project to allow me to export static methods from pretty much any .Net language. And since the whole marshalling infrastructure works for exports as well, you can do pretty much anything with it that you want (like passing native/managed objects as IUnknown).
The resulting assembly will look to the consuming process like a real DLL, which means you can't have it to scale up to 64bit automatically anymore.
However, since you do have native bits in your application, you already have this issue anyways. ;-)
When you do not specifiy the CPU target in your C# project, my task will emit a warning that it created a folder for all targets (x86,x64 and Itanium), and there you'll have *.dll and *.pdb for each platform.
If you want the program to be native, and not managed, you'll need to port it to C++, instead of using C#.
That being said, you can compile it in C# into a library, and use it from C++ by using C++/CLI. This just requires that you compile the files that use the C# library with the /clr flag. This provides C++ access to the .NET framework, and lets you use libraries made in C# directly from C++.
Alternatively, you can use .NET's COM interop to expose the C# class(es) as COM objects, and then use those from native C++.
It is possible in Visual Studio 2008, but you're not going to be able to write it using C#.
To create a native DLL, you'll have to write your code using one of the unmanaged C++ project types.
You can expose the DLL to COM. Have a look here for some examples.
yes you can.
you need to create second project.
the project must be unmanaged (like "visual c++"->class library).
the name of this procedure is "calling from unmanaged code to managed code".
good to read unmanaged to managed (codeproject)
you must be aware, that any computer that using your dll must have preinstalled DotNet and Visual C++ Redistributable Package

Using Mono for developing in C++

I am starting to use Mono to develop applications in C# and C++. I wanted to ask you, how is Mono compiling the C++ code? is it using GCC? It is amazing to see that it has the STL containers... Also, can I use the Boost libraries and GSL libraries with Mono? Thanks in advance!!!
I think you must be using MonoDevelop, the IDE, as opposed to Mono itself.
Yes, MonoDevelop uses gcc/g++ to compile C/C++ source code, but it is not compiled to CIL - it is compiled to a native binary.
If I am understanding correctly, then you should be able to use boost just fine.
If, however, you are asking if Mono has support for Mixed-Mode assemblies or executables (e.g. assemblies/exe's that contain both native and .NET CIL), then I am sorry to inform you that this feature is not supported, nor is compiling C++ to pure CIL by Mono.
As long as you don't need mixed mode (i.e., forget the native part and go for CIL-only), mono does work with C++ code (I hear they're now experimentally supporting mixed mode, on Windows especially, and elsewhere via wine, but I think that part's NOT ready for prime time). The one well-supported C++ compiler at this time is Microsoft C++/CLI on Net 2.x frameworks; efforts have been underway (for many years now) to add gcc, but I don't know of any production-ready result so far:-(.

Categories