Solved

C++ Dll

Posted on 1997-05-20
3
161 Views
Last Modified: 2010-04-04
I have some classes written in C++ in a DLL.
How can I use this classes in Delphi?
0
Comment
Question by:facr
  • 2
3 Comments
 
LVL 2

Expert Comment

by:gysbert1
Comment Utility
Create a similar pascal function prototype and follow it by external 'MyDllName.dll';

EG:

C++:
int
MyDllFunction(int FirstParam, word SecondParam) {
  // Blaah Blaah Blaah
}

Delphi:
function MyDllFunction(FirstParam :integer; SecondParam :word) :integer; external 'MyDllName.dll';

Then just go ahead and call it in your code ...
0
 
LVL 2

Expert Comment

by:gysbert1
Comment Utility
Ok, that was calling functions of course.

Structures have to be duplicated in much the same way, only the functions can be called directly from the .dll.

You can thus not create an instance of a class that you only declared in the .dll.

The same limitations apply in Delphi as in C++ with respect to accessing stuff in dll's
0
 
LVL 3

Accepted Solution

by:
sperling earned 50 total points
Comment Utility
Create a virtual, abstract class in Delphi, where all methods map exactly (and in the same order) as those of the C++ class.

Next, export a function from the C++ DLL which creates an instance of the class, and returns it as a pointer. In delphi, cast this pointer to the abstract class you've defined, and use.


E.g.

C class:

class SomeClass ....
{
  void SomeMethod (byte * Data);
}

Delphi class

TSomeClass = class
protected
  procedure SomeMethod (Data : PBYTE); virtual; abstract;
end;

funtion DLLCreateSomeClass : POINTER; external 'DLLNAME.DLL';

procedure SomeProcedure;
var
  SomeObject : TSomeClass;
begin
  SomeObject := TSomeObject(DLLCreateSomeClass);
end;


I'm not certain of the C++ syntax, but this'll hopefully give you an idea of how to do. You'll ofcourse have to ensure that calling conventions are correct.

Regards,

Erik.

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now