⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch06.htm

📁 好书《C++ Builder高级编程技术》
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>

<HEAD>
	<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
	<META NAME="Author" Content="Steph Mineart">
	<TITLE>Ch 6 -- Sharing Code Between Delphi and C++Builder</TITLE>

</HEAD>

<BODY BACKGROUND="bg1.gif" tppabs="http://pbs.mcp.com/ebooks/0672310228/buttonart/bg1.gif" BGCOLOR="#FFFFFF">

<P ALIGN="CENTER"><IMG SRC="sams.gif" tppabs="http://pbs.mcp.com/ebooks/0672310228/buttonart/sams.gif" WIDTH="75" HEIGHT="24" ALIGN="BOTTOM"
BORDER="0"><BR>
<BR>
<A HREF="index-3.htm" tppabs="http://pbs.mcp.com/ebooks/0672310228/index.htm"><IMG SRC="toc.gif" tppabs="http://pbs.mcp.com/ebooks/0672310228/buttonart/toc.gif" WIDTH="40" HEIGHT="40" 
ALIGN="BOTTOM"
ALT="TOC" BORDER="0" NAME="toc4"></A><A HREF="ch05.htm" tppabs="http://pbs.mcp.com/ebooks/0672310228/ch05.htm"><IMG SRC="back-1.gif" tppabs="http://pbs.mcp.com/ebooks/0672310228/buttonart/back.gif"
WIDTH="40" HEIGHT="40" ALIGN="BOTTOM" ALT="BACK" BORDER="0" NAME="toc1"></A><A HREF="ch07.htm" tppabs="http://pbs.mcp.com/ebooks/0672310228/ch07.htm"><IMG
SRC="forward.gif" tppabs="http://pbs.mcp.com/ebooks/0672310228/buttonart/forward.gif" WIDTH="40" HEIGHT="40" 
ALIGN="BOTTOM" ALT="FORWARD" BORDER="0"
NAME="toc2"></A></P>
<H2 ALIGN="CENTER"><FONT COLOR="#000077">Charlie Calvert's C++ Builder Unleashed</FONT></H2>
<P>
<H2 ALIGN="CENTER"><A NAME="Heading1"></A><FONT COLOR="#000077">- 6 -</FONT></H2>
<H2 
ALIGN="CENTER"><A NAME="Heading2"></A><FONT COLOR="#000077">Sharing Code Between
Delphi and C++Builder</FONT></H2>
<P>C++Builder and Delphi have a unique relationship that enables programmers to easily
share code between the two environments. Almost 
anything you write in Delphi can
easily be used in C++Builder, and some of the code you write in C++Builder can be
used in Delphi. As a result, programmers can work in either C++ or Delphi, depending
on their skill level or inclination.</P>
<P>Despite 
their similarity, each has capabilities:

<UL>
	<LI>Delphi's ease of use enables a wide range of programmers to quickly produce high
	performance, robust components, objects, and applications.
	<P>
	<LI>C++Builder, on the other hand, allows the best 
programmers to write code in a
	language known for its flexibility, subtlety, and wide acceptance.
</UL>

<P>Because you can share code between the two environments, programmers can use the
tools that best suit their current needs. Then later on they 
can swap the code back
and forth between the two environments.</P>
<P>This chapter has somewhat unusual origins, in that I created it originally as
a white paper for use on Borland's Web sites. I have decided to include it in this
book without 
extensive revisions because it covers an important topic in a fairly
short space. Readers with good ears will hear a rather different tone in this chapter
than you find in the other sections of this book.</P>
<P>Because of its origins, this chapter 
does not contain as many sample programs
as most of the chapters in this book. However, if you look in the <TT>Chap06</TT>
directory on the C++Builder Unleashed CD-ROM, you will find a few programs illustrating
the main points of this chapter.</P>

<P>This chapter is divided into two main sections. The first part contains a summary
of the techniques for sharing code between the two environments. The second, and
longer, section of the chapter contains a more technical analysis of the methods
for 
sharing code between C++Builder and Delphi.
<H3><A NAME="Heading3"></A><FONT COLOR="#000077">Benefits of Sharing Code Between
C++Builder and Delphi</FONT></H3>
<P>This section lists some of the key benefits gained when you share code between
Delphi 
and C++Builder. These items are listed in no particular order:

<UL>
	<LI>Programmers can work in either C++ or Delphi. You don't have to worry that code
	written with one tool will not be accessible from inside the other tool. Instead,
	you can 
concentrate on finding the fastest way to create usable modules, and then
	link them in to either C++Builder or Delphi, depending on the needs of your engineers.
	Of course, the route from Delphi to C++Builder is much more open than the route from
	
C++Builder to Delphi, but it is possible to move in either direction.
	<P>
	<LI>Sharing code between Delphi and C++Builder also promotes code reuse. You can
	write an object once for a Delphi project, and then reuse it, unchanged, in a C++
	project. 
There is no performance or size penalty associated with writing code in
	Delphi rather than C++. Anything you might have heard about C++ being significantly
	faster than Object Pascal is simply untrue.
	<P>
	<LI>Delphi is extremely easy to use. It 
offers programmers a simple, easy-to-understand
	syntax. Companies can allow inexperienced programmers to work in Delphi without fear
	that their code cannot also be used in C++ projects. You can therefore have some
	of your team working full-time in 
C++, and the rest of the team working full-time
	with the much easier to use Delphi language. The efforts of both programmers can
	then be combined into a single, high-performance executable.
	<P>
	<LI>Because Delphi is so easy to use, it is often the 
language of choice during projects
	that have tight deadlines. Even your best programmers might prefer to use Delphi
	when they are rushed, or when they want to create high-performance programs as easily
	as possible. Any modules or objects created on 
these types of projects can then later
	be linked in to C++ projects. This means that there is no penalty associated with
	using Delphi, even if you are primarily a C++ shop.
</UL>

<H3><A NAME="Heading4"></A><FONT COLOR="#000077">Technical 
Highlights</FONT></H3>
<P>This section of the chapter contains an overview of the key points you need to
know about sharing code between C++Builder and Delphi. It consists of two subsections
called Overview: Using Delphi Code in C++Builder



<BLOCKQUOTE>
	<P>Overview: Using C++ Code in Delphi

</BLOCKQUOTE>

<P>When you are through reading these sections, you will have a general feeling for
how, and to what degree, you can share code between Delphi and C++Builder. In subsequent
sections, 
I will review most of these points in more detail.</P>
<P>Before beginning, you should note that Delphi uses Object Pascal, which is widely
taught in many schools. C++Builder uses C++, which is probably the most widely used
language in corporate 
America. Most programmers will know one, if not both, of these
languages.
<H4><A NAME="Heading5"></A><FONT COLOR="#000077">Overview: Using Delphi Code in C++Builder</FONT></H4>
<P>C++Builder does not care whether the units you add to your project are 
written
in C++ or in Delphi. It will compile either type of unit as if it were native to
C++Builder.</P>
<P>As a general rule, any Delphi 2.01 unit you create that compiles in Delphi should
compile unchanged in C++Builder. There are a few Delphi 
syntactical elements that
will not be accepted by C++Builder. However, these are minor exceptions, and you
will generally find that all your Delphi 2.01 or Delphi 2.0 code will compile unchanged
in C++Builder. After all, the entire VCL is written in 
Object Pascal, and it compiles
fine under BCB. The VCL consists of many thousands of lines of code, and contains
almost any imaginable Object Pascal construct.</P>
<P>In particular, the following types of code will compile in C++Builder:

<DL>
	
<DD><B>1. </B>Delphi forms<BR>
	<BR>
	<B>2. </B>Delphi units containing objects<BR>
	<BR>
	<B>3. </B>Delphi units containing procedures, functions, constants, structures, arrays,
	and so on<BR>
	<BR>
	<B>4. </B>Delphi components
</DL>

<P>You usually 
do not have to change your Delphi units at all to link them in to
C++Builder. There is no need to change the code, to create header files, or to massage
your code in any way. You can mix the types of code listed previously in any way
you like. For 
instance, you can link in to C++Builder one Delphi unit that contains
the following:

<UL>
	<LI>A form
	<P>
	<LI>An object
	<P>
	<LI>Some functions
	<P>
	<LI>A component
</UL>

<P>The unit can contain any combination of the preceding types, or of any 
other valid
Object Pascal types. These types can appear in any combination. For instance, a Pascal
unit can contain only an object, or it can contain both an object and a form. You
can link in as many Pascal units as you like, with each containing any 
combination
of the above objects. There is no limit on the number of Delphi units you can add
to a C++Builder project.</P>
<P>To link Delphi code into C++Builder you simply choose Add to Project from the
menus and then browse for files that end in 
PAS. Once you have added one or more
Delphi units to your project, they will compile and link seamlessly with the rest
of your C++ code. You may edit only the source code of the units while they are open
inside of C++Builder. You cannot edit the 
forms. You can, however, create a descendant
of a Pascal form and then edit that descendant.</P>
<P>Besides the simple techniques outlined previously, advanced programmers can also
link Delphi code into C++ by using one of the following techniques:


<DL>
	<DD><B>1.</B> Delphi dynamic link libraries (DLLs) can be easily added to your C++
	projects.<BR>
	<BR>
	<B>2.</B> You can use COM and OLE to add Delphi 2.0 or Delphi 3.0 objects to C++Builder.
	In particular, you can add Delphi objects to 
C++Builder through OLE Automation.<BR>
	The basic rules of COM, including dual interfaces.<BR>
	Creating Delphi ActiveX controls that you use in C++Builder.<BR>
	<BR>
	<B>3.</B> Delphi and C++Builder share the same Virtual Method Tables (VMTs). This
	
means you can create an object in Delphi and use it in C++Builder through the technique
	outlined in the section called &quot;Mapping Virtual Method Tables.&quot; This technique
	is very similar to what happens when you create a dual interface in OLE 
Automation.
</DL>

<H4><A NAME="Heading6"></A><FONT COLOR="#000077">Overview: Using C++ Code in Delphi</FONT></H4>
<P>C++ supports some syntactical elements such as multiple inheritance and function
overloading that make it difficult to directly link 
all C++Builder code into Delphi.
However, you can still use C++ code in Delphi projects through the following techniques:

<DL>
	<DD><B>1. </B>COM and OLE. Delphi fully supports both OLE Automation and dual interfaces.<BR>
	<BR>
	<B>2.</B> DLLs.<BR>
	
<BR>
	<B>3.</B> Direct linking of C++Builder units containing functions. (Note that Delphi
	supports OBJ files.)
</DL>

<P>The most convenient of the preceding techniques is the first, especially if your
C++ COM objects support type libraries. Delphi 
97 has full support for type libraries,
and you will be able to generate Delphi units directly from a type library. This
means that you can link C++ COM objects that support type libraries directly into
Delphi without having to write any extra code. 
All the necessary files for linking
in C++ COM objects that support type libraries will be generated automatically by
Delphi.</P>
<P>DLLs are a powerful means of sharing code between Delphi and C++Builder. Virtually
any C++Builder function that you 
create can be placed in a DLL and called directly
from Delphi. Delphi supports all the common calling conventions such as CDECL, PASCAL,
and STDCALL. You can access an object written in C++ from inside Delphi simply by
reversing the technique 
described in the section titled &quot;Mapping Virtual Method
Tables.&quot;</P>
<P>It is possible, under certain circumstances, to link C++ OBJ files directly into
Delphi. This is a powerful technique that can be very useful in certain circumstances.

However, there are limitations on what you can do with this technology, and it should
not be viewed as a primary means of porting code between the two environments.
<H4><A NAME="Heading7"></A><FONT COLOR="#000077">Summary of Techniques for Sharing

Code</FONT></H4>
<P>Before closing this section of the chapter, it might be helpful to review some
of the key points mentioned so far.</P>
<P>C++Builder has virtually no limitations on its capability to use Delphi 2.01 code.
You can simply link your 
Pascal units directly into C++Builder without modifying
them in any way. They will then compile into the same small, tight, high-performance
machine code you would expect if you wrote them in C++.</P>
<P>If you use both C++Builder and Delphi in a 
single project, you can promote code
reuse while simultaneously leveraging the skills of all your programmers. Your best
engineers can work in C++, where they can take advantage of that language's flexibility.
Inexperienced programmers, those who 
prefer Object Pascal, and programmers who are
rushed, can work in Delphi. With this division of labor, all the programmers on your
team will be productive immediately.</P>
<P>The key point is that the technology outlined in this chapter enables your 
programmers
to work in either C++ or Delphi without fear that the code will not be available
for use in all your C++ projects.
<H3><A NAME="Heading8"></A><FONT COLOR="#000077">A More Technical Analysis</FONT></H3>
<P>You have now completed the 
overview of the techniques for sharing code between
C++Builder and Delphi. For most programmers, this is where the chapter starts to
get interesting. The subject matter in the following sections starts out with the
simplest techniques, and becomes 
increasingly complex as the chapter nears its conclusion.
<H4><A NAME="Heading9"></A><FONT COLOR="#000077">Ground Rules for Linking Delphi
Code into C++Builder</FONT></H4>
<P>You have already learned that you can link Delphi code directly in to 
C++Builder
projects. In this section, I explain how this is possible, and put to rest any fears
you might have about this kind of project.</P>
<P>First, I will lay down three key ground rules:

<DL>
	<DD><B>1.</B> You cannot mix C++ and Delphi code in 
the same unit. You have to create
	separate units (modules) for each type of code.<BR>
	<BR>
	<B>2.</B> It's best if you have the Pascal source to the unit you want to use. You
	cannot link a Pascal binary file (DCU) in to a C++Builder project. You 
can, however,
	link in BCB OBJ files made from Pascal source. In other words, you must have either
	the Pascal source for the unit or an OBJ made in BCB. You can't use a DCU file. Third
	parties that don't want to distribute source to their tools can 
simply recompile
	once in BCB and then distribute the resultant binary OBJ files.<BR>
	<BR>
	<B>3.</B> You cannot edit the visual part of a Pascal form while it is open in C++Builder.
	You can, however, edit the source. If you create a descendant of 
the Pascal form,
	you can edit the descendant object with the visual tools.
</DL>

<P>Furthermore, you should note that a Pascal unit cannot usually be the project
file for a C++Builder application. However, if one small module is written in C++,
and 
everything else in the project is written in Object Pascal, the project is still,
by definition, a C++Builder project, even though the code is 98 percent written in
Object Pascal. The Entities program from Chapter 28, &quot;Game Programming,&quot;
is 
an example of this kind of project.</P>
<P>Here is a detailed description of the simple steps needed to link a Delphi unit
in to a C++Builder project:

<DL>
	<DD><B>1. </B>First, choose the Project | Add to Project menu option.<BR>
	<BR>
	<B>2.</B> 
When the Add to project dialog appears, pull down the Files of type combo
	box at the bottom of the dialog. You will then be able to select files of type CPP,
	PAS, C, RES, OBJ, or LIB, as shown in Figure 6.1. You should select PAS.<BR>
	<BR>
	
<B>3.</B> Browse for the Pascal source file you want and add it to your project.
	After you have selected the file you want to use, you can click OK to add it to your
	project and proceed exactly as you would if this were a C++ file.
</DL>

<P><BR>
<A 
NAME="Heading10"></A><A HREF="06ebu01.jpg" tppabs="http://pbs.mcp.com/ebooks/0672310228/art/06/06ebu01.jpg">FIGURE 6.1.</A><FONT COLOR="#000077">
</FONT><I>Selecting a Pascal file to add to your C++ project.</I><BR>
<BR>
The header file for your unit will be generated automatically. You don't have to
do 
anything to the unit to use it in a C++Builder project. An example of this type
of project is found on the CD that accompanies this book, in the <TT>Chap06</TT>
directory, in a project called <TT>Test1</TT>.
<H4><A NAME="Heading11"></A><FONT 
COLOR="#000077">Linking a Delphi Component in to
a C++Builder Project</FONT></H4>
<P>If you want to use a Delphi component in a C++ project, you would normally want
to first add it to the Component Palette. You do this through the same basic technique

you would use to add a C++Builder component written in C++. In particular

<DL>
	<DD><B>1. </B>Choose Component | Install from the menu.<BR>
	<BR>
	<B>2.</B> Select the Add button from the Install Components dialog, as shown in Figure
	6.2.<BR>
	
<B><BR>
	3.</B> Use the Add Module dialog to enter or browse for the name of the PAS file
	you want to use--that is, for the PAS file that contains the registration procedure
	for your Delphi component.<BR>
	<BR>
	<B>4.</B> After selecting the module, 
click the OK button in the Install Components
	dialog. C++Builder will then recompile <TT>CMPLIB32.DLL</TT>, thereby adding your
	component to the Component Palette.
</DL>

<P><BR>
<A NAME="Heading12"></A><A HREF="06ebu02.jpg" tppabs="http://pbs.mcp.com/ebooks/0672310228/art/06/06ebu02.jpg">FIGURE 
6.2.</A><FONT COLOR="#000077">

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -