Friday, March 17, 2006

Eager to use Delphi.NET for .NET 2.0? Start now!

Yesterday I played with the DCCIL command line compiler and noticed an interesting compiler switch called clrversion. And yes, it does exactly what its name suggest, it loads another CLR version of .NET, rather than the default one "v1.1.4322".

Therefore, I took out my .NET 2.0 installer and installed it in my BDS 2006 test VM. It never saw the version 2.0 of .NET before ;-) So far so good. I started the Developer Studio 2006 and created a new, simple console application. Thanks to the .NET SDK from my Visual Studio 2005 VM I found a simple test case rather fast.
program SimpleSampleNET2;

{$APPTYPE CONSOLE}

uses
System.Text;

begin
System.Console.BackgroundColor :=
ConsoleColor.Blue;
end.

Console applications for the MS.NET Framework 1.1 do not support changing the background color through System.Console.BackgroundColor. So it does not compile within the Developer Studio against the .NET 1.1.

Okay, create a .dpr file, copy this "complex" lines into it and save it into a folder on your disk with the name SimpleSampleNET2. Enter following command line into your command line (will not work yet, but never mind that now; not the path for the -NS switch!): dccil -CC -NSC:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 --clrversion:v2.0.50727 --no-config SimpleSampleNET2.dpr

You'll see the Delphi compiler loading the .NET 2.0 framework. The success is announced in line 3: .NET Framework v2.0.50727 loaded. Next it'll load some of the assemblies needed and create the "dcpil" files as needed. However, it stops with an error when trying to include the Borland System unit: SimpleSampleNET2.dpr(1) Fatal: F1026 File not found: 'Borland.Delphi.System.dcuil'

Well, go to $(BDS)\source\dotNet\rtl and copy the file Borland.Delphi.System.pas to your application path. Now you simply need to recompile the system unit for .NET 2.0, using the following command line: dccil -CC -NSC:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 --clrversion:v2.0.50727 --no-config -Q -M -y -Z -$D- Borland.Delphi.System.pas. Never mind the warnings and that single hint there.

Now, compile your little application again: dccil -CC -NSC:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 --clrversion:v2.0.50727 --no-config SimpleSampleNET2.dpr. Success!

Now, in the command line, start your little application. If you do not have "Blue" as background color already, it should be changed now, as we have done just that in our code. It works! It is a .NET 2.0 application written and compiled with the Delphi 2006 .NET compiler. Just to be sure, copy that little application on a computer that only has .NET 2.0 installed (not .NET 1.1) and test it. It'll work just the same there.

Take it from here and have fun!

EDIT:

On Request I have bundled a small package for you (ConsoleApplication.zip 8KB). It contains:
  • make.bat
  • SimpleSampleNET2.dpr
  • SimpleSampleNET2.exe
It does not contain the Borland.Delphi.System.pas file! Copy that from your installation path as instructed above prior to running make.bat.

9 Comments:

Blogger Hallvards New Blog said...

Great post!

http://hallvards.blogspot.com/2006/03/compiling-delphi-code-for-net-20.html

1:12 PM  
Blogger Doc Martin said...

did you know that you can even run the Delphi 2005 GUI under .NET 2.0 and use 2.0 assmblies?

A lot of stuff does not work (like the debugger :0)

But you can do 2.0 WinForms development, for example.

You'll need to alter the bds.exe.config file, and I suggest strongly copying the bds.exe file to another name and creating a bds20.exe.config file that binds bds to .Net 2.0 AND using -rNet20 in the command line to ensure your are using isolated registry entries for that instance...

After, that you'll need to disable/delete some packages that will not load with any amount of persuasion, and Delphi will fire up and compile against 2.0.

have at my post here for details

http://docmartinscodingstuff.blogspot.com/2005/08/delphi-2005-and-net-runtime-20.html



Check your output using reflector, just in case, but there you go...

3:53 PM  
Blogger Daniel "sakura" Wischnewski said...

Quiet interesting. Cool, thanks. I'll play around with it and write something up for BDS 2006, hoping it will not kill my whole setup :)

3:56 PM  
Anonymous Anonymous said...

What are the advantages of using .NET 2.0 this way?

12:42 AM  
Blogger Daniel "sakura" Wischnewski said...

For me it was simply the interest of getting it to work. I am not sure whether it will be useful in its current state.

For .NET 2.0, currently I use VS 2005 waiting for Highlander :-)

5:09 PM  
Anonymous Anonymous said...

Mmm I don't if you guys have BDS 2006, but i'd be interested to know if there's something similar for the C#Builder personality... does it really mean I must get VS for .NET for a while before Borland fixes things up to support 2.0?

11:19 AM  
Anonymous Anonymous said...

I using BDS 2006 with Compact Framework Help Builder 1.3 from Jeremy North.
Insert the "-NSC:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 --clrversion:v2.0.50727" in parameters.

It's Works !

8:06 PM  
Blogger andy345 said...

Great the option for the compact framework! But with BDS 2006 with Compact Framework Help Builder 1.3 if I use the monthcalendar the result is "build failed". What suggest you me?

3:58 PM  
Anonymous Anonymous said...

This is really a cool trick, but it is "normal" since Delphi.net like other .net language don't create native code but MSIL. So the MSIL is the almost the same between .net 1 and 2, it's just the framework itself that has changed.
But using .Net 2 this way has a little interest. Still no masterpage in Asp.net for instance.
--
For the guy asking if the trick is working with c# personality, let's have a try.. But what hell can make you develop in c# with BDS instead of free MS C# Express... That's mysterious !

6:16 PM  

Post a Comment

<< Home

Google
Search WWW Search delphi-notes.blogspot.com