C# and Delphi integration - c#

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

Related

How to call a c# dll from java program

I have a c# dll that needs to be called in Java.I see that there is a method using jni to call c++ dlls.How can I do it for a c# dll..Please help..I couldnt find any good material on this
From here:-
IKVM.NET is an implementation of Java for Mono and the Microsoft .NET
Framework. It includes the following components:
A Java Virtual Machine implemented in .NET
A .NET implementation of the Java class libraries
Tools that enable Java and .NET interoperability
You can use Java Native Interface. Or you can create a COM assembly from the C# code and use J-Interop to invoke it.
If you have C# dll sources you need to use maybe the better way will be to translate it to Java using some tools like GrassHopper.
According to GrassHopper key feature explanation it can convert MSIL to Java bite code. So can use without sources of c# dll
Check this: http://www.javonet.com
If you look for quick and easy solution then Javonet should work fine for you. It is light counterpart of IKVM and J-Integra works also as native bridge.
All you have to do is:
add Javonet.jar do your project call
call Javonet.addReference("yourlib.dll")
use your .NET library like it was almost JAVA package
Sample:
NObject obj = Javonet.New("yourDotNetClass");
obj.invoke("YourMethod","arg1", 2);
The syntax is not strongly-typed and works like reflection but gives you fastest access to any custom .NET code, third-party libs or .NET framework as no changes are needed on .NET side. If you need it is also possible to implement custom strongly-typed wrappers.
I do recommend this bridge as in my opinion it is easiest to quickly get things done but also other native bridges are worth checking as this is best approach for such case.
I would avoid going into custom JNI or COM unless you have a lot of time and you just want to learn, if you need quick and reliable solution take one of third-party bridges.

What is the best way to wrap our software library to serve both C++ and C# client apps?

I am starting work on a new project where the core product is a Software Library. There is existing code we will be wrapping, written in C++ because it is very numerically intensive; we own and compile this core code.
I believe we will have two prominent client types:
1. App written in C++, probably MFC
2. App written in C#.NET.
The Question: In order to offer an "ideal" interface to both these client types, what technology do we use? For example, we could offer two interfaces, one using C/C++ functions and callbacks for the C++ clients, and the other using C#.NET for the C# clients? Also, in principle, could this be done nicely in one DLL, or will we want two (or more)?
Other info... Client apps are embedded, not a desktop or laptop; assume any UI is not Windows-based. Also we will have to compile and test for both 32-bit and 64-bit, probably C++ on 32-bit and C# on 64-bit.
My apologies here -- I realize this question may have been asked and answered previously, but if so, I was not able to find it.
Interesting question, I'm looking forward to other answers.
One possible solution, if probably not the best: If your library code follows object-oriented practices, you could write the core functionality in C++ and then provide separate COM wrapper classes around the core classes (also in C++). This would allow you to use your library components from .NET via COM interop. I don't know how efficient that would be — I suspect performance should generally be fine —, and COM has some obvious drawbacks (no generics, for example), so that may not be the optimal solution.
If your library is not object-oriented, you can still call single functions exported from any DLL in .NET using P/Invoke... if you go down this route, it's likely that you'll end up with a C# wrapper around your C++ DLL.

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++.

Visual Studio 2008 - Sharing Data in C++/C# Multi Project Solution

I have a solution with 2 projects:
a c++ main project
a c# project (display simulator)
Today these 2 apps share data using a loopback TCP client/server connection, but that's not very optimal (timing issues..).
I was wondering if there was a way to access the c# data from the c++ project directly and vice versa? (It seems to be possible with 2 c# projects..)
If it's not possible, what's the best way to implement this with shared memory?
thanks!
Michael
EDIT: thanks for the answers. The 2 projects used to be independant solutions and are both executables - I'm actually trying to merge the 2 into 1 solution / executable.
For info: The c++ app is a PC version of an embedded app - the c# app is a lcd/HMI simulator.
Converting the C++ project to a C++/CLI project might be the easiest way to go. Note however that some code doesn't play well with C++/CLI (we've had problems using libraries that use boost::thread in a managed executable).
You can use COM Interop or Platform Invoke to access native code in C#.
If that's not what you're asking for, please elaborate.
Named Pipes?
For interprocess communication via named pipes you can check out the .NET 3.5 feature
http://msdn.microsoft.com/en-us/library/system.io.pipes.aspx for the C# side. From the C++ side, I assume you know the equivalent :).
There are two ways I know of to get direct access to memory between c++ and c# without the overhead of marshaling/demarshaling and com. If it is required to keep the c++ part of your code native then the only way I know to achieve this is to host the clr from your c++ application. A relatively complicated undertaking. Google "hosting the common language runtime". Then you can load your c# code as an assembly, call into the c# code and provide a common shared memory style interface. Although you will have to write all of the shared memory support yourself as I have found no direct support for shared memory in c#/.net.
The other way is to compile your c++ code with common language runtime support. This is by far easier and will allow you all the power and glory of c++ while allowing access to clr based data types, and the ability to call back and forth between your c++ and c# code. See "Language Features for Targeting the CLR" in your VS2008 documentation. pin_ptr will become your close friend and ally in this process.

Consuming a .Net library via COM or direct integration in Java

I have to admit the last time I programmed in Java was a data structures class in high school. So please be kind to this Java noob.
I have spent a good deal of time putting together a C# COM library at work. This library relies heavily on some new .Net technologies (WPF being the big one) so translating it into another language is not really an option. I have tested consuming this library from C++ through the COM interface and that was simple enough. I wanted to also prove that this same library could be used from a Java program.
So the problem that I have run into is that I can not find a free or even inexpensive way to use COM objects from Java. I guess the other solution would be to find a way to directly integrate the library. Does anyone have any input on what might be the best way to accomplish this? I am using Eclipse for my java environment. Below is an example of what my C# codes exposure looks like. Thanks for any direction you can provide.
//Example C# Object Code
[Serializable,
ComVisibleAttribute(true),
Guid("Long Guid String"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(IFooBarStructure)),
ProgID("My Application")]
public class MyFooBarObject
{
public MyFooBarObject(){}
public string DotNetMethod(){ return String.Empty; }
}
You can use JACOB
From the site:
JACOB is a JAVA-COM Bridge that allows you to call COM Automation components from Java. It uses JNI to make native calls into the COM and Win32 libraries.
Sound like the kind of thing you're looking for.
If you have a little bit of money to spend on a commercial product, I recommend that you take a look at Intrinsyc's J-Integra for COM or J-Integra for .NET products. I used their COM product to access a 3rd party ActiveX control (the Bloomberg data access library) from Java code for a project I worked on a few years ago, and it worked very well. They have a reasonable trial / demo policy, and they are quite responsive to support requests.
If you can access your library through C++, you can access it through JNI. JNI is pretty easy to do, just read the guide carefully especially section 8.6 where it talks about the differences between C and C++
The direct Java/.NET integration can be done with OOJNI. Google "Object-Oriented JNI for .NET".
I am author of jni4net, open source intraprocess bridge between JVM and CLR. It's build on top of JNI and PInvoke. No C/C++ code needed. I hope it will help you.

Categories

Resources