Interface declarations

Top  Previous  Next

What is translated > Types > Records, Classes, Interfaces > Interfaces > Non-generic interfaces > Interface declarations

Delphi interfaces are declared using the interface keyword and may optionally include a GUID for COM compatibility. All interfaces in Delphi must ultimately derive from IUnknown (typically referred to as IInterface in the RTL). This base interface defines the QueryInterface, AddRef, and Release methods required for dynamic interface resolution and reference counting.

 

In Delphi2Cpp, interfaces are mapped either to C++Builder-specific syntax or to portable C++ constructs, depending on the selected target. In both cases, interface references should generally be managed through the DelphiInterface<T> smart pointer class to ensure proper reference counting and safe lifetime management.

 

Delphi

 

type

  IMyInterface = interface

    ['{12345678-1234-1234-1234-1234567890AB}']

    procedure DoSomething;

  end;

 

 

C++Builder

 

When targeting Embarcadero C++Builder, Delphi interfaces are translated using the compiler’s native __interface keyword along with __declspec(uuid(...)). This results in:

 

 

__interface INTERFACE_UUID("{12345678-90AB-CDEF-1234-567890ABCDEF}" ) IFoo : public IInterface

{

   void __fastcall DoSomething();

};

 

 

- __interface enforces pure virtual methods and COM rules automatically.

- D2C_UUID(...) expands to __declspec(uuid(...)).

 

 

Portable C++ (non-C++Builder)

 

For portable compilers, interfaces are represented as struct s with pure virtual methods. The GUID is attached using the DECLARE_UUID macro, which maps to __declspec(uuid(...)) under MSVC-compatible compilers and expands to nothing elsewhere:

 

#ifdef _MSC_VER

  #define DECLARE_UUID(x) __declspec(uuid(x))

#else

  #define DECLARE_UUID(x)

#endif

 

struct DECLARE_UUID("{12345678-90AB-CDEF-1234-567890ABCDEF}") 

IFoo : public virtual System::IInterface

{

  virtual void DoSomething() = 0;

};

 

 

__interface enforces pure virtual methods and COM rules automatically.
D2C_UUID(...) expands to __declspec(uuid(...)).
Even though C++Builder applies reference counting automatically, using DelphiInterface<T> remains recommended for exception-safe memory management.

 

 

Note on __uuidof:

 

In C++Builder and MSVC, the __uuidof(Type) operator retrieves the GUID associated with a type that is marked using __declspec(uuid("...")). This is functionally equivalent to Delphi’s '[...]' GUID syntax in interface declarations. Delphi2Cpp uses this mechanism internally to implement runtime interface lookups and to support as<T> and Supports operations.

 

For portable compilers, __uuidof is emulated via DEFINE_UUIDOF and TGuidRegistry, which provide a static association between interface names and GUIDs.

 

Visual C++ __interface

 

Support for Visual C++’s __interface (COM-style) keyword has been intentionally dropped, because it does not allow member properties, which are essential for translating Delphi interfaces with property declarations.

 

 

 

 



This page belongs to the Delphi2Cpp Documentation

Delphi2Cpp home  Content