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
Related
I have a rather large legacy nmake (Win32) project that creates a static library from native C++ code. I need to use this library in a C#/.Net application. In the past after much effort I had been successful at wrapping the static library in a managed C++ library, which I am then able to reference in a C#/.Net application. However, after receiving updates from the developers of the nmake project, and having gone through an many upgrades on my own build machine in the meantime, it is no longer working.
I am however able to import the cpp and header files of the nmake project and build it to a Win32 static library in VS 2010, by setting all of the preprocessor constants in the build properties. I set the build configuration type to DLL, and then try to add a reference to the Win32 output in my C#/.Net application hoping to use P/Invoke down the road, and it fails with a message "A reference to MyLibrary could not be added."
Is there a way to build the Win32 library so that it can be referenced by the C#/.Net project and so that I can use P/Invoke?
Is there a way to build the Win32 library so that it can be referenced by the C#/.Net project and so that I can use P/Invoke?
If you want to directly reference the library, you'll need to build a C++/CLI project using your library, and make managed wrappers.
If you want to use P/Invoke (which is a separate concept), you can make exports with a C API, and call those directly via P/Invoke.
The two approaches are both valid, but completely different in terms of implementation (C++/CLI vs. C API wrappers) on the native side, as well as used differently (directly referenced for C++/CLI vs. P/Invoke via [DllImport]).
You can use SWIG to generate wrappers for your code. SWIG is a very powerful tool and worth taking the time learn. It creates wrappers for a number of languages including Python, C#, and Java so if you get it working with one language it is fairly easy to use in other languages as well. It will generate all the wrapper code for you, although you will probably need to do some work with type. You use swig to create a special DLL that SWIG generates code for and then used supplied C# code to access the DLL without needing to deal with managed C++ assemblies which can be a nightmare to deal with.
http://www.swig.org/Doc2.0/SWIGDocumentation.html
Edit: my explanation may not be that clear and the docs are pretty overwhelming, take a look at the "What is swig?" section here to get started:
http://www.swig.org/Doc2.0/SWIGDocumentation.html#Introduction_nn2
My 3D graphics software, written in C# using SlimDX, does a lot of vector operations on the CPU. (In this specific situation, it is not possible to offload the work to the GPU).
How can I make my vector math faster? So far, I have found these approaches:
Run on Mono instead of Microsoft .NET, because they have SIMD support. Not an option for this project.
SlimGen, a project that injects high-performance maths code at runtime. Unfortunately, the project is not in a usable state yet.
Write a DLL in C++ using a compiler that utilizes SSE instructions. Interop with that DLL from C#.
Are there any other options to accomplish faster vector math in .NET?
Write a DLL using Microsoft Visual C++'s compiler. Use standard C++ with SSE intrinsics and/or OpenMP for the heavy numeric code, with #pragma unmanaged. Use #pragma managed to define a clean C++/CLI API which C# can use.
C++ interop is quite a bit faster than p/invoke. And C++/CLI is the only elegant way to deal with both garbage collected memory and the assumptions of native functions (that memory blocks won't move).
You might find that moving some of the OpenGL calls to C++, and using the C++-allocated memory buffers directly for loading VBOs, etc. also gives a big performance win.
Microsoft just announced support for generating vectorized instructions in their .NET Native compiler thanks to back-end C++ compiler optimizations, and more importantly native support for SIMD vector types in the most recent version of their JIT ("RyuJIT"). See some samples here.
Latest developments in .NET include a SIMD dedicated vector/matrix library called System.Numerics.Vector:
Using System.Numerics.Vector for Graphics Programming
This will be enabled as soon as the new JIT compiler "RyuJIT" will be the default, the announcement is here:
RyuJIT: The next-generation JIT compiler for .NET
So very soon (hopefully 2015) we will have very fast SIMD vectors in .NET without any programming overhead.
If you're in the mood to write assembly code in C#, another option is the NAsmJit project, which is a port of AsmJit to C#. I haven't updated it to reflect the latest changes in that project, but much of the support was quite usable at last check.
Mixing .NET with native code. In this case you will need to have one release for x86 and another for x64. You can see Mixed (Native and Managed) Assemblies [MSDN]
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
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:-(.
I am a C# .NET programmer and am learning Python. I have downloaded IronPython, and know that it can call into .NET libraries.
I'm wondering whether there is a way to do the reverse, that is to call into some existing "classic" Python libraries in my C# code, maybe using .NET Interop.
I'd like to be able to access functions in libraries such as pygame.
Ironpython 2.0 is CPython 2.5 compatible, so pure Python that uses <=2.5 APIs should work fine under Ironpython. I believe Ironpython code can then be compiled into a DLL.
For C-extensions like Pygame, you might want to take a look at Ironclad. It's a project to allow for C-extensions to be used within Ironpython. This may also give you the native code bridge you're looking for.
You can use Python for .Net, which allows you to 'use CLR services and continue to use existing Python code and C-based extensions while maintaining native execution speeds for Python code.'
Further, 'A key goal for this project has been that Python for .NET should "work just the way you'd expect in Python", except for cases that are .NET specific (in which case the goal is to work "just the way you'd expect in C#"). In addition, with the IronPython project gaining traction, it is my goal that code written for IronPython run without modification under Python for .NET.'
Hope this helps