As you probably know, C++Builder was created from Delphi. Much of what C++Builder contains comes directly from Delphi. Sometimes this can be frustrating, but there are some benefits. There is a wealth of Delphi example code available (on the Internet and from other sources) that can be a great aid in developing C++Builder applications. In some cases this code can be used directly. I other case the code can be converted for use in C++Builder. Further, there are many example components available for Delphi for which there is no C++Builder example.
C++Builder has a built-in Pascal compiler. The Pascal compiler allows you to use Delphi code in C++Builder. It can also aid in converting code from Delphi to C++Builder. The Pascal compiler is available both from within the C++Builder IDE (transparently) or via a command line compiler.
Often you will find Delphi example projects that contain a unit that you want to use in your applications. The simplest way of using a Delphi unit is to simply add it to your project. Here are the steps to take to add a Delphi unit to a C++Builder project:
Create your C++Builder project.
Select Add to Project from the C++Builder toolbar or menu.
Select "Pascal unit" from the Files of Type combo box on the Open dialog box.
Select a Delphi unit to add to your project and click OK.
Build the application before writing any code that references the Delphi unit. This builds the Delphi unit and creates a header you can include in your application.
Choose File | Include Unit Hdr… from the C++Builder main menu and add the Delphi form to your application.
Write code that references the Delphi unit.
When you build the application, C++Builder uses its built-in Pascal compiler to create an OBJ that the application can use. The Pascal compiler also generates a header from the Pascal source. Using Delphi units this way is very easy.
As you can see, adding a Delphi unit to your project is quite simple. However, you may not want to use a Delphi unit in this way. You may, for example, require that all of your code be in C++. In this case you will have to convert the code from Pascal to C++.
There isn’t any practical way for me to explain every detail of converting Delphi code to C++. What I can do, however, is show you how to easily convert some of the tricky Pascal declarations to C++.
Let’s say, for example, that you have a Delphi unit that looks like this (obviously simplified for this example):
unit TestUnit;
interface
type
MyEnum = (meOne, meTwo, meThree);
function DoSomething(Value : MyEnum) :
string;
var
I : Integer;
Buffer : array [0..255] of Char;
implementation
function DoSomething(Value : MyEnum) :
string;
begin
case Value of
meOne : Result := 'One';
meTwo : Result := 'Two';
meThree : Result := 'Three';
end;
end;
end.
Even without knowing Pascal you can probably manage to convert this unit manually. However, you can get a head start by using C++Builder’s Pascal compiler to create a header for this unit. You could add the unit to your C++Builder application and compile, but you can also use the command line compiler. Here are the steps:
1.Open a command prompt box and navigate to the folder containing the Delphi unit.
2.At the command prompt, type the following:
dcc32 -jphn TestUnit.pas
DCC32.EXE is the Pascal compiler. The -jphn switch tells the Pascal compiler to create a header and an OBJ file compatible with C++Builder. When this command executes, the file is compiled and a header and OBJ are created (the OBJ is not really significant in this case since you won’t be using it anyway). The header generated for the test unit looks like this (with comment lines removed for clarity):
#ifndef TestUnitHPP
#define TestUnitHPP
#pragma delphiheader begin
#pragma option push -w-
#pragma option push -Vx
#include <SysInit.hpp> // Pascal unit
#include <System.hpp> // Pascal unit
namespace Testunit
{
#pragma option push -b-
enum MyEnum { meOne, meTwo, meThree };
#pragma option pop
extern PACKAGE int I;
extern PACKAGE char Buffer[256];
extern PACKAGE AnsiString __fastcall
DoSomething(MyEnum Value);
} /* namespace Testunit */
#if !defined(NO_IMPLICIT_NAMESPACE_USE)
using namespace Testunit;
#endif
#pragma option pop // -w-
#pragma option pop // -Vx
#pragma delphiheader end.
#endif // TestUnit
This is still a bit muddied by various compiler options, but here is the relevant part:
enum MyEnum { meOne, meTwo, meThree };
int I;
char Buffer[256];
AnsiString __fastcall
DoSomething(MyEnum Value);
Notice how the declarations are conveniently created for you. You still have to convert the actual code in the unit, but at least the declarations are done for you.
Here’s another example, only slightly more complex:
const
MaxSize = MaxLongInt;
type
TDoubleArray = array[0..
(MaxSize div SizeOf(Double))-1]
of Double;
PDoubleArray = ^TDoubleArray;
TIntArray = array[0..
(MaxSize div SizeOf(Integer))-1]
of Integer;
PIntArray = ^TIntArray;
The generated declaration looks like this:
typedef double TDoubleArray[268435455];
typedef double *PDoubleArray;
typedef int TIntArray[536870911];
typedef int *PIntArray;
The previous examples are fairly simple. Some Pascal declarations, however, can leave you scratching your head wondering how to convert them to C++. Here’s an example:
TMyCallback = function(const S : string;
Size : Integer) : Integer;
This is a declaration for a callback function. When you compile this unit with the Pascal compiler you get a header that contains this declaration:
typedef int __fastcall (*TMyCallback)
(const AnsiString S, int Size);
Perhaps you would have easily figured out how to convert the Pascal code to this declaration, but it is unlikely unless you have expertise with both Pascal and C++. The point is, of course, that the header generation feature of the Pascal compiler makes it simple to convert any Pascal declaration to C++.
I could provide even more complex examples, but I think you get the point.
There are many example, shareware, and freeware components available for Delphi. In most cases, the author does not include a C++Builder equivalent. Components including Delphi source code can typically be used with little or no modification. The steps to use a Delphi component are:
1.Create a new package for the component. Typically you will create a package that is both a run-time and a design package.
2.Add the Delphi source code for the component to the package.
3.Build the package and install it.
Granted, this process is simple, but many C++Builder programmers don’t realize that Delphi components can be used in this way.
There is a large amount of Delphi code available on the Internet. The ability to use this code in your applications is certainly a benefit. Knowing that you can use this code, and exactly how to use it is the key.