Einzelnen Beitrag anzeigen

Benutzerbild von Desmulator
Desmulator

Registriert seit: 3. Mai 2007
Ort: Bonn
169 Beiträge
 
#3

Re: Delphi's Interfaces in C++

  Alt 1. Jun 2009, 10:04
DelphiUnit
Delphi-Quellcode:
unit BaseEngine;

interface

type
  TBaseResult = Cardinal;
  TBaseHandle = Cardinal;

  TBaseDateMonth = 1..12;
  TBaseDateDay = 1..31;
  TBaseTimeHour = 0..23;
  TBaseTimeMinute = 0..59;
  TBaseTimeSecond = 0..59;

  TBaseDateTime = record
    Year : Word;
    Month: TBaseDateMonth;
    Day : TBaseDateDay;
    Hour : TBaseTimeHour;
    Minute : TBaseTimeMinute;
    Second : TBaseTimeSecond;
  end;

  TBaseDate = record
    Year : Word;
    Month: TBaseDateMonth;
    Day : TBaseDateDay;
  end;

  TBaseTime = record
    Hour : TBaseTimeHour;
    Minute : TBaseTimeMinute;
    Second : TBaseTimeSecond;
  end;

  TBaseFileAttribute = ( bfaArchive, bfaDirectory, bfaHidden, bfaReadOnly, bfaSystem, bfaTemporary, bfaCompressed, bfaOffline, bfaNotContentIndexed, bfaEncrypted );
  TBaseFileAttributes = record
    Archive : Boolean;
    Hidden : Boolean;
    Readonly : Boolean;
    System : Boolean;
    Temporary : Boolean;
    Offline : Boolean;
    NotContentIndexed : Boolean;
    { Not changeable }
    Directory : Boolean;
    Compressed : Boolean;
    Encrypted : Boolean;
  end;

  TBaseFileOpenMethod = ( bfomClear, bfomOpen, bfomOpenExisting );
  TBaseFileAccess = set of ( bfaRead, bfaWrite );
  TBaseFileShare = set of ( bfsRead, bfsWrite, bsfDelete );

type
  IBaseInterface = interface(IInterface)
    function GetHandle : Pointer; stdcall;
  end;

  IBaseStream = interface(IBaseInterface)
    function GetSize : Cardinal; stdcall;
    function GetPosition : Cardinal; stdcall;
    function SetPosition(NewPosition : Cardinal) : Boolean; stdcall;
    function Seek(By : Integer) : Boolean; stdcall;

    function Read(var Buffer; Len : Cardinal) : Boolean; stdcall;
    function Write(var Buffer; Len : Cardinal): Boolean; stdcall;

    function Assign(Stream : IBaseStream) : Boolean; stdcall;
    function WriteTo(Stream : IBaseStream) : Boolean; stdcall;
  end;

  IBaseIOStream = interface(IBaseStream)
  end;

  IBaseFileInfo = interface;

  IBaseFile = interface(IBaseIOStream)
    function GetFileInfo(Extended : Boolean) : IBaseFileInfo; stdcall;
  end;

  IBaseFileInfo = interface(IBaseInterface)
    function GetFullPath : PChar; stdcall;
    function GetRelativPath : PChar; stdcall;

    function ExtendInformation : Boolean; stdcall;
    function ReduceInformation : Boolean; stdcall;

    function GetAttributes : TBaseFileAttributes; stdcall;
    function GetAttribute(Attribute : TBaseFileAttribute) : Boolean; stdcall;
    function SetAttributes(AttributeBlock : TBaseFileAttributes) : Boolean; stdcall;
    function SetAttribute(Attribute : TBaseFileAttribute; Value : Boolean) : Boolean; stdcall;

    function GetCreateTime : TBaseDateTime; stdcall;
    function GetLastAccessTime : TBaseDateTime; stdcall;
    function GetLastWriteTime : TBaseDateTime; stdcall;
  end;

const
  BASE_INVALID_HANDLE : TBaseHandle = $FFFFFFFF;
  BaseEngineDll = 'BaseEngine.dll';

function BaseVersion : PChar; stdcall; external BaseEngineDll;
function BaseRevision : PChar; stdcall; external BaseEngineDll;
function BaseCompiled : PChar; stdcall; external BaseEngineDll;
function BaseGetErrorStr(Value : TBaseResult) : PChar; stdcall; external 'BaseEngine.dll';
function BaseGetLastErrorStr(Error : PCardinal) : PChar; stdcall; external 'BaseEngine.dll';
function BaseGetLastError : TBaseResult; stdcall; external 'BaseEngine.dll';
function BaseCloseHandle(Handle : TBaseHandle) : Boolean; stdcall; external 'BaseEngine.dll';
function BaseTimeTickCreate : TBaseHandle; stdcall; external 'BaseEngine.dll';
function BaseTimeTickGap(Handle : TBaseHandle) : Cardinal; stdcall; external 'BaseEngine.dll';
function BaseTimeTick(Handle : TBaseHandle) : Cardinal; stdcall; external 'BaseEngine.dll';
function BaseTimeTickPerSec(Handle : TBaseHandle) : Cardinal; stdcall; external 'BaseEngine.dll';
function BaseTimeTickReset(Handle : TBaseHandle) : Boolean; stdcall; external 'BaseEngine.dll';
function BaseCreateFile(FileName : PChar; OpenMethod : TBaseFileOpenMethod; Access : TBaseFileAccess; ShareMode : TBaseFileShare) : IBaseFile; stdcall; external 'BaseEngine.dll';

implementation

end.
C++ Equivalent
Code:
#include <windows.h>

typedef HRESULT TBaseResult;
typedef UINT TBaseHandle;


// Date & Time Records
typedef struct BaseDateTime {
    WORD Year;
    BYTE Month;
    BYTE Day;
    BYTE Hour;
    BYTE Minute;
    BYTE Second;
} TBaseDateTime;

typedef struct BaseDate {
    WORD Year;
    BYTE Month;
    BYTE Day;
} TBaseDate;

typedef struct BaseTime {
    BYTE Hour;
    BYTE Minute;
    BYTE Second;
} TBaseTime;


// FileAttributes and so on
typedef enum {
    bfaArchive,
    bfaDirectory,
    bfaHidden,
    bfaReadOnly,
    bfaSystem,
    bfaTemporary,
    bfaCompressed,
    bfaOffline,
    bfaNotContentIndexed,
    bfaEncrypted
} TBaseFileAttribute;

typedef struct BaseFileAttributes {
    bool Archive;
    bool Hidden;
    bool Readonly;
    bool System;
    bool Temporary;
    bool Offline;
    bool NotContentIndexed;
    bool Directory;
    bool Compressed;
    bool Encrypted;
} TBaseFileAttributes;

// FileEnums, like share options
typedef enum {
    bfomClear,
    bfomOpen,
    bfomOpenExisting  
} TBaseFileOpenMethod;

// Pascals "set of" is not avaible
#define bfaRead UINT(1)
#define bfaWrite UINT(2)

#define bfsRead UINT(1)
#define bfsWrite UINT(2)
#define bfsDelete UINT(4)

/*interface DECLSPEC_UUID("D4C2BA04-9C15-4631-8774-5AE2E05EBB80") IBaseInterface;
typedef interface IBaseInterface;

#undef INTERFACE
#define INTERFACE IBaseInterface
DECLARE_INTERFACE_(IBaseInterface, IUnknown)
{
    __stdcall void* GetHandle;
    STDMETHOD(QueryInterface)(THIS_ REFIID riid, void** ppvObj);
    STDMETHOD_(ULONG,AddRef)(THIS);
    STDMETHOD_(ULONG,Release)(THIS);
};
typedef struct IBaseInterface;*/ // Entfernt, weil fehlerhaft. vTable nicht gefunden


extern "C" {
     __declspec(dllimport) char* BaseVersion(); // Weitere Funktionen aus "kB"-Gründen erstmal noch nicht eingebaut.
}
Lars
There are 10 kinds of people in the world:
those who get binary, and those who don’t.
  Mit Zitat antworten Zitat