How to use Unmanaged Exports in Delphi - c#

I'm making a project in C# and I want to use Unmanaged Exports and later to use it in Delphi. So can anyone explain me, how does Unmanaged Exports works, then how to use/import in Delphi. I'm using Visual Studio 2010 Express, on Windows 7 64 bit

The best description I've heard for unmanaged exports is "reverse PInvoke". These are limited to scalar types, but let you consume managed assemblies from native code without the overhead of COM Interop.
I haven't tried it, but you can do this in C# with a little tweaking.
As an alternative, Delphi Prism supports this functionality out of the box.

Brian Long wrote the classic article on Inverse P/Invoke to call managed C# code from Delphi Win32.
I'm not sure I'd use this kind of technology, but it is possible :-)
--jeroen

Related

C# and Delphi integration

Currently we have couple projects that are written in Delphi 6. Because of specific components that use in these projects (components also written in Delphi 6) it is not easy to convert it in newer version.
As I prefer .NET development and our new products are developed in .NET, I would like to develop new functionalities using these technologies. C# will be programming language.
My question is: How to integrate new functionalities developed in C# with current code in Delphi? Is this good idea at all and what can be possible issues? If someone have similar experience it would be to hear advantages and disadvantages.
I heard for integration in way to develop .dll with C# and use it from Delphi code.
TnX in advance!
Nemanja
You can use COM (ActiveX) both ways.
So Yes, you can make a DLL in C# and mark it as COM-visible and import it into Delphi.
But you cannot use simple (not COM) DLLs this way.
My first port of call would probably be looking into WCF (written in C#) and have Delphi talk to it.
The dll is not a bad idea, but I just think putting it in WCF is more scalable + portable.
COM would probably be my choice. However, if for some reason you wanted to avoid COM you could take a look at the very nifty Unmanaged Exports by Robert Giesecke.
Unmanaged Exports is an MSBuild task that essentially allows you to export static functions from your .Net assemblies to be consumed as ordinary native DLL exports.
You may want to check out Hydra from RemObjects. It permits native and managed code to be used in the same application.
http://www.remobjects.com/hydra/default.aspx

Calling Unmanaged C++ code from C#

I have zero experience in C++, but have a few years C# experience.
Are there an examples out there showing how I can create a method in a C++ program which is then called from a C# program using the DLL?
Cheers
Using p/invoke, you can call C++ code from C#.
Read this: Calling Win32 DLLs in C# with P/Invoke
Another small yet good article : Using P/Invoke to Access Win32 APIs
--
EDIT:
This aritcle explains how to create a DLL library in C and then use it with C#
You can also try compiling the C++ code in Visual Studio to VC++, which is plain old .NET. Avoid the p/invoke, if the code is compatible.
Create some function in C++ with "extern "C"" to avoid the c++ name mangling and then use PInvoke as suggested by Nawaz is the better way.

Should I use Managed C++ or C# for my application?

If you had to decide between C# and Managed C++, which would you choose and why?
Are there benefits of Managed C++ over C#? Which language do you prefer? What decisions would you make under what circumstances?
I would use managed C++ if I:
Needed to integrate with existing C/C++ code
Needed to port existing C/C++ code to .net
Needed to use .NET objects from C++
Needed to expose .NET object over COM in a more complex way than what .net makes easy
Needed to access hardware directly
Needed to call lots of unmanaged APIs
And already had some skills in C++, as the above tasks will need a experienced C++ programmer. Most of the time I would only consider managed C++ if the company already had a C++ code base, otherwise who is going to maintain the managed C++ code.
In other cases I would always choose C#. Even if I choose managed C++ for part of the system, I would try to write as much of the system as possible in C#.
Think of manage C++ as a bridge building kit for going between the
unmanaged world of C/C++ and the
managed world of .NET.
If you only need to call a few simple APIs from C#, then see pinvoke.net and this overview to find how to call them from C#, as a few lines of complex pinvoke code (prebuilt bridge) in C# is normally better then introducing C++ to a project that is not already using it.
what is the benefit of managed C++
over C#?
C++.net is useful for interacting with C++ and C code (that is, calling external C or C++ libraries, providing callback functions to external modules written in C or C++ and so on.
what language of both would you
prefer?
I would prefer C# for all situations except the one described above (interacting with C and C++).
C# is easier to write, simpler, and geared specifically to use the .NET platform. C++ can do it also, but it has all the complexity of the C++ language plus the extensions needed to use the .NET platform.
Unless you need to interact with native C++ or C code, you're better off using C# in most cases (that is, if you're coding for the .NET platform).
Normally I prefer C++, but when needing to code for .NET, it doesn't beat C#.
Managed C++ is good for interop with C++: for example, if you have a .NET application and your assembly has to interact with a native interface that comes as C++ .lib files (which I had more than once), or with a nice C++ API.
Example: Rithmic (not that you ever heard of them) until recently ONLY supported a C++ API. If you try to access them from C# - good luck ;) Managed C++ allows me to access their API and expose nice .NET objects.
Basically interop. Managed C++ REALLY shines in interop with low level C / C++ API's.
I used managed C++ when I needed to build up new NET component with much ofC++ unmanaged code inside.
I did a specific class used to Marshall some objects forward and back from old C++ code.
I've encountered a problem which was transparent in managed C++, but made a big headache in C# - I had to send a callback function to a C++ unmanaged library, that defined this callback as __cdecl. In C#, the default calling convention is __stdcall, and it is pretty compilcated to move a C# delegate from __stdcall to __cdecl (Actually it requires either a 'shim' unmanaged DLL to do so or using some reflection classes).
In C++, (C++.Net as well), it is just a simple attribute.
I haven't personally written, or read for that matter, too many lines of code in managed C++, but from what I have seen it looks too convoluted and patchy. That said, you might want to use managed C++ if you are really good in C++, and when learning the idioms and patterns of a new language would be too much of a risk.
Use C# if you are quite competent in it. If you are only getting started with C++ and C#, I think, C# is the easier route to take.
I would prefer C#, or specifically .NET, over C++ because of the extensive .NET standard library.

Connecting C++ to C#

I've been writing a program in C++ and noticed there is a library in C# that someone else wrote that I would like to link in to my code.... but I'm not sure how to do that. Can someone suggest something? Doubt this matters, but I'm using Windows 7 with MSVC2010.
Thanks in advance!
You can try compiling your C++ program in C++/CLI mode. Then the compiler will produce a .NET executable which can create C# objects and use their methods.
C++/CLI is discussed here: C++/CLI
If you're familiar with COM you could access the .NET library through COM. If the library doesn't provide COM interop out of the box you could write a wrapper around it using C# and expose that through COM.
If you're going to pull in a .NET library you should be aware that it requires a .NET runtime which may take up valuable resources. If you ware building the application in C++ for performance reasons, maybe you're better off porting the parts of the library you need to C++.

Creating C++ Dll, and call it from C#

In my project I got a device which comes with C++ Sample codes. The codes are ok and the device is working as expected.
But I need it to talk with my C# interface because all other devices are currently using C# interface.
So I am planning to create a DLL Wrapper for the driver. I will create a C++ Library of my own (from source code with proper interface) and Call this C++ Library from C# using DLLImport (just call my interfaces there.).
I am pretty sure it can be done this way, but I have never created a C++ Library and used it from C# yet. So, can anyone refer me to some tutorial that goes with my problem?
I am using C++/C# int VS.NET 2008.
Regards,
Maksud
Have a look at
using a class defined in a c++ dll in c# code
Another useful tool you have at your disposal is C++ CLI.
You can use C++ CLI to create an intermediate library - one that exposes managed classes but runs unmanaged C++ code. You can actually mix managed and unmanaged C++ in the same DLL.
The unmanaged portion can accesses the unmanaged DLLs without having to use the PInvoke functions.
Your C# code can access the managed classes in this intermediate library.
Depending on the DLL and what you need to do you may not need to create a wrapper directly. You might be able to get away with P/Invoke for the functions. You will need to evaluate your specific needs and what is already available in the libraries/code provided.
For anyone who comes to this question and are looking for answers, you may want to try xInterop NGen++ , a C# wrapper generator for native C++ DLL, which has been just released to the public, the tool can generate C# wrapper for native C++ DLL automatically and instantly by using advanced P/Invoke technologies. Check out the current version and a free version will be out soon.
(I am the author of the tool)

Categories

Resources