Can I use managed C# DLL in unmanaged Delphi EXE? Or the only way is to have managed DLL and EXE or unmanaged DLL and EXE?
There are quite a few options, including at least the following:
Expose the classes that you need via COM which can be readily consumed by Delphi.
Create a mixed mode C++/CLI wrapper that uses the C# DLL as a reference. That mixed mode DLL can export native functions and you can then consume that DLL using Delphi external. That is, you consume the DLL just in the same way as Windows system DLLs are consumed.
Use Robert Giesecke's UnmanagedExports.
Use the CLR's native API to consume the C# DLL directly. This option is probably the least attractive since it's the hardest to get right.
Related
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.
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 learned not long ago that C++/CLI is the way to go to use unmanaged C++ code. I was able to figure out how to call code from a C++ project in a C++/CLI project with the C++ code being in a static library. The C++ code is in a static library and the C++/CLI code in in a Win32 application.
Then I wanted to transform the C++/CLI project into a static library (from now on known as wrapper) and create another "consumer" project (either C++/CLI or C#) to call on the wrapper. This is the place where I'm stuck.
In C#, linking project is so easy. All you need is to add a reference from the consumer project to the library project. How do you do that from either a C++/CLI or C# project to a C++/CLI project?
Okay, so that is the main question. My final goal is to be able to go from C# to C++/CLI to C++ freely, either using DLLs or static libraries, but I've got to solve my currently problem first.
In C# you can't add a reference to a C++/CLI static library (.lib) because .libs are C++-specific. Instead you need to create the C++/CLI project as a Class Library project which compiles into a .NET assembly (a DLL). The resulting DLL can be added as a reference from the C# project.
One more thing, I don't recommend using C++/CLI to create wrappers for unmanaged C++ classes. In those cases it's better to turn the classes into COM classes (using ATL). Since .NET is backwards- and forwards-compatible with COM, COM classes can be used from C# directly without requiring you to create any wrappers. C++/CLI is good for when you need to mix managed and unmanaged code in the same place, but whenever you find yourself creating wrappers, you should consider using COM instead.
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 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