Win32 program in Delphi 2005 is transplanted to .NET (Part 1)

xiaoxiao2021-03-06  42

Win32 program in Delphi 2005 is transplanted to .NET

(first part)

Borland® Delphi® 2005 Migration To .NET Using Vcl for .Netby Bob Swart, Bob Swart Training & Consultancy, Translate Into Chinese By Visli.

Abstract - This article demonstrates how to use VCL (for .NET) in Borland Delphi 2005, available VCL for Win32 applications to Microsoft .NET Framework.

Introduction VCL and VCL for .NET data access summary

Introduction, we will use the Borland VCL (Visible Component Library) to Microsoft .NET Framework. These two ready-made vcl for win32 applications come from DELPHI 7 DEMOS directory - The Delphi 2005 will be opened and ported to the .NET framework with Vcl for .NET.

We will demonstrate how to solve some problems in the transplant process, although it is a small problem but it is very meaningful.

VCL and VCL for .NET now VCL can be perfectly applied to the .NET framework, allowing us to easily port the WIN32 VCL application to .NET (VCL project porting to WinForms will also become possible).

Delphi 2005 comes with a large number of sample programs in the BDS / 3.0 / DEMOS directory. There are two subdirectors of Delphi.net and DelphiWin32. BDS / 3.0 / DelphiWin32 / Vclwin32 / Directory contains several Win32 VCL routines, where large Part has been ported to .NET, readers can be found in the BDS / 3.0 / Delphi.net / VCL directory.

There is still a Threads routine that is not ported to .NET, which is used to demonstrate rapid sorting in multi-threads, select sorting and bubble method. This Threads project we will use vcl for .net Transplanted to .NET Win32 VCL app.

First, we have to create a copy of the necessary file.

Create a Threads subdirectory in the BDS / 3.0 / DEMOS / DELPHI.NET / VCL directory. So, our transplant procedures can be used as another example of Delphi for .NET VCL. Put BDS / 3.0 / Demos / All files in the DelphiWin32 / Vclwin32 / threads directory are copied to the BDS / 3.0 / DEMOS / DELPHI.NET / Vcl / Threads directory. Delete this file for this file, which is a Delphi Win32 project.

Now let's start transplant the Threads project to .net.

Run Delphi 2005 Open Project files from BDS / 3.0 / DEMOS / DELPHI.NET / VCL / THREADS directory THRDDEMO.DPR

Due to the association of the.bdsproj file, open the project file, Delphi 2005 IDE will ask you to update to a Win32 project or .NET project. The following figure is the Project Update dialog:

THRDDEMO project is updated to .NET

Select the Delphi for .NET radio box, click OK to determine.

This will generate a new THRDDEMO.BDSPROJ file, specified some of the "features of .NET. We now save the project.

Click on the main menu File | Save All so that thrddeemo project includes a new THRDDEMO.BDSPROJ file that is saved. Press Ctrl F9 to compile THRDDEMO projects for the first time.

You will get 11 warnings and 5 errors as shown below:

[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 temporarily ignore all warning messages, will handle them later. First, let's fix these compilation errors.

The first error occurred in the 18th line of the sortthds.pas file, described as "unsafe pointer only allowed if compiling with {$ unsafecode on}" (unless you use {$ unsafecode on}. Cause it The actual code of the alarm is as follows, which is used to declare a psortarray variable in the definition of the TsortThread class:

Type

PsortaRray = ^ TsortArray;

TsortArray = array [0..maxint Div Sizeof (Integer) - 1] of integer; tsortthread = class (tthread)

Private

FBOX: TPaintBox;

FSORTARRAY: PSORTARRAY;

PsortArray is a TSORTARRAY type pointer, and pointers are not a safe type, so the error alarm and other unsafe error alarms are related to this unsafe pointer type.

Although there is no unsafe type or code at all, we first allow the compiler to compile, then replace these unsafe parts with a secure code.

Adding a compile instruction {$ unsafecode on} before the definition of the TsortThread class, so that FSORTARRAY can be accepted. Save all files of the project, then press SHIFT F9 to recompile the project.

Ignore those warning messages, you will get four error messages.

[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'

The first is an unknown identifier Point. The error is as follows:

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

Begin

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

END;

The above code should be called Point twice, but I don't know why, .NET compiler can't find the Point function. This is a good case that demonstrates how to use reconstruction techniques - Find Unit function, this Function is used to help us locate missing units (Point function definitions) and add it to the USES clause.

In the code editor, move the mouse to the Point identifier, right-click the mouse pop-up menu, select the Find Unit submenu item on the Refactor menu item.

This will pop up the Find Unit dialog and Point has been automatically filled in the Search condition. And many units have also been proposed, including borland.vcl.types.point, which should be the unit that defines the Point.

Select the Borland.vcl.types.point unit, the interfabs below the dialog box is used to indicate that the add cell is referenced to that section, here is option, click OK.

Reconstruction Technology - Find Unit

Save all files, press SHIFT F9 to recompile.

Now it will return to three errors. In short, the last fatal error (FATAL ERROR) is actually due to two errors in front, using unfinished unit compilation. So in fact there are only two errors to be corrected.

[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'

Because we are still using unsafe pointers FSORTARRAY, these two error reports are triggered. The first error involves a line of Create constructor, in which we get the address of the SortArray parameter and assign it to an unsafe FsortArray pointer This code is unsafe, unless we add a UNSAFE keyword to the Create constructor, the compilation will not succeed. Add the unsafe keyword in the implementation of the Create constructor, as shown below: 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;

Save all files, press SHIFT F9 to recompile.

This, unsafe errors in the Create constructor 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 reason for this error message is due to the initialization of some special attributes before calling the Inherited constructor. However, unless you have a better reason to do this, we recommend before the custom constructor code Call inherited. Of course this is easy to modify.

In the CREATE constructor implementation code, move the inherited call to the first line: Constructor Tsortthread.create (box: tpaintbox; var sortarray: array of integer; unsafe;

Begin

Inherited Create (False);

FBOX: = Box;

FSORTARRAY: = @SortArray;

Fsize: = high (sORTARRAY) - LOW (SortArray) 1;

FreeOnterminate: = true;

END;

Save all files, press SHIFT F9 to recompile.

Now we have only one error to modify, this is another 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, in the Execute method, we use ^ operations on the unsafe pointer FsortArray. It is best to modify it to add the Unsafe keyword.

Add a Unsafe keyword to the implementation section of the Execute method, as shown below: Procedure Tsortthread.execute; unsafe;

Begin

Sort (slice (fsortarray ^, fsize);

END;

Save all files, press SHIFT F9 to recompile. [Error] Sortthds.Pas (114): E2454 SLICE Standard Function Not ALOWED for VAR NOR OUT ARGUMENT [Fatal Error] thsort.pas (39): F2063 Could Not Compile Unit 'Sortthds.pas'

This has encountered a big problem. Due to some reason, the SLICE function under .NET exists when the parameters are passed. Considering the features it implement, we can ignore the calling slice, and pass directly to FSORTARRAY ^ to Sort. Inside the sort method, we will use high and low to determine the actual size of the array. (The Slice function is used in the VCL for Win32, the prototype is Function Slice (Var A: Array; Count: Integer): Array; Slice function can only be used as parameters, instead of use alone)

Use FsortArray ^ in the sort call instead of the SLICE function, as follows: procedure tsortthread.execute; unsafe;

Begin

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

Sort (fsortarray ^);

END;

Save all files, press SHIFT F9 to recompile.

This time, there is only a warning message:

[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 warnings only tell us vcl for .NET IS specific to a platform. The last two warnings are more serious, reflecting unsafe code: @ and ^ operation. We will handle this later. Now let us ignore these warnings first Instead, it will run the project first.

.NET line sorting example

Now, the program is run as a pure .NET executable.

However, it is not a 100% secure application, so we have to go back to safe handling.

The "SAFE" code includes unsafe code, so when you check them when you check them. We continue to work hard to achieve a 100% secure .NET application.

Remove {$ unsafecode on} Compile instructions and two UNSAFE keywords (Create constructor and execute methods), ready to correct these unsafe code parts.

The first question - is also the reason for all other issues - is the definition of the pointer type. We use a "Array Of Integer" to replace the TsortArray definition. If you want to make the code, you can compile the code and under .NET Compile, you can use the compilation instruction to distinguish.

Modifying the TsortArray type is defined as an array and uses the compilation instruction to distinguish the Win32 and .NET compiler, as shown below: {$ IFDEF Win32}

PsortaRray = ^ TsortArray;

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

{$ Else}

TsortArray = array of integer;

{$ ENDIF}

Save all files, press SHIFT F9 to recompile.

This time, you will get an error message about FSORTARRAY definition, because PsortArray is now an unknown type. Use the .NET compiler, we can simply define FsortArray as a TSORTARRAY type.

Modify FSORTARRAY is defined as TsortArray. Of course, you can also use compilation instructions to distinguish, as follows: Tsortthread = Class (TTHREAD)

Private

FBOX: TPaintBox;

FsortArray: {$ IFDEF WIN32} PsortArray {$ Else} TsORTARRAY {$ ENDIF};

Save all files, press SHIFT F9 to recompile.

At this time, you will get an error message that cannot use @ operations in the Create constructor.

You can use the compilation instruction to modify as follows: Constructor Tsortthread.create (box: tpaintbox; var sortarray: array of integer);

Begin

Inherited Create (False);

FBOX: = Box;

FSORTARRAY: = {$ IFDEF WIN32} @ {$ ENDIF} SoteArray;

Fsize: = high (sORTARRAY) - LOW (SortArray) 1;

FreeOnterminate: = true;

END;

Save all files, press SHIFT F9 to recompile.

The last error is about using ^ operations in the Execute method. Similarly, we can modify the following:

Procedure tsortthread.execute;

Begin

// sort (slice (fsortarray ^, fsize); sort (fsortarray {$ IFDEF WIN32} ^ {$ ENDIF});

END;

Save all files, press SHIFT F9 to recompile.

We now only have a special warning message left, no longer have an unsafe code error. This will get a safe pure .NET executable file using VCL for .NET.

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

New Post(0)