Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

Classes in Dll

I have a class build in c++ in a Dll.
Can anyone tell me how can i use it in delphi 2.0?
Thank you.

0
Nuno Alves
Asked:
Nuno Alves
  • 2
1 Solution
 
sperlingCommented:
Create a similar virtual and abstract class in delphi 2. Get the address of a instance of the C++ class, and cast this address to the delphi class.


e.g.

type

  TClassCorrespondingToDllClass = class
    function FirstMethodInCPPClass : INTEGER; stdcall; virtual; abstract;
    procedure SecondMethodInCPPClass; stdcall; virtual; abstract;
  end;


Then

var
  TheObject : TClassCorrespondingToDllClass;

begin
  TheObject := TClassCorrespondingToDllClass ( DLLFunctionCreatingTheClassAndReturningPointer)

  TheObject.SecondMethod;
end;

If your delphi class maps *exactly* to the c++ class (calling convention and parameters), this should work. You need to have the DLL create an instance of the class though.

Regards,

Erik.
0
 
Nuno AlvesAuthor Commented:
How can I load the dll in delphi?
I now that with the dll in C I just have to put before
the declaration of the function external 'Dll name' but
with the class i couldn't do the same.
0
 
sperlingCommented:
You have to declare an exported function in the C DLL. This function should create an instance of the class and return it as a pointer. I can't be of much help when it comes to C syntax...

Import it into delphi like this:

function CreateAnObject : POINTER; stdcall; external 'DLLNAME.DLL' name 'ExportedNameOfCreateAnObject';


Regards,

Erik.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now