Recent

Author Topic: GCC C++ Classes  (Read 14067 times)

rtusrghsdfhsfdhsdfhsfdhs

  • Full Member
  • ***
  • Posts: 162
GCC C++ Classes
« on: December 27, 2014, 09:23:11 am »
How can I use these GCC classes in FPC? With something like CORBA interfaces?

A small example for both sides would be welcome.

Thanks
« Last Edit: December 27, 2014, 09:27:57 am by Fiji »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: GCC C++ Classes
« Reply #1 on: December 27, 2014, 12:14:54 pm »
There' s a little preliminary support with restrictions.

rtusrghsdfhsfdhsdfhsfdhs

  • Full Member
  • ***
  • Posts: 162
Re: GCC C++ Classes
« Reply #2 on: December 27, 2014, 03:16:06 pm »
This works good in Delphi XE7 / Visual C++ but crashes in FPC Trunk?  :o



I get some SIG error.

Code: [Select]
program Test;

//{$APPTYPE CONSOLE}
{$mode delphi}

const
  DLL = 'C:\Users\Administrator\Desktop\Interop\DLL-CPP\Interface\Debug\Interface.DLL';

type
  TPerson = class
    function Age(): Integer; overload;  virtual; cdecl; abstract;
    procedure Age(const Value: Integer); overload; virtual; cdecl; abstract;
    procedure Free; overload; virtual; cdecl; abstract;
  end;

function NewPerson(): TPerson; cdecl; external DLL;

var
  Person: TPerson;
begin
  Person := nil;
  Person := NewPerson;

  if Person = nil then
  Writeln('not initalized');

  Person.Age(22);
  Writeln(Person.Age);
  Person.Free;
  Person := nil;
  Readln;
end.




#include "stdafx.h"
#include <iostream>

using namespace std;

class AbstractPerson
{
virtual void _cdecl Age(const int Value) = 0;
virtual int _cdecl Age() = 0;
virtual void _cdecl Free() = 0;
};

class Person : public AbstractPerson
{
private:
int FAge;
public:
void  _cdecl Age(const int Value) { FAge = Value; };
int  _cdecl Age() { return FAge; };
void _cdecl Free() { if (this) delete this; };

Person() { FAge = 0; };
    ~Person() {};
};
 
extern "C" __declspec(dllexport)  AbstractPerson* __cdecl NewPerson()
{
return new Person();
}
 
« Last Edit: December 27, 2014, 04:05:45 pm by Fiji »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: GCC C++ Classes
« Reply #3 on: December 27, 2014, 04:09:48 pm »
This works good in Delphi XE7 / Visual C++ but crashes in FPC Trunk?  :o
  • That's not how FPC supports it, please read the thread link above
  • Delphi perhaps decided to follow VC++ VMT layout (this is implementation specific that's not even compatible among C++ compilers), therefore it works in Delphi. Simply changing C++ compiler to compile the DLL is enough to crash it

rtusrghsdfhsfdhsdfhsfdhs

  • Full Member
  • ***
  • Posts: 162
Re: GCC C++ Classes
« Reply #4 on: December 29, 2014, 03:24:11 am »
So FPC cannot use ActiveX? Because interfaces are really abstract classes with some extra functionality..

Besides I made it work with GCC :) On delphi ofcourse.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: GCC C++ Classes
« Reply #5 on: December 29, 2014, 05:56:44 am »
So FPC cannot use ActiveX? Because interfaces are really abstract classes with some extra functionality..
ActiveX is supported, you can use LazActiveX to import ActiveX libraries. However, as you will read in the wiki, it's Windows only. FPC can deal with CORBA interface, if you want something more universal.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: GCC C++ Classes
« Reply #6 on: December 29, 2014, 06:00:50 am »
So FPC cannot use ActiveX? Because interfaces are really abstract classes with some extra functionality..
Interfaces are not abstract classes. They're VMTs, where VMT is following certain convention.
ActiveX is implemented based of COM interfaces (where COM is the convention). There're also CORBA interfaces (and they're incompatible with COM interfaces, since the convention is different).

ActiveX interfaces are not C++ classes.. and FPC does support COM and ActiveX

rtusrghsdfhsfdhsdfhsfdhs

  • Full Member
  • ***
  • Posts: 162
Re: GCC C++ Classes
« Reply #7 on: December 29, 2014, 11:40:09 am »
Please make an example how to use CORBA interfaces then.. i never used them.

Look what someone posted on http://www.hu.freepascal.org/lists/fpc-devel/2005-January/004403.html

Well, interfaces are really C++ abstract classes. COM model adds some bell and
whistles above that (including IUnknown that is integrated in object pascal
language interfaces).  ::)

This is totally incorrect statement. Inprocess COM is heavy relayed on C++
abstract classes. I'm not talking here about ActiveX (it's really just a
framework above COM), marshalling, etc.


« Last Edit: December 29, 2014, 12:21:51 pm by Fiji »

rtusrghsdfhsfdhsdfhsfdhs

  • Full Member
  • ***
  • Posts: 162
Re: GCC C++ Classes
« Reply #8 on: December 29, 2014, 02:49:33 pm »
I tested COM with FPC its not even compatible with Delphi..  :o
So much for compatibility ::)


Code: [Select]
{DELPHI}

library Project1;

uses
  System.SysUtils;

type
  IPerson = interface ['{6675C5C0-D95C-11D4-BDE0-00A024BAF736}']
    function Age(): Integer; overload; stdcall;
    procedure Age(const Value: Integer); overload; stdcall;
  end;

  TPerson = class(TInterfacedObject, IPerson)
  private
    FAge: Integer;
  public
    procedure Age(const Value: Integer); overload; stdcall;
    function Age(): Integer; overload; stdcall;
  end;

procedure TPerson.Age(const Value: Integer);
begin
  FAge := Value;
end;

function TPerson.Age(): Integer;
begin
  Result := FAge;
end;

function CreatePerson(): IPerson; stdcall; export;
begin
  Result := TPerson.Create;
end;

exports
  CreatePerson;

begin

end.

{FREEPASCAL}

program Project1;

{$mode delphi}

uses
  Classes;

type
  IPerson = interface ['{6675C5C0-D95C-11D4-BDE0-00A024BAF736}']
    function Age(): Integer; overload; stdcall;
    procedure Age(const Value: Integer); overload; stdcall;
  end;

const
  DLL = 'C:\Users\Administrator\Desktop\DLL\Win32\Release\Project2.dll';

function CreatePerson(): IPerson; stdcall; external DLL;

var
  Person: IPerson;
  I: Integer;
begin
  Person := CreatePerson();
  Person.Age(10); // EXTERNAL SIGV CRASH
  I := Person.Age;
  Readln;
end.
           
« Last Edit: December 29, 2014, 02:56:13 pm by Fiji »

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: GCC C++ Classes
« Reply #9 on: December 29, 2014, 03:09:41 pm »
Works just fine with D7 and fpc 2.6.4

rtusrghsdfhsfdhsdfhsfdhs

  • Full Member
  • ***
  • Posts: 162
Re: GCC C++ Classes
« Reply #10 on: December 29, 2014, 03:10:38 pm »
It doesn't work with XE7 & FPC Trunk..

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: GCC C++ Classes
« Reply #11 on: December 29, 2014, 03:11:56 pm »
It doesn't work with XE7 & FPC Trunk..
Assembler is your friend then. 
Expected problem - interface is being released on returning from CreatePerson function. Should you use out parameter?
Code: [Select]
procedure CreatePerson(out person: IPerson)

rtusrghsdfhsfdhsdfhsfdhs

  • Full Member
  • ***
  • Posts: 162
Re: GCC C++ Classes
« Reply #12 on: December 29, 2014, 03:15:32 pm »
I tried out and var. Still crashes. Seems this is either a bug in trunk or something else.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: GCC C++ Classes
« Reply #13 on: December 29, 2014, 03:18:32 pm »
I tried out and var. Still crashes. Seems this is either a bug in trunk or something else.
XE7 maybe? ... try an earlier version of FPC?
Btw, what CPU i386 or x86_64?

Try this:
// library side
Code: [Select]
function CreatePerson(): IPerson; stdcall; export;
begin
  Result := TPerson.Create;
  writeln( NativeInt ( Result ));
end;
// program size
Code: [Select]
var
  Person: IPerson;
  I: Integer;
begin
  Person := CreatePerson();
  writeln( PtrUInt( Person ) );
  Person.Age(12); // EXTERNAL SIGV CRASH
  I := Person.Age;
  writeln(I);
  Readln;
end.
numbers should match in the first place.

rtusrghsdfhsfdhsdfhsfdhs

  • Full Member
  • ***
  • Posts: 162
Re: GCC C++ Classes
« Reply #14 on: December 29, 2014, 03:20:03 pm »
Windows 8.1 x64 + Intel Q6600  + FPC Trunk x86. But will try 2.6.4 :) I have doubts.

On Delphi I get number in console but in FPC none  :-\
« Last Edit: December 29, 2014, 03:26:57 pm by Fiji »

 

TinyPortal © 2005-2018