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

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

Related

How can I call a C# DLL in C++? [duplicate]

I have a native C++ DLL, and I want to import a C# DLL and use some of its functions (for example connecting to a database).
Now I have read that you can turn the DLL into a TLB COM file, and I have done that no problems. My problem lies with the C++. To call that TLB file I need to set CLR support. I use themida to help secure all my DLL's as well as PEC. They do not support .net DLL's and when I enable CLR the programs recognize it as a .net DLL.
Is there anyway to call a c# function inside a .net DLL from inside a native C++ DLL without enabling CLR?
The best way in my opinion is to crate a C++/CLI lib that you can use to communicate between the layers.
This 3rd party library will be compiled with /clr enabled.
Then you can use the header definitions to the proper c++ function wrappers to call the C# functions.
C++/CLI can help you manage the call just the way you want it, and let you design the type casts the way you want them.

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.

How can I make a Win32 DLL that is reference-able in a C# application?

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

Statically link COM DLL in C# (avoid regfree)

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.

Categories