Translation Borland® Delphi® 2005 Transplanted to .NET (1) using Vcl for .NET (1)

xiaoxiao2021-03-06  18

Borland® Delphi® 2005 Migration To .NET Using VCl for .Netby Bob Swart, Bob Swart Training & Consultancy

Summary: This tutorial demonstrates the use of Delphi 2005 and VCL (for .NET). Transplant Delphi Win32 Source, UnitS and (Database) application to Microsoft .NET Framework.

getting Started

VCL and VCL for .NET

data access

to sum up

getting Started

In this tutorial, we will use the Delphi 2005 to transplant a real Win32 application to Microsoft .NET Framework, and use Borland VCL (Visual Component Library) as a framework. Several existing VCL for Win32 applications - sample directory from Delphi 7 Finding - will open with Delphi 2005 and transplanted to .NET and convert to vcl for .net.

.............. ..

This course will demonstrate several transplant examples, and the procedures are small but representative.

VCL and VCL for .Netvcl can already be used under the .NET Framework, which allows us to transplant the Win32 VCL application to .NET (likely to convert VCL items to WinForms)

Delphi 2005 has a lot of application examples in the BDS / 3.0 / DEMOS directory, including - in addition to individual-Delphi.Net and Delphi Win32 subdirectory. Several included in BDS / 3.0 / Delphiwin32 / Vclwin32 / directory Win32 VCL example, some of which have been ported to .NET, can be found in the BDS / 3.0 / Delphi.net / VCL directory.

One of them has not been ported to .NET's example Threads, demonstrates the Quicksort, Selectionsort, and Bubblesort algorithms in multi-thread. Threads project is a Win32 VCL application, now we will copy it to .NET and use vcl for .net

First, we create a backup for important files.

Create a subdirectory Threads under the BDS / 3.0 / DEMOS / DELPHI.NET / VCL. After the project is transplanted, it will become an example of another delphi for .NET VCL. From the directory BDS / 3.0 / Demos / Delphiwin32 / VCLWIN32 / Threads copy all The file to the directory BDS / 3.0 / DEMOS / DELPHI.NET / VCL / THREMOS / DELPHI.NET / VCL / Threads Remove this file, because this file does this mean this is a delphi win32 project (and actually we want to port it to .NET)

Now we are ready to work on a new THREADS project and transplant it to .NET

Run Delphi 2005

Click the "Open Project" button on the welcome page and open the THRDDEMO.DPR under the directory BDS / 3.0 / Demos / Delphi.net / Vcl / Threads (we just copy from the Win32 paradigm)

Because this project does not have a bdsproj file, Delphi 2005 IDE will ask you to upgrade to Win32 or .NET project. A project upgrade dialog will appear, as shown below

Upgrade THRDDEMO project to .NET

Specify the target as Delphi for .ne, and press the OK button

This will generate a THRDDEMO.BDSPROJ file to us, there is a corresponding .NET PERSONALITY ID. Now we save the project, the new related property information will be saved to the file thrddemo.bdsproj.

Execute menu file! Save all, save the thrddemo project, including the newly generated file thrddeemo.bdsproj

Press CTRL F9 to start finishing THRDDEMO project

You will get approximately 11 warnings and 5 mistakes, as follows: [Warning] thrddemo.dpr (8): w1005 unit 'borland.vcl.form' 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 now ignore all warning messages, wait for a while, let's turn back. First, let's correct those documented mistakes first.

The first error is the 18th line of the Sortthds.Pas file, the prompt is: T "Unsafe Pointer Only Allowed if compiling with {$

Unsafecode

On

} ". (Insensitive pointer only can be done in using {$ unsafecode on}). Causes the wrong code is declared in the TsortThread class PsortArray:

Type

Psortarray = ^ TsortArray; TsortArray = array [0..maxint Div Sizeof (Integer) - 1] of integer

Tsortthread = Class (TTHREAD)

Private

FBOX: TPaintBox;

FSORTARRAY: PSORTARRAY;

PsortArray is a pointer to TsortArray, and the pointer is not a "secure" type, so warning prompts will be triggered. There are other unsafe states of unsafe declarations, as long as this unsafe pointer type is used.

Although the final version will not use unsafe type or code, first, mark the code as an unsafe method to solve the documented error - first make the project to be done, then the unsea-safe part is used Safe code is replaced.

In addition to the TSORTTHREAD class declaration, this is added to the $ unsafecode on}, so that the declaration points to the type of FSORTARRAY can be saved in all items in all items by pressing Shift F2, and then press SHIFT F9 to re-add the project.

Ignore the relevant warning information, there are still four mistakes.

[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 a statement that the unknown type variable. This is unpleasant code is as follows:

Procedure PainTline (Canvas: Tcanvas; I, Len: Integer);

Begin

Canvas.Polyline ([POINT (0, I * 2 1), Point (Len, i * 2 1)]);

END;

Looks it to call (twice) a function point, but I don't know why, .NET's finger cannot find the function Point. This is a use reconstruction (refactor) --- Find Unit FunctionAlity (Finding unit function) Opportunity, it will help us position the lack of units (define the function Point) and increase the Uses clause of the unit.

In the code editor, position the cursor into one of the Point identifiers of the source code, and press the Right click on the submenu under the menu Refactor (another option is in the system main menu Refactor Select the Find Unit option in the system main menu REFAACTOR)

This will call the Find Unit dialog, and the wrap contains a standard unit that finds all definitions of Point. Many units are found, including borland.vcl.types.point, one looks best for this coded definition.

Select the Borland.vcl.types.Point unit, then select Interface or Implement to determine the declaration of this unit in the USES clause in the sortthds.pas code.

Refactoring - Find Unit

press

Shift F2

Save files in all projects

,

then press

Shift f9

Re-re-resetting the project

.

There is still rest

3

Error

.

Just like it

,

the last one

(

serious

)

Error is caused by two previous errors

.

and so

,

Actually

,

We have two errors to modify

.

[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 FSORTARRAY's unsafe pointers, which is a reason why two errors occur. In the CREATE constructor in the code line, in this, I get the address of the parameter SortArray and point to an unsafe FSORTARRAY Class pointer. This code is unsafe, and is incorporated, unless we identify a UNSAFE for the Create constructor.

Increase the implementation code of the Unsafe to the Create constructor, as follows:

· Constructor Tsortthread.create (Box: TpaintBox; Var SortArray: array of integer; unsafe;

Begin

· Fbox: = box;

· Fsortarray: = @SortArray;

· Fsize: = high (sotarray) - Low (sORTARRAY) 1;

FreeOnterminate: = true;

· Inherited Create (false);

· END;

Press SHIFT F2 to save files in all projects, and then press SHIFT F9 to re-add the project.

Now, the unsafe code error prompts in the Create constructor have no longer appear, however, another error message about the Create constructor 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 essence of this error is that we can call the Inherited constructor after initializing some specific properties. However, unless you have a good reason not to do this, the recommended practice is to call the Inherited constructor before using custom code. This error is of course very easy to modify.

The first line of the CREATE CREATE is moved to the Create constructor, as shown below:

· Constructor Tsortthread.create (Box: TpaintBox; Var SortArray: array of integer; unsafe;

Begin

· Inherited Create (false);

· Fbox: = box;

· Fsortarray: = @SortArray;

· Fsize: = high (sotarray) - Low (sORTARRAY) 1;

FreeOnterminate: = true;

· END;

Press SHIFT F2 to save files in all projects, and then press SHIFT F9 to re-add the project.

Now let's only have an error to modify, or an incorrect message for unsafe code:

[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, in the Execute method, we use the ^ operator for unsafe pointers FsortArray. Modifications are to increase the Unsafe keyword to this function.

Increase the Unsafe keyword to implement the code of the Execute method, as shown below:

· Procedure tsortthread.execute; unsafe;

Begin

· Sort (slice (fsortarray ^, fsize);

· END;

Press SHIFT F2 to save files in all projects, and then press SHIFT F9 to re-add the project.

· [Error] Sortthds.Pas (114): E2454 SLICE Standard Function Not ALOWED for VAR NOR OUT ARGUMENT

· [Fatal Error] THSORT.PAS (39): F2063 Could Not Compile Used Unit 'Sortthds.pas'

This is a big problem, because some reasons, the SLICE function is in .Net, there is a problem when we do as a parameter transmission. Considering the role of it in the code, you can safely ignore the call slice, directly transfer the current FSORTARRAY ^ to function sort. In the sort method, we will use high and low to determine the actual array length, this will run very well (note, later, we will modify these code is safe, controlled Code).

Remove the SLICE function used in the call sort, and pass it directly to Sort with FsortArray ^ as a parameter, as shown below:

· Procedure tsortthread.execute; unsafe;

Begin

· // sort (Slice (fsortarray ^, fsize);

Sort (fsortarray ^);

· END;

Press SHIFT F2 to save files in all projects, then press SHIFT F9 to re-add

Now, only warning information is 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

[WARNING] Sortthds.Pas (6): W1005 Unit 'Borland.vcl.extCtrls' Is Specific To a Platform

[Warning] Sortthds.Pas (71): W1047 Unsafe Code '@ Operator'

[Warning] Sortthds.Pas (113): W1047 Unsafe Code '^ Operator'

Most of the information is only to tell us that VCL for .NET can only run in the specified platform - don't be too surprised. There are two warning messages to be severely punished, pointing out the use of non-secure code: use @ and ^ operator. Slightly We will handle this. Now, let us ignore the warning prompt, first run the project:

Threads Sorting Demo for .NET

The project is now running with the active local .net, with three concurrent threads.

However, this project is not a 100% security code program, now we continue to work to modify it.

"Safety" code

Contains applications that are unsafe code that will not pass when you check with Peverify. The current situation is that you can accept a transplanted result, but your last goal, you may want to implement a 100 for your application. % Secure code version

Remove the {$ unsafecode on}, the documented, of course, there are two keywords (in the CREATE constructor, and executers), now prepare to modify the unsafe code.

The first problem - cause all other error messages - is a declaration of a pointer. We can declare TsortArray as a "array of integer". If you want to keep your code, you can add it in Win32 and .NET. You can use the fingers to distinguish the original code and new code.

Modify TsortArray defined types of Array Of Integer, and use the finish to process the old Win32 code and new .NET definition, as shown below:

· {$ IFDEF WIN32}

· Psortarray = ^ TsORTARRAY;

· TsortArray = array [0..maxint Div Sizeof (Integer) - 1] of integer

{$ Else}

· Tsortarray = array of integer;

{$ ENDIF}

Press SHIFT F2 to save files in all projects, then press SHIFT F9 to re-add

Now, you will get an error message about FSORTARRAY, because PsortArray is now an unknown type. Use the .NET sector, we can simply define a fsortarray. Type TsortArray. Type of FSORTARRAY from PsortArray Change to TSORTARRAY, if you want to use the fingertorge from the WIN32 and .NET code, then you will look like this:

· Tsortthread = Class (TTHREAD)

Private

· FBOX: TPAINTBOX;

· Fsortarray: {$ IFDEF WIN32} PsortArray {$ ELSE} TsortArray {$ ENDIF};

Press SHIFT F2 to save files in all projects, then press SHIFT F9 to re-add

Now, you will get a wrong prompt message, prompt to use the @ operator in the Create constructor, now this is no longer used in this package.

Remove the @ operator used in the CREATE constructor, use the finished indicator, now the code should look like this:

Conntructor Tsortthread.create (Box: TpaintBox; Var SortArray: array of integer);

Begin

· Inherited Create (false);

· Fbox: = box;

· Fsortarray: = {$ IFDEF WIN32} @ {$ ENDIF} SoteArray;

· Fsize: = high (sotarray) - Low (sORTARRAY) 1;

FreeOnterminate: = true;

· END;

Press SHIFT F2 to save files in all projects, then press SHIFT F9 to re-add

The final error message is about using ^ operators in the Execute method. Obviously, you can process it in the .NET segmentation, modified as follows:

Procedure tsortthread.execute;

Begin

// sort (slice (fsortarray ^, fsize);

Sort (fsortarray {$ IFDEF WIN32} ^ {$ ENDIF});

END;

* Note that the function sort has a VAR parameter, so don't worry that the entire Interger number of intergers will be transmitted as a value (in addition, you have to pay attention to check if you need to add a const keyword)

Press SHIFT F2 to save files in all projects, then press SHIFT F9 to re-add

We have no prompts for unsafe code, with only a Warning of Platform Specific. Now it is already a local, secure controlled .NET executable, using Vcl for .NET. This Threads demonstrates rapid but not perfect (unsafe) portions, then transplanted it from Win32 to a safe, fully controlled .NET For Delphi VCL application.

Last continued

转载请注明原文地址:https://www.9cbs.com/read-56368.html

New Post(0)