I’ve been doing some exploration around migration issues from Delphi 7 (Win32) to Delphi 8 (.NET). Yes, VCL for .NET covers a whole bunch of stuff and makes migration a whole bunch easier than it is for other languages, but there are still things you can do in Delphi 7 code that won’t migrate seamlessly to Delphi 8.
Today, I sat down to look at moving a COM client from Delphi 7 to Delphi 8. I started with a nice, simple scenario. A COM object called BasicSrv, which exposes an Add method. Add takes 2 parameters, x and y, both integers and returns another integer. Hardly staggering stuff, but ever since I started using DCE years ago, through DCOM, CORBA, COM+, J2EE, whatever, this simple scenario has been my "Hello World".
Once I had this COM object in Delphi 7, I created a Delphi 7 client. Again, very simple. Two edit boxes, a button and a label. Click the button, we create an instance of the COM object and call Add, shoving the results back into the label. The code looks like this:
implementation
uses Server_TLB;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
basicSrv : IBasicSrv;
begin
basicSrv := CoBasicSrv.Create;
Label1.Caption := IntToStr(basicSrv.Add(StrToInt(Edit1.Text),
StrToInt(Edit2.Text)));
end;
Like I said, nice and simple.
So then I went to get the client working in Delphi 8 under .NET. I’m a little ashamed to admit, I didn’t expect the experience to be very pleasant, as it seemed like way too much "stuff" had changed under me in the move from Win32 to .NET for this to be anything but painful. Anyway, I opened the Delphi 7 project in Delphi 8, removed the Server_TLB unit from my project and added a reference to my COM object. Surprisingly, the code needed to call this COM object from .NET looked like this (the bits that changed are in bold):
implementation
uses Server;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
basicSrv : IBasicSrv;
begin
basicSrv := BasicSrvClass.Create;
Label1.Caption := IntToStr(basicSrv.Add(StrToInt(Edit1.Text),
StrToInt(Edit2.Text)));
end;
All that changed was my unit name and the class name. And I suspect that if I paid a little more attention while adding my reference (or used tlbimp directly), I may have got away with no code changes. How cool is that?
I’ll be spending a bit more time with examples that are a little more complicated, but I’m really encouraged, and a little ashamed of myself for doubting 🙂
Be the first to leave a comment. Don’t be shy.