Borland Delphi 2005 Migration To Using Vcl For.pdf
(
563 KB
)
Pobierz
Tutorial 1
Tutorial 1
Case Study
Product News
Events
Training
Delphi 2005
Borland® Delphi® 2005 Migration to .NET using VCL for .NET
by Bob Swart, Bob Swart Training & Consultancy
Abstract - This tutorial demonstrates the migration of Delphi Win32 source code, units and (database) applications to the
Microsoft .NET Framework using Borland Delphi 2005 and VCL (for .NET).
l
Introduction
l
VCL and VCL for .NET
l
Data Access
l
Summary and Conclusions
Introduction
In this tutorial we will migrate a real-world Win32 application to the Microsoft .NET Framework using Delphi 2005, using the Borland VCL
(Visual Component Library) as the framework. A number of existing VCL for Win32 applications - taken from the Delphi 7 Demos
directory - will be opened in Delphi 2005 and migrated to a .NET target using VCL for .NET.
These exercises will demonstrate several migration issues, small and more significant alike.
VCL and VCL for .NET
The VCL has been made available for the .NET Framework, enabling us to easily migrate Win32 VCL applications to .NET (its also
possible to take VCL projects to WinForms).
Delphi 2005 comes with a large number of example applications, in the BDS\3.0\Demos directory, which contains - among others - the
Delphi.NET and DelphiWin32 subdirectories. The BDS\3.0\DelphiWin32\VCLWin32\ directory contains several Win32 VCL examples,
some of which are already migrated to .NET, and found in the BDS\3.0\Delphi.NET\VCL directory.
One of the examples which is not already migrated to .NET is the Threads example, demonstrating the QuickSort, SelectionSort and
BubbleSort algorithms in a multi-threaded example. The Threads project is a Win32 VCL application that we will be migrating to .NET
using VCL for .NET.
First, let's create a copy of the necessary files.
l
Create a new subdirectory Threads in the BDS\3.0\Demos\Delphi.NET\VCL directory. The result of our migration will then be
available as another example of a Delphi for .NET VCL project.
Copy all files from the BDS\3.0\Demos\DelphiWin32\VCLWin32\Threads directory to the BDS\3.0\Demos\Delphi.NET\VCL
\Threads directory.
l
Remove the thrddemo.bdsproj file, since that file stores the fact that this was a Delphi Win32 project (and we want to migrate to .
NET instead).
l
http://www.borland.com/delphi/demo/tutorial/tutorial1.html (1 of 20)3/27/2005 12:52:22 AM
Tutorial 1
Now we are ready to work on the new Threads project, and migrate it to .NET.
l
Start Delphi 2005
l
Click on the Open Project button in the Welcome Page and open the thrddemo.dpr project file from the BDS\3.0\Demos\Delphi.
NET\VCL\Threads directory (the copy we just made of the Win32 demo).
Since this project has no .bdsproj file associated with it, the Delphi 2005 IDE needs to ask you if you want to upgrade it to a Win32 or .
NET project. This is done with the Project Upgrade dialog, shown in the following figure:
Project Upgrade thrddemo to .NET
l
Specify the Delphi for .NET target, and click on the OK button.
This will generate a thrddemo.bdsproj for us, with the .NET personality specified inside. We will now save the project, so the new
personality information is stored in the thrddemo.bdsproj file.
l
Do File | Save All, so the thrddemo project is saved, including the new thrddemo.bdsproj file.
l
Press Ctrl+F9 to compile the thrddemo project for the first time.
This should give you about 11 warnings and 5 errors, which are as follows:
[Warning] thrddemo.dpr(8): W1005 Unit 'Borland.Vcl.Forms' is specific to a platform
[Warning] ThSort.pas(6): W1005 Unit 'Borland.Vcl.Windows' is specific to a platform
[Warning] ThSort.pas(6): W1005 Unit 'Borland.Vcl.Messages' is specific to a platform
[Warning] ThSort.pas(6): W1005 Unit 'Borland.Vcl.Graphics' is specific to a platform
[Warning] ThSort.pas(6): W1005 Unit 'Borland.Vcl.Controls' is specific to a platform
[Warning] ThSort.pas(6): W1005 Unit 'Borland.Vcl.Forms' is specific to a platform
[Warning] ThSort.pas(6): W1005 Unit 'Borland.Vcl.Dialogs' is specific to a platform
[Warning] ThSort.pas(7): W1005 Unit 'Borland.Vcl.ExtCtrls' is specific to a platform
[Warning] ThSort.pas(7): W1005 Unit 'Borland.Vcl.StdCtrls' is specific to a platform
[Warning] SortThds.pas(6): W1005 Unit 'Borland.Vcl.Graphics' is specific to a platform
[Warning] SortThds.pas(6): W1005 Unit 'Borland.Vcl.ExtCtrls' is specific to a platform
[Error] SortThds.pas(18): E2397 Unsafe pointer only allowed if compiling with {$UNSAFECODE ON}
[Error] SortThds.pas(57): E2003 Undeclared identifier: 'Point'
[Error] SortThds.pas(65): E2396 Unsafe code only allowed in unsafe procedure
[Error] SortThds.pas(107): E2396 Unsafe code only allowed in unsafe procedure
[Fatal Error] ThSort.pas(39): F2063 Could not compile used unit 'SortThds.pas'
We can ignore all warnings for now, and will get back to those later. First, let's fix the compiler errors.
The first error is regarding line 18 of SortThds.pas, and mentions that "unsafe pointer only allowed if compiling with {$UNSAFECODE
ON}". The actual line of code that causes this warning is the declaration of FSortArray of type PSortArray in the TSortThread class
http://www.borland.com/delphi/demo/tutorial/tutorial1.html (2 of 20)3/27/2005 12:52:22 AM
Tutorial 1
definition:
type
PSortArray = ^TSortArray;
TSortArray = array[0..MaxInt div SizeOf(Integer) - 1] of Integer;
TSortThread = class(TThread)
private
FBox: TPaintBox;
FSortArray: PSortArray;
The PSortArray is a pointer to TSortArray, and a pointer is not a "safe" type, hence the warning. The warning is also followed by a
number of other unsafe warnings, related to the use of this unsafe pointer type.
Although the final result will have no unsafe types or code at all, at first it's usually easier to remove the compiler errors by marking code
as unsafe first - first make the project compile successfully, and then replace the unsafe sections by safe code.
l
Add the {$UNSAFECODE ON} compiler directive before the class definition for TSortThread, so the field declaration of
FSortArray is accepted.
Press Shift+F2 to Save All files in the project, and then press Shift+F9 to rebuild the project.
l
Ignoring the warnings, this should give you 4 errors left.
[Error] SortThds.pas(59): E2003 Undeclared identifier: 'Point'
[Error] SortThds.pas(67): E2396 Unsafe code only allowed in unsafe procedure
[Error] SortThds.pas(109): E2396 Unsafe code only allowed in unsafe procedure
[Fatal Error] ThSort.pas(39): F2063 Could not compile used unit 'SortThds.pas'
There is now an identifier Point which is unknown. The offending line of code is actually as follows:
procedure PaintLine(Canvas: TCanvas; I, Len: Integer);
begin
Canvas.PolyLine([Point(0, I * 2 + 1), Point(Len, I * 2 + 1)]);
end;
It looks like a call to a function Point is made (twice), but somehow the .NET compiler cannot find the Point function. This is a good case
to use the Refactor - Find Unit functionality, to help us locate the missing unit (with the Point function definition), and add it to the uses
clause.
l
Place the cursor in one of the Point identifiers in the source line in the Code Editor, and right click to select the Find Unit
submenu of the Refactor menu (alternately you can use the Refactor menu choice to select the Find Unit choice).
This will bring up the Find Unit dialog with the Search criteria Point already specified. A number of units are also mentioned, including
Borland.Vcl.Types.Point which seems like the perfect candidate for a definition of Point.
l
Select the Borland.Vcl.Types.Point unit, and click on either the Interface or Implementation choice to decide where this unit will
be added to the uses clause of the SortThds.pas unit.
http://www.borland.com/delphi/demo/tutorial/tutorial1.html (3 of 20)3/27/2005 12:52:22 AM
Tutorial 1
Refactoring - Find Unit
l
Press Shift+F2 to Save All files in the project, and then press Shift+F9 to rebuild the project.
This will now result in 3 errors. As always, the last (fatal) error is caused by the fact that we have two earlier errors. So there are in fact
only two errors to fix.
[Error] SortThds.pas(70): E2396 Unsafe code only allowed in unsafe procedure
[Error] SortThds.pas(112): E2396 Unsafe code only allowed in unsafe procedure
[Fatal Error] ThSort.pas(39): F2063 Could not compile used unit 'SortThds.pas'
We still use the unsafe pointer in FSortArray, and this is what the two errors report. The first error concerns a line of code in the Create
constructor, where we take the address of the SortArray parameter and assign it to the unsafe FSortArray pointer. This code is unsafe,
and doesn't compile unless we mark the Create constructor with the unsafe keyword.
l
Add unsafe to the implementation of the Create constructor, as follows:
constructor TSortThread.Create(Box: TPaintBox; var SortArray: array of Integer); unsafe;
begin
FBox := Box;
FSortArray := @SortArray;
FSize := High(SortArray) - Low(SortArray) + 1;
FreeOnTerminate := True;
inherited Create(False);
end;
l
Press Shift+F2 to Save All files in the project, and then press Shift+F9 to rebuild the project.
This time, the error message about the unsafe code in the Create constructor is no longer shown. However, another error message
concerning the Create constructor now appears:
[Error] SortThds.pas(72): E2305 'Self' might not have been initialized
[Error] SortThds.pas(112): E2396 Unsafe code only allowed in unsafe procedure
[Fatal Error] ThSort.pas(39): F2063 Could not compile used unit 'SortThds.pas'
The error message concerns the fact that we can call the inherited constructor after we've done some specific property initialization.
However, unless you have a good reason not to do so, the recommendation is to call the inherited constructor before the custom
constructor code. This is easy to fix, of course.
l
Move the inherited call to Create as first line of code in the Create constructor, as follows:
constructor TSortThread.Create(Box: TPaintBox; var SortArray: array of Integer); unsafe;
begin
http://www.borland.com/delphi/demo/tutorial/tutorial1.html (4 of 20)3/27/2005 12:52:22 AM
Tutorial 1
inherited Create(False);
FBox := Box;
FSortArray := @SortArray;
FSize := High(SortArray) - Low(SortArray) + 1;
FreeOnTerminate := True;
end;
l
Press Shift+F2 to Save All files in the project, and then press Shift+F9 to rebuild the project.
We are now left with only one error to fix, again an unsafe code error message:
[Error] SortThds.pas(112): E2396 Unsafe code only allowed in unsafe procedure
[Fatal Error] ThSort.pas(39): F2063 Could not compile used unit 'SortThds.pas'
This time, it's the Execute method where we use the ^ operator on the unsafe pointer FSortArray. The fix consists of adding the unsafe
keyword to this method as well.
l
Add the unsafe keyword to the implementation of the Execute method, as follows:
procedure TSortThread.Execute; unsafe;
begin
Sort(Slice(FSortArray^, FSize));
end;
l
Press Shift+F2 to Save All files in the project, and then press Shift+F9 to rebuild the project.
[Error] SortThds.pas(114): E2454 Slice standard function not allowed for VAR nor OUT argument
[Fatal Error] ThSort.pas(39): F2063 Could not compile used unit 'SortThds.pas'
This is a bigger problem. For some reason, the Slice function under .NET has a problem with the way we pass the arguments right now.
Considering what it does, it actually looks safe to ignore the call to Slice, and just pass the actual FSortArray^ to Sort. Within the Sort
method, we'll use High and Low to determine the actual size of the array, and that should work just fine (note that we'll turn it into safe,
managed code in a moment anyway).
l
Remove the call to Sort that uses the Slice standard function, and replace it with a call to Sort passing just FSortArray^ as
argument, as follows:
procedure TSortThread.Execute; unsafe;
begin
// Sort(Slice(FSortArray^, FSize));
Sort(FSortArray^);
end;
l
Press Shift+F2 to Save All files in the project, and then press Shift+F9 to rebuild the project.
This time, only warnings are left:
[Warning] thrddemo.dpr(8): W1005 Unit 'Borland.Vcl.Forms' is specific to a platform
[Warning] ThSort.pas(6): W1005 Unit 'Borland.Vcl.Windows' is specific to a platform
[Warning] ThSort.pas(6): W1005 Unit 'Borland.Vcl.Messages' is specific to a platform
[Warning] ThSort.pas(6): W1005 Unit 'Borland.Vcl.Graphics' is specific to a platform
[Warning] ThSort.pas(6): W1005 Unit 'Borland.Vcl.Controls' is specific to a platform
[Warning] ThSort.pas(6): W1005 Unit 'Borland.Vcl.Forms' is specific to a platform
[Warning] ThSort.pas(6): W1005 Unit 'Borland.Vcl.Dialogs' is specific to a platform
[Warning] ThSort.pas(7): W1005 Unit 'Borland.Vcl.ExtCtrls' is specific to a platform
[Warning] ThSort.pas(7): W1005 Unit 'Borland.Vcl.StdCtrls' is specific to a platform
[Warning] SortThds.pas(6): W1005 Unit 'Borland.Vcl.Graphics' is specific to a platform
http://www.borland.com/delphi/demo/tutorial/tutorial1.html (5 of 20)3/27/2005 12:52:22 AM
Plik z chomika:
uena78
Inne pliki z tego folderu:
kurs php.zip
(586590 KB)
Video.Kurs.PHP.i.MYSQL.PL-R3R.iso
(587104 KB)
Kurs SQL.zip
(363307 KB)
Kurs_C++_Video.part1.rar
(683593 KB)
Kurs_C++_Video.part2.rar
(683593 KB)
Inne foldery tego chomika:
- ╣█ AM 6.15 ( 1312 Finał )( PL - EU )( Gotowiec Na SD ) 25-01-2014
- ╣█ AM 6.15 ( 1312 Finał )( PL - EU )( Oryginał ) 25-01-2014
- ▧ ▍Programy PC 2014
- AutoMapa 6.11e (1212) FINAŁ EUROPA + POLSKA ( CRACKED ) 19-12-2012 ◉◉◉◉◉◉◉◉◉◉◉◉◉
- ♛ Oryginalne Obrazy WINDOWS XP 2012 ◉◉◉◉◉◉◉◉◉◉◉◉◉
Zgłoś jeśli
naruszono regulamin