using c++ wrapper in c# .NET project - c#

I'm using the C++ wrapper in this link:
https://github.com/TekRTSA/RSA_API
In order to use a driver written in C++ inside C# project.
When I publish/release my C# project it doesn't work on other computers without Visual C++.
Is there any way that I can publish my project on computers that don't have the visual C++? Or is there any software package like .NET that I can use in order to make it work?

With .NET applications you need to make sure customers have a version of .NET that matches what you compiled your application with.
With (Microsoft) C++ applications that depend on the C Runtime, you need to make sure customers have a version of the Visual C++ Redistributables that matches what you compiled your application with. Do a google search for 'visual c++ redistributable' and you will see lots of suggestions on what to look for.

Related

C++ in VS2010 running a managed C# DLL targeting .NET 4.5

I have a C++ project, created in Visual Studio 2010, which needs to execute code from a C# DLL. I have done so using the
#import "pathname\filename.tlb" raw_interfaces_only
directive, as well as the CoInitialize(NULL) and CoUninitialize() COM calls.
The C# DLL was compiled in VS2012, and targeted .NET 4.5.
I was able to compile and run the project, and it seemed as though everything was fine, but we were noticing differences in some function calls. This was verified by loading the exact same C++ project into VS 2012, turning on /clr in Configuration Properties, and changing the Targeted Framework (of the C++ project) to v4.5 by modifying the .vcxproj file in Notepad.
So, I guess my questions are:
Why does a C++ project need to target a .NET framework? Shouldn't the C# DLL be able to handle that by itself? (Answered by Hans Passant)
Why is it possible to run a C#.NET v4.5 DLL through C++ with /clr off, and not have any warnings that it might not work? (Answered by Hans Passant)
Why do I even get results back from the function call? How is it possible that the C# DLL "works" (returns coherent results) at all? Why does it return different values when targeting .NET v4.0?
Is it possible to use the C# DLL from the C++ code, while still using VS 2010? Or do I need to switch the project to VS 2012?
Thanks in advance.
A native C++ project does not target the .NET Framework at all. The project creation wizard is a bit clumsy, it does not hide the combobox when you create the C++ project.
What framework version the C# DLL needs is established when you register it. Either done automatically when you tick the "Register for COM interop" build setting or explicitly by running Regasm.exe.
That native C++ code can call C# code without trouble is just a goody you get from using COM. Whose basic purpose is to provide the contracts and the glue to allow one language to call code written in another. The CLR has very good COM support and makes it look very easy. That's not the case for C++, writing COM client code is generally unpleasant due to the verbosity and it is pretty easy to make mistakes. Using raw_interfaces_only is a mistake, you don't get the benefit of memory management with smart pointers and error handling with exceptions.

Calling managed code (C#/Visual Basic VS2010) from an unmanagened project (C/C++ VS2005)

Short Version:
Have: DLL's Managed Code (C#/Visual Basic) from Brüel & Kjær SDK
Need: Communicate with the DLL's in our old Project Un-Managed Code (C++ Visual Studio 2005)
Long Version:
We have a project written in C/C++ (Visual Studio 2005). Now I have to implement a communication with a new device. (Brüel & Kjær 2250SDK Noice).
The problem is, Brüel & Kjær only supports you with Libraries for C# or Visual Basic (Managed-Code) (Visual Studio 2010 and higher), but our project is an old unmanaged C/C++ code.
So, the question is, how can I work with the DLL's in my old C++ Code?
I don't have the source of the DLL's, I only have the DLLs.
I hope someone out there can help me with that problem. Thanks in advance!
What you want is probably C++/CLI (Common Language Infrastructure). It basically enables you to use .NET types in C++. With this you could call a C# DLL and use the data which is provided by the DLL as the .NET type. Since you already have you application in C++ code, I guess you don't want to rewrite it completely. You'd than have to convert the managed types to unmanaged types, which is possible with C++/CLI.
If you want to know more about it, here is a lengthy MSDN article about it and here is a useful little quick tutorial.

NumPy/SciPy in c# with Visual Studio 2008

I want to access the NumPy/SciPy libraries from c#.
According to this site, "The IronPython ports of NumPy and SciPy are full .NET ports and include custom C#/C interfaces to a common native C core. This means that the full functionality is available not only to IronPython but to all .NET languages such as C# or F# by directly accessing the C# interface objects or sometimes by evaluating IronPython expressions from other .NET languages."
According to the instructions for setting up skipy on .net the first step in installing skipy is "Download and install IronPython 2.7, this will require .NET v4.0.".
I am not adverse to installing net4.0, but for development I am currently stuck on Visual studio 2008, which only supports net3.5.
I searched for instructions to get an ironpython version for net3.5, but that approach seems dubious. Is there a way to use the net4.0 version of ironpython for this purpose? (I do not require ironpython working in visual studio 2008, I only want to access the numpy libraries from c#)
Or how hard would it be to build ironpython as well as NumPy/ScyPy for net 3.5? I have to confess that so far I have not even be able to find any hint that the NumPy visual studio port was ever successfully built under vs2008.
What would be the recommended way to get NumPy/SciPy classes in a visual studio 2008 project?
Has anyone successfully done this?

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

Calling C# from native C++, without /clr or COM?

I have a class library written in C#, and I want to call it from a legacy native C++ application. The host application is truly native, compiled on Windows and Linux, and it’s a console application. So how can I make it call the C# class library, assuming using Microsoft .NET on Windows, and Mono on Linux?
I have looked at SWIG and wrapping with COM interfaces on Windows, but is there a standard recognized solution that works cross platform? I.e., that is generic, works with both Microsoft .NET and Mono. A write-once-use-everywhere implementation.
Solutions should expose the full class interfaces from the C# domain to the C++ domain.
Similar questions focus only on the Windows solutions, for example -
Call C# methods from C++ without using COM
If you want to do this cross platform, I would recommend going with a 100% Mono approach.
Mono has a clean Embedding API which works on Linux and Windows.
With .NET 5.0 (the successor of .NET Core) this is now possible to call C# from C++ in a cross-platform way without using Mono. Please see the solution explained in this GitHub issue using DNNE to generate a shared library and GCHandles to access C# objects.
With this you get a shared library that can be used from C or C++. Note that this will give a C-like API (no objects, like when using extern C in C++), in the future there may be tools like SWIG for C++ to overcome this limitation.

Categories