Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: C++ Classes in Delphi/FreePascal

  1. #1

    C++ Classes in Delphi/FreePascal

    Hello all!

    I want to be able to use C++ and Delphi/FreePascal (in Delphi Mode) for a project of mine. I'll use dll files to handle the communication between the languages.

    Anyway after I compiled a dll with FreePascal the application crashed. It worked fine when compiling with Delphi, but that's not good enough for me since I want it to work with both compilers.

    Here's a small example which shows what doesn't work:

    C++
    Code:
    // in *.h
    typedef class IMyClass
    {
        public:
            __cdecl virtual void DoSomething()=0;
    } *lpMyClass;
    
    typedef class CMySub: public IMyClass
    {
        public:
            __cdecl void DoSomething();
    };
    
    // in *.cpp
    __cdecl void CMySub::DoSomething()
    {
        printf("hello world\n");
    };
    Pascal:
    [pascal]
    // MyClass.pas
    type
    IMyClass = class(TObject)
    public
    procedure DoSomething; virtual; cdecl; abstract;
    end;

    // pasdll.dpr
    procedure InitDll(MyClass: IMyClass); cdecl;
    begin
    MyClass.DoSomething;
    end;

    exports
    InitDll name 'InitDll';
    [/pascal]

    And some extra code for initializing everything.

    Any ideas on how to get things working with FreePascal?
    Signature:
    <br />This is a block of text that can be added to posts you make. There is a 255 character limit

  2. #2
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    C++ Classes in Delphi/FreePascal

    First - I know little about interfaces
    Second - I've never used classes and DLLs together

    So:

    1. Why is _I_MyClass not inheriting from some sort of Interface (IUnknown)>
    2. Where Is the MyClass.Create statement?
    3. Ummm


    But dont believe a word I say here because Its out of my league
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  3. #3

    Re: C++ Classes in Delphi/FreePascal

    Quote Originally Posted by Ultra
    I'll use dll files to handle the communication between the languages.
    That's ok, but DLLs does not handle nicely classes. You can have c++ classes and read them with c++ programs, but not mixing classes from different languages, becouse they're implemented in different ways.

    To do what you want usually one write the program in c++ but then writes a layer of code exporting only plain procedures.
    If you save your data in a proprietary format, the owner of the format owns your data.
    <br /><A href="http://msx80.blogspot.com">http://msx80.blogspot.com</A>

  4. #4

    C++ Classes in Delphi/FreePascal

    Quote Originally Posted by cairnswm
    1. Why is _I_MyClass not inheriting from some sort of Interface (IUnknown)>
    Because I'm not entirely sure on how interfaces work in Delphi. I could probably use interfaces and just add stuff like AddRef() and Release() to the C++ class but since I'm not sure how it would work I don't want to use it.

    Quote Originally Posted by cairnswm
    2. Where Is the MyClass.Create statement?
    In the C++ code. I can just use CMySub *MyClass = new CMySub(). And then send the returned pointer to Delphi.

    Quote Originally Posted by {MSX}
    That's ok, but DLLs does not handle nicely classes. You can have c++ classes and read them with c++ programs, but not mixing classes from different languages, becouse they're implemented in different ways.

    To do what you want usually one write the program in c++ but then writes a layer of code exporting only plain procedures.
    I guess this is more or less what's the problem. My guess is that the Virtual Method Table indexes are different in Delphi and FreePascal. (I can't really explain what the VMT is since I'm not entirely sure myself, but think of it as an array of method pointers where all the TObject methods has a negative index. That way a C++ class, with it methods starting at 0, can be called without problems. If these have a positive index in FreePascal MyClass.DoSomething() will point to something irrelevant live MyClass.Create()).
    As for not using something like MyClass_DoSomething(). Well, that could probably work (and if I can't come up with some other solution I'll probably use it). However it's not as clean, especially when I have some code like IMyClass.Add(): IMyOtherClass and IMyClass.Find(AName: PChar), then it can be a hell to use...

    As for using C++ classes in Delphi I used this article.

    [edit] Seems like I don't really know how to use URLs... :?
    Signature:
    <br />This is a block of text that can be added to posts you make. There is a 255 character limit

  5. #5

    C++ Classes in Delphi/FreePascal

    @Ultra: Just leave out the quotation marks Then it works fine.

    But in general you should ALWAYS avoid using classes for transaction between modules like Program<->DLL.
    The more complex a system is, the smaller the bugs get; the smaller the bugs are, the more often they appear.

  6. #6
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    C++ Classes in Delphi/FreePascal

    Many years ago I did some work on a PC game that actually made it to the shelves. I worked on a Rugby Match simulator. I did the DLL in Delphi and the rest of the project team worked in C++.

    I went back and had a look at my code. I used Classes internal to my DLL but only exported procedures and functions.

    My Type declarations were somethign like:
    [pascal]
    Type
    TPlayer = Class
    Name : Stirng;
    Skill : Integer;
    End;
    [/pascal]

    My functions that got exported were
    [pascal]
    Procedure FindPlayer(Name : String);
    Function PlayerSkill(Name : Stirng):Integer;
    Procedure FirstPlayer;
    Procedure NextPlayer;
    [/pascal]

    Then the guys could call PlayerSkill('Ultra') and get his skill.

    So my DLL managed all the Class stuff and memory issues that went with that and the exports were static to find a single item of information (sort of like TSearchRec does it).
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  7. #7

    C++ Classes in Delphi/FreePascal

    I'm probably gonna just use regular functions when I call the dll's. It's a bit disappointing but should work with both Delphi and FreePascal.
    Signature:
    <br />This is a block of text that can be added to posts you make. There is a 255 character limit

  8. #8

    C++ Classes in Delphi/FreePascal

    Just some irregular thoughts:
    1) Have you enabled Delphi compatibility in FPC?
    2) Try decralring class as: "IMyClass = class" (without TObject)
    There are only 10 types of people in this world; those who understand binary and those who don't.

  9. #9

    C++ Classes in Delphi/FreePascal

    Quote Originally Posted by Clootie
    Just some irregular thoughts:
    1) Have you enabled Delphi compatibility in FPC?
    2) Try decralring class as: "IMyClass = class" (without TObject)
    1) Yes, I've written {$IFDEF FPC}{$MODE DELPHI}{$ENDIF} at the top of the file. If I remember correctly "class" isn't a reserved word in FreePascal so it wouldn't even compile.

    2) Does it matter? TObject should always be inherited whether or not you explicitly say it. I just write so for clarity.
    [Edit] I did try it though. Didn't make any change.
    Signature:
    <br />This is a block of text that can be added to posts you make. There is a 255 character limit

  10. #10

    Not possible

    Using classes in different languages is impossible and will never be as far as I know via SO's or DLL's.

    You could use things like COM or COBRA but those are winshit only.

    The problem lies in the VMT and in the memory layout of classes.
    This is not FPC specific it's common for all OOP language(hence COM)

    If it's not cruical I'd suggest using classes internaly and exporting only procedures/functions.
    Feel the power of Open Source.
    <br />Feel the power of Free Pascal.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •