.NET Delphi

Title: Consuming .NET class libraries in Delphi (Win32)
Question: .NET has a very rich set of *standard* libraries that can be used by any language that supports .NET, at the same time is quite resource consuming, what if you wanted just a little bit of code that can be easily written in .NET, in your Win32 application?
Answer:
The .NET concept is very powerful, and it's popularity keeps growing really fast, you see many code examples all over the web in .NET to do all kinds of stuff, create .PDF files, .ZIP files, web services, etc
The good news is that *all* the funcionality can be EASILY exported to any language that supports COM (like Delphi)
In this article I'll show you a simple example class created in C#; I could've created that in Delphi.NET, but just wanted to show you can reuse code written in any .NET language, in Delphi.
so, let's create that class in C# really fast
I used Visual Studio 2003
- Create a new project
- select Visual C# projects, Class library
I named the project CSharpExample, and the class ExampleClass, this is the CSharp code:
using System;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using System.Text;
namespace CSharpExample
{
///
/// Summary description for Class1.
///
[ComVisible(true),
Guid("634E2C36-81A8-420E-BFB8-A0E038932647"),
ClassInterface(ClassInterfaceType.AutoDispatch)]
public class ExampleClass
{
[ComVisible(true)]
public int Sum(int x1, int x2) {
return x1+x2;
}
[ComVisible(true)]
public string ReverseString(string str) {
string result = "";
foreach (char c in str)
result = c+result;
return result;
}
[ComVisible(true)]
public string GetMD5(string str) {
Byte[] clearBytes = new UnicodeEncoding().GetBytes(str);
Byte[] hashedBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);
return BitConverter.ToString(hashedBytes);
}
public ExampleClass()
{
}
}
}
is a simple class with 3 methods, Sum, ReverseString and GetMD5 just to give you and idea on using different types as the parameters and result. We compile that and get a .dll file, let's leave .NET there for now.
Now, let's create our Delphi application that consumes that class, I just created a form with some boxes to provide the input, and some buttons to execute the class methods
You will need ComObj in the uses clause.
to get an instance of the object, all we need to do is:
declare a OleVariant variable:
public
{ Public declarations }
DotNetObject:OleVariant;
end;
and since I'm going to be using this in more than one place, I load the object on the Forms OnCreate event:
procedure TForm1.FormCreate(Sender: TObject);
begin
DotNetObject:=CreateOleObject('CSharpExample.ExampleClass');
end;
now, the code to execute the methods is as straight forward as this:
procedure TForm1.Button1Click(Sender: TObject);
begin
LabeledEdit1.Text:=DotNetObject.Sum(StrToInt(Edit1.Text), StrToInt(Edit2.Text))
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
LabeledEdit1.Text:=DotNetObject.ReverseString(Edit3.Text)
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
LabeledEdit1.Text:=DotNetObject.GetMD5(Edit4.Text)
end;
note that on the first method (Sum), it returns an integer, but since I'm using variants I don't even need to worry about that, the conversion to string is implicit and you'll get the correct result there
one more thing, before you can get this to work, you need to copy the generated .NET .dll file to the folder where you have your Delphi project, then using the regasm utility, register it so that we can access it from Delphi as a COM Object
from the Windows Start button,
- select programs,
- Microsoft Visual Studio .NET 2003,
- Visual Studio .NET Tools,
- Visual Studio .NET 2003 Command prompt
change to the directory where you have your Delphi project and execute:
regasm CSharpExample.dll, done, run your Delphi program and see by your self =o)
The class is very simple, but nothing should stop you from wrapping some .NET code that processes files, communicates with web services, etc.
All the Delphi and C# code is available here for download
Note:
This code is using late binding (late binding=slow when calling it), which will be ok in many cases, but if you wanted to use early binding check this out:
http://interop.managed-vcl.com/netinterop_delphi.php, they have a utility that generates a type library for you out of the .NET code so you can use a class directly in Delphi and you can see all the methods available
keep up coding.
salu2
EberSys