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.
Related
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.
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.
I have perhaps a silly question:
We have a VC++ COM DLL (developed internally) and we have ported our main app to C# and used COM Interop, reg-free to access the COM dll. Everything works just fine with internal embedded manifest.
However, the friendly product-dev/marketing/sales want to minimize the package and include the COM dll directly. Somehow, someone became convinced that the app distro should include the exe only (since it's unmanaged we can't just ILMerge it in).
Since we have the tlb/lib of the COM, could we somehow statically link it, without porting the whole COM to C# managed re-work?
Thank you
P.S. Pardon my wording: the company was downsized and I am the Python guy who had to learn everything .NET in the last week or so since now I am doing my job and the job of 2 ex-senior .net developers
It looks like Costura can more or less do this.
https://github.com/Fody/Costura
It specifically has support for merging unmanaged assemblies (ie a C++ DLL) into a .NET assembly.
Note - this is not true static linking but would achieve the aim of packaging everything in the single EXE to be distributed.
It is possible to include the source for the COM DLL into the project for the exe, or you could change the COM DLL project into a static lib project. Once you've accomplished that, you must modify the code to create the COM objects directly. All said, neither options are particularly easy.
Alternatively you could look into products like Spoon Studio that would allow you to wrap your exe and COM DLL into one exe without any code.
I have a python project that calls a c++ wrapper dll that calls a c# com interop dll.
In my computer, with all frameworks and programs installed, my project runs very well.
But in a computer that just got formatted it doesn't.
I allready installed c++ 2008 redistribute and the c++ part is working but when I call a function from it (that will call the c# correspondent one), it gives an error.
I want to know what are the dll dependencies from both c++ and c# dll's to see what is missing :)
Looks like you need Dependency Walker.
Dependency Walker (a.k.a. depends.exe) works for both native DLLs and managed DLLs.
It is included in some Visual Studio versions, and can also be downloaded here.
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