3
votes

I am trying to use a Delphi function in a C++ Builder project (because ImageEn's IEVision component does not have a C++ Interface that works to extract the barcodes yet, so I need to extract the barcodes in a Delphi unit)

I create a C++ Builder project, and add a Delphi Unit, whose complete code is:

unit Unit2;

interface

implementation
function MyOutput : String ;
begin
  Result := 'hello';
end;

end.

I use that unit in my C++ form:

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "Unit2.hpp"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
String result = MyOutput();
}

The generated .hpp is:

// CodeGear C++Builder
// Copyright (c) 1995, 2013 by Embarcadero Technologies, Inc.
// All rights reserved

// (DO NOT EDIT: machine generated header) 'Unit2.pas' rev: 26.00 (Windows)

#ifndef Unit2HPP
#define Unit2HPP

#pragma delphiheader begin
#pragma option push
#pragma option -w-      // All warnings off
#pragma option -Vx      // Zero-length empty class member 
#pragma pack(push,8)
#include <System.hpp>   // Pascal unit
#include <SysInit.hpp>  // Pascal unit

//-- user supplied -----------------------------------------------------------

namespace Unit2
{
//-- type declarations -------------------------------------------------------
//-- var, const, procedure ---------------------------------------------------
}   /* namespace Unit2 */
#if !defined(DELPHIHEADER_NO_IMPLICIT_NAMESPACE_USE) && !defined(NO_USING_NAMESPACE_UNIT2)
using namespace Unit2;
#endif
#pragma pack(pop)
#pragma option pop

#pragma delphiheader end.
//-- end unit ----------------------------------------------------------------
#endif  // Unit2HPP

Notice that the declaration of the MyOutput function is not included in the .hpp file. How do I get Delphi to put that function into the .hpp

1
Sorry to those who object to a such a basic Delphi question, but as I noted, I use RAD Studio for C++ Builder, and only drop into Delphi when C++ Builder won't do what I need. I generally try to use example code to start with, but was having trouble because my actual prototype required an object that was defined in one of the units in "uses". Googling for "Delphi Interface" did not solve the problem for me because "Interface" is so overloaded, and @DavidHefferman 's answer and comment was exactly what I needed.nachbar

1 Answers

5
votes

The generated header file only contains symbols from the interface section. You only declared the function in the implementation section. That function cannot be called from Delphi code either.

You must also declare the function in the interface section in order for it to be visible to other units.

unit Unit2;

interface

function MyOutput : String ;

implementation

function MyOutput : String ;
begin
  Result := 'hello';
end;

end.