Delphi
2005 Win32 program ported to .NET
(the second part)
Borland® Delphi® 2005 Migration To .NET Using Vcl for .Netby Bob Swart, Bob Swart Training & Consultancy, Translate Into Chinese By Visli.
Data Access We have ported a VCL application to .NET, and many programs in practical applications contain data access, now we transplanted a big bit of such procedures. Let's take a look at Delphi7 / Demos. / DB directory. There are several sample programs in this, MastApp and IBMastApp are the largest, using a lot of units and forms.
If you don't have Delphi 7, then go to the BDS / 3.0 / DEMOS / DELPHIWIN32 / DB directory, which also has the same functionality MASTAPP and IBMastApp projects (but they have been ported to new engineering format - containing a .bdsproj file).
MastApp has been ported to .NET, we can find it in the BDS / 3.0 / DEMOS / DELPHI.NET / DB / MASTAPP directory, but the IBMastApp project has not been ported to VCL for .NET, so we can't be in BDS / 3.0 / Demos It is found in /Delphi.net/db or bds / 3.0 / demos / delphi.net / vcl / db directory.
Therefore, we use it as our usage, let our IBMastApp application transplant from Win32 to .NET - a nice database sample program.
First, let's create a copy of the necessary file.
Create an IBMASTAPP subfolder in the BDS / 3.0 / DEMOS / DELPHI.NET / VCL / DB directory. Used as our work directory. Copy all the files in BDS / 3.0 / DEMOS / DELPHIWIN32 / VCLWIN32 / DB / IBMASTAPP directory to BDS / 3.0 / DEMOS / DELPHI.NET / VCL / DB / IBMASTAPP directory. Delete the mastapp.bdsproj file.
Now we are ready to work on new IBMastApp projects and port it to .net.
Run Delphi 2005 Open the MastApp.dpr Project file from the BDS / 3.0 / DEMOS / DELPHI.NET / VCL / DB / IBMASTAPP directory in Delphi.
Since the project is now there is no .bdsproj file to associate it, Delphi 2005 IDE will ask you to update it to Win32 project or .NET Project. Below is Engineering Update dialog:
Update MastApp Project to .NET
Select the Delphi for .NET option, then click the OK button.
This will produce a new mastapp.bdsproj file, stores the .NET instruction feature. We now save the project.
Select the File | Save all menu so that the MastApp project is saved (including new mastapp.bdsproj files).
Transplant Data Module Now we have associated engineering as Delphi for .NET, we first need to determine if the database is correct. This is what you need to do before you try to compile. Once the database connection is correct, we can concentrate Energy on the transplantation of the source code.
Turn on the data module, that is, the DataMod.PAS file.
The design window of the data module should be roughly shown below. This program uses Interbase Express as a data access technology. (Translation: You need to install Interbase, otherwise the data connection component in the picture below is not displayed)
VCL for .NET data module
We need to reconfigure the Database component, located in the lower right corner of the data module (red circle above).
Select Database components on the data module (it is a TibDatabase Type). Right-click on the Database component, which will display a pop-up menu to tell you the interbaseExpress version (9.09) and provide a menu item for a Database Editor. Select Database Editor, you will see the following dialog: Database component editor
Note the D: /. You should point the path to the specific location of the Interbase database file mastsql.gdb in your computer. In my machine, Mastsql.gdb is located in C: / Program Files / Common Files / Borland Shared / Data / Mastsql.gdb.
Click the TEST button to verify if the interbase database mastsql.gdb can be connected correctly. If you can't, confirm that the location of the Mastsql.gdb file is correct, and the Interbase database service is running. Once you get the "Successful Connection" prompt You can turn off the database component editor.
* Note: Most of the work needs to be completed when you transplant the VCL project to .NET: First confirm that the data access component refers to the correct database. Most VCL data access components have corresponding VCL for .NET SQL Links is not supported in .NET, you can use DBEXPRESS, DBGO for ADO, InterBaseExpress, DBGO for ADO, InterBaseExpress, or other VCL for .NET data access technology to complete transplantation.
Once the transfer source code is completed, we can start the first compilation project.
Save all files. Press CTRL F9 to build MastApp projects for the first time.
This will bring you approximately 17 warning information and 1 error message, as shown below:
[Warning] mastapp.dpr (23): w1005 unit 'borland.vcl.forms' is Specific To a platform
[Warning] main.pas (6): w1005 unit 'borland.vcl.windows' Is Specific To a platform
[Warning] main.pas (6): w1005 unit 'borland.vcl.Messages' Is Specific To a platform
[Warning] main.pas (6): w1005 unit 'borland.vcl.graphics' Is Specific To a platform
[Warning] main.pas (6): w1005 unit 'borland.vcl.controls' Is Specific To a platform
[Warning] main.pas (7): w1005 unit 'borland.vcl.Forms' Is Specific To a platform
[Warning] main.pas (7): w1005 unit 'borland.vcl.dialogs' Is Specific To a platform
[Warning] main.pas (7): w1005 unit 'borland.vcl.Buttons' Is Specific to a platform
[Warning] main.pas (7): w1005 unit 'borland.vcl.stdctrls' Is Specific To a platform
[Warning] main.pas (7): w1005 unit 'borland.vcl.menus' Is Specific to a platform
[Warning] main.pas (7): w1005 unit 'borland.vcl.extCtrls' is specific to a platform [Warning] DATAMOD.PAS (8): w1005 unit 'borland.vcl.windows' Is Specific to a platform
[WARNING] DATAMOD.PAS (8): W1005 Unit 'Borland.vcl.Messages' Is Specific To a Platform
[WARNING] DATAMOD.PAS (8): W1005 Unit 'Borland.vcl.graphics' IS Specific To a Platform
[Warning] DATAMOD.PAS (8): W1005 Unit 'Borland.vcl.controls' Is Specific To a Platform
[WARNING] DATAMOD.PAS (8): w1005 Unit 'Borland.vcl.Forms' Is Specific To a Platform
[WARNING] DATAMOD.PAS (8): W1005 Unit 'Borland.Vcl.dialogs' Is Specific To a Platform
[Fatal Error] Datamod.Pas (9): F1026 File Not Found: 'Varutils.dcuil'
Warning information can be ignored - these warnings just tell us that the VCL for .NET we use is for special platforms. To cancel this warning function, you can disable it.
Select Menu Project | Options Open Options dialog. In the Compiler Messages classification, you uncheck the "Platform Unit" warning (as shown below). If you compile the project, you will find that the warning message will no longer appear. .
Project Options - Warning Information
Press SHIFT F9 to recompile. No longer the first 17 warnings, but the error is still.
[Fatal Error] Datamod.Pas (9): F1026 File Not Found: 'Varutils.dcuil'
This error message is caused by VCL for .NET, the Varutils unit is no longer (it is likely to have a VariantS unit). So we can remove the no longer existing Varutils unit from the USES clause.
Delete Varutils in the USES clause of DataMod.PAS.
* Note that if you want your project to be compiled under Win32, you can put an {$ IFDEF WIN32} .... {$ ENDIF} compile instruction on the VARUTILS unit reference.
You can modify the USES clause as follows: USES
Windows, Messages, Sysutils, Classes, Graphics, Controls, Forms, Dialogs,
DB, IbQuery, IbcustomDataSet, IBTable, IBDatabase, IB, Variants
{$ IFDEF WIN32}, VaruTils {$ ENDIF};
Save all files, press SHIFT F9 to recompile.
3 prompts and 2 errors will appear.
[Error] edcust.pas (54): E2010 Incompatible Types: 'Variant' and 'DOUBLE'
[Fatal Error] Brcstord.pas (48): F2063 Could Not Compile Used unit 'edcust.pas' caused by incompatible code as follows:
Procedure Tedcustform.ED (Custno: double);
Begin
Mastdata.cust.open;
Mastdata.cust.locate ('Custno', Custno, []);
ShowModal;
END;
The error message is caused by the CustNO parameter of MastData.cust.locate. Its type is Double, but it is a Variant type. This should have no problem, but the compiler needs to have a VariantS unit to add to the USES clause.
Add a Variants unit to the EDCUST.PAS unit IMplementation area. Save the file, press SHIFT F9 to recompile.
This will return 3 prompts and 1 error.
[Fatal Error] Edorders.Pas (10): F1026 File Not Found: 'DBLOOKUP.DCUIL'
IT APPEARS That The DBlookUp Unit is Also No longer available in vcl for .net - Or Required for this Project. We can Safely Remove IT.
Remove the dblookup unit from the uses clauses of the interface section of edorders.pas.
* Note that if you want your project to be compilable to a Win32 target as well, you may want to place the DBLookup unit in an {$ IFDEF WIN32} .... {$ ENDIF} block instead of just removing the unit from the Uses clause.
If you decide to use theness, modify, modify, modify, modify, modify, ip, ipooks as Follows: Uses
SYSUTILS, Windows, Messages, Classes, Graphics, Controls,
Dialogs, Forms, Stdctrls, DBGRIDS, DBCTRLS, DB,
Buttons, Grids, {$ IFDEF WIN32} DBLOOKUP, {$ ENDIF} Extctrls, Mask
Press SHIFT F2 TO SAVE All Files in The Project, And The Press Shift F9 To Rebuild The Project.
THIS WILL RESULT IN 3 hints and 4 errors.
[Error] srchdlg.Pas (54): E2010 Incompatible Types: 'Variant' and 'DOUBLE'
[Error] srchdlg.Pas (64): E2010 Incompatible Types: 'Variant' and 'DOUBLE'
[Error] srchdlg.pas (98): E2010 Incompatible Types: 'Variant' and 'Tcaption'
[Fatal Error] EDORDERS.PAS (76): F2063 Could not compile used unit 'SrchDlg.pas'The first three problems are very similar to an error that we saw before, which could simply be solved by adding the Variants unit to the uses Clause.
Add The Variants Unit To The Uses Clause of the Implementation Section of Unit Srchdlg.Pas. Press Shift F2 To Save All Files in The Project, And The PRESS SHIFT F9 TO REBUILD The Project.
This Will Result in 3 Hints, 1 Warning, And 2 Errors.
[Error] Edorders.Pas (105): E2010 Incompatible Types: 'Variant' and 'DOUBLE'
[Warning] edorders.pas (229): w1050 widechar reduced to byte char in set expnesss
[Fatal Error] Brcstord.Pas (48): F2063 Could Not Compile Used Unit 'Edorders.PAS'
.................... ...
Add The Variants Unit to The Uses Clause of the Implementation of Unit Edorders.Pas. Press Shift F2 To Save All Files in The Project, and The Press Shift F9 To Rebuild The Project.
This Will Result In 3 Hints, 1 Warning, And 3 Errors.
Add The Variants Unit To The Uses Clause of the Implementation of Unit Brcstord.Pas. Press Shift F2 To Save All Files in The Project, and The Press Shift F9 To Rebuild The Project.
This Will Result In 3 Hints, 1 Warning, And 1 Error.
REMOVE DBLOOKUP AS CLAUSE (Interface) of edparts.pas, or place the dblookup unit in an {$ ifdef Win32} BLOCK As Follows: Uses
SYSUTILS, Windows, Messages, Classes, Graphics, Controls,
Forms, Dialogs, DB, Stdctrls, Extctrls, Mask, Dbctrls,
{$ IFDEF WIN32} DBLOOKUP, {$ ENDIF} Buttons;
.
Add The Variants Unit to The Uses Clause of the Implementation section of Unit Edparts.Pas. Press Shift F2 To Save All Files in The Project, and The Press Shift F9 To Rebuild The Project.
This Will Result in 3 Hints, 1 Warning, And 2 Errors.
Add The Variants Unit to The Uses Clause of The Implementation section of Unit Brparts.Pas. Press Shift F2 To Save All Files in The Project, And The PRESS SHIFT F9 TO Rebuild The Project.
If you are running the updated version of Delphi 2005, then your application will compile and link at this time and you can move on to the section "Running the Marine Adventure Order Entry Application". If you find that you still have some errors, then Please payue on here.
This Will Result In 3 Hints, 1 Warning, And 1 Fatal Error.
[Hint] DATAMOD.PAS (244): H2443 Inline Function 'ExpandFileName' HAS NOT BEEN Expanded
Because Unit 'System.io' Is Not Specified in Usees List
[Hint] DATAMOD.PAS (743): H2443 Inline Function 'FileExists' Has NOT BEEN Expanded Because
Unit 'system.io' is not specified in usees list
[Hint] DATAMOD.PAS (748): H2443 Inline Function 'ExtractFileName' HAS NOT BEEN Expanded
Because Unit 'System.io' Is Not Specified in Usees List
[Warning] edorders.pas (229): w1050 widechar reduced to byte char in set expnesss
[Fatal Error] Custrpt.Pas (6): F1026 File Not Found: 'Quickrpt.dcuil'
At this time, you will also get a design-time error dialog that mentions properties which can not be found when the Delphi 2005 VCL Designer tries to display the CustRpt unit.No QuickReportsThe problems are caused by the fact that the Quickrpt.dcuil unit can not be found by the CustRpt.pas unit: QuickReport for .NET is not included with Delphi 2005. The recommended way to migrate this functionality, is to use Rave Reports for example or look for a .NET version of QuickReports, or find some other reporting. Framework to use.
Reporting is Left As Exercise for the Reader, But West Need To Make The Project Compile - Even WITHWITHE Reporting Functionality.
Do Project | View Source to open the project source file mastapp.dpr, and look for the QuickReports units in the uses clause (these are the lines with the TQuickRep types), and place them in comments as follows: uses
Forms,
Main in 'main.pas' {mainform},
BrPARTS in 'brparts.pas' {brpartsform},
QRYCUST IN 'QRYCUST.PAS' {querycustdlg},
Edparts in 'edparts.pas' {edpartsform},
BRCSTORD IN 'BRCSTORD.PAS' {Brcustordform},
EDCUST IN 'edcust.pas' {edcustform},
Edorders in 'edorders.pas' {edorderform},
Srchdlg in 'srchdlg.pas' {searchdlg},
Splash in 'splash.pas' {splashform},
Pickdate in 'pickdate.pas' {brdateform},
About in 'About.pas' {Aboutbox},
PickRep in 'pickrep.pas' {pickrpt},
// Custrpt in 'Custrpt.Pas' {CustomerbyInvoiceReport: tquickrep},
// ORDERRPT IN 'ORDERRPT.PAS' {ORDERSBYDATEREPORT: TQUICKREP},
// INVCRPT IN 'INVCRPT.PAS' {InvoicebyRDERNOREPORT: TQUICKREP},
Pickinvc in 'pickinvc.pas' {pickordernodlg},
DataMod in 'DataMod.pas' {MastData: TDataModule}; A nice quick way to place source lines in comments is to press the Ctrl / keys This keystroke will toggle a line of code from uncommented to commented, and move the cursor to the. Next line. so you can Quickly Place All Three Lines in Comments.
.
. Move to the bottom of the project source file, and locate the three CreateForm statements that create the QuickReports forms Select them in a block, and press Ctrl / to place them all in comments, resulting in the following code: begin
Application.INITIALIZE;
Splashform: = Tsplashform.create (Application);
Splashform.show;
Splashform.Update;
Application.title: = 'Marine Adventures Order Entry';
Application.helpfile: = 'mastapp.hlp';
Application.createform (TMAINFORM, MainForm);
Application.createform (TBRPARTSFORM, BRPARTSFORM);
Application.createform (tquerycustdlg, querycustdlg);
Application.createform (TedPartsform, edpartsform);
Application.createform (TBRCUSTORDFORM, BRCUSTORDFORM);
Application.createform (TEDCustform, edcustform);
Application.createform (TEDORDERFORM, EDORDERFORM);
Application.createform (TsearchDLG, SearchDLG);
Application.createform (TBRDATEFORM, BRDATEFORM);
Application.createform (Taboutbox, AboutBox);
Application.createform (TPICKRPT, PICKRPT);
// Application.createForm (TCustomerbyInvoiceReport, CustomerbyInvoiceReport);
// Application.createForm (TordersByDateReport);
// Application.createform (TinvoiceByOrdernoreport, InvoicebyOrdernoreport);
Application.createform (TPICKORDERNODLG, Pickordernodlg); Application.createform (TMastData, Mastdata);
Splashform.hide;
Splashform.free;
Application.run;
End.
...........................................
DO Search | Find in Files, And Look for Custrpt (Which Could Be in The Uses Clause of some Other Units, and Should the Be Removed).
Delphi 2005 Find in Files
* Note That We can group the results by file, and the results will be displayed in a new treeview. As a result, the main.pas unit is shown.
Open file main.pas, and take a look at The Uses Clause of the us....
* Note That Error Insight Automatically Marks This Unit, AS Well As The ORDERRPT AND INVCRPT UNITS AS BEING INVALID (The Compiler Cannot Resolve The Unit Names).
Cannot Resolve Unit Names- Plus Other UncompilaBle Code
Use Ctrl / To Place The Custrpt, Orderrpt, And Invcrpt Units In Comments. Press Shift F2 To Save All Files in The Project, and The PRESS SHIFT F9 TO Rebuild The Project.
The Next Error Is Shown In The PrintCustomerReport Method.
Select the code inside the PrintCustomerReport method and place it in comments, for example as follows (feel free to select more or less code to comment): procedure TMainForm.PrintCustomerReport (Preview: Boolean);
Begin
With mastdata.custbylastinvquery do
Begin
Open;
// if preview then
// CustomerbyInvoiceReport.preview
// else
// CustomerbyInvoiceReport.print;
CLOSE;
END;
END;
The Next Problem Can Be Found in The PrintOrderReport Method, Which Again Should Be Placed in Comments.
SELECT The CTE INSIDE THE PRINTORDERREPORT METHOD, AND PRESS CTRL / TO PUT EVERYTHING IN Comments, As Follows: PrintOrderReport in Comments
There's One Place Left: The PrintInvoiceReport.
Select the code inside the PrintInvoiceReport method and place it in comments, for example as follows (feel free to select more or less code to comment): procedure TMainForm.PrintInvoiceReport (Preview: Boolean);
Begin
IF pickordernodlg.showmodal = mrok dam
// if preview then
// invoicebyordernoreport.preview
// else
// InvoicebyRDERNOREPORT.PRINT;
END;
Press SHIFT F2 TO SAVE All Files in The Project, And The Press Shift F9 To Rebuild The Project.
THIS TIME, The Project Compiles and Links WITHOUT PROBLEMS.
DatabasePathThere's one more thing that needs some attention. If you run the application now, there's a chance that it will work but there's also a good chance that you'll get the message that the database could not be located.
THIS Last Issue Is Actually Caused by The Code In The Application Itself. Inside The Data Module, There's A Function Called DataDirectory, Which IS Defined As Follows:
Function TMastData.DataDirectory: String;
Begin
{Assume Data is in ../../../../../common files / borland shared / data / data relative to where we are}
Result: = ExtractFilePath (paramstr (0));
// Result: = expandFileName (Result '../../data/');
Result: = expandFileName (Result '../../../../common files / borland shared / data /');
END;
As The Comments Say, It's An Assumption That The Data IS located in ../../../ ... the Demo directory structure, but is not something I would like to use when deploying the application.In fact, right at the beginning when we started to migrate this project, we made sure that the database could be connected to at design-time. So The Database.DatabaseName Is Already Pointing to The Right Place.
.
Edit the datamod.pas unit, and locate the MastDataCreate method Make sure to assignment DataFile with the value of Database.DatabaseName, overwriting the value of the DataDirectory, as follows: procedure TMastData.MastDataCreate. (Sender: TObject);
VAR
DataFile: string;
Begin
DataFile: = DATADIRECTORY 'MASTSQL.GDB';
DataFile: = Database.Database.databaseName; // Already Working At Design-Time!
IF not fileexists (datafile) THEN
IF Messagedlg ('Could Not Locate Mastsql.gdb. Would You Like To Locate The File?',
MTERROR, [MBYES, MBNO], 0) = MRYES THEN
If OpenDialog.execute THEN
Begin
IF Uppercase (OpenDialog.FileName) = 'Mastsql.gdb' Then
DataFile: = OpenDialog.FileName
Else
Raise Exception.create ('Invalid File:' OpenDialog.FileName);
end
Else
Raise Exception.create ('Cannot Locate Interbase Data File: Mastsql.gdb');
Database.DatabaseName: = datafile;
Database.open;
Transaction.startTransaction;
END;
* Note That You Can Also Remove The Call to DataDirectory Here, But You Still Need To Assign To New Value to DataFile.Press Shift F2 To Save All Files in The Project
Running the Marine Adventures Order Entry Application
Press SHIFT F9 to Rebuild The Project.
The Project Again Compiles and Runs, Showing The Marine Adventures Order Entry Application, AS Follows:
Marine Adventures Order Entry Main Application
Click on The New Order Button for the Order Form:
Order Form
Close The Order Form and Click on the browse button for the order by customer form:
ORDERS by Customer Form
Close The Orders by Customer Form and Click on The Parts Button for the Browse Parts Form:
Browse Parts Form
Close The Browse Parts Form and if you are not running on An Updated Delphi 2005, Click ON The Reports Button for the Report Selection Form:
Report Selection Form
* Note That Neither of the Three Reports Will Be Available - The Buttons Are Not Working - BECAUSE We Placed All Use of QuickReports and the Reporting Forms in Comments. Feel Free To Add your OWN Reports.
Summary and ConclusionsIn this tutorial, we have seen how to migrate examples of existing Win32 VCL applications to .NET using Delphi 2005 and VCL for .NET. We started with a threading example, which was first migrated to an unsafe .NET application, and then Turned Into A 100% Safe Native .NET Application. We the Moved ON WITH A More Complex Example Using DataBases.
The amount of effort that it takes in migrating VCL applications from Win32 to .NET is far less than it would take to rebuild the application. Delphi 2005 offers great support for migration to .NET, protecting our investments of existing Win32 applications.