Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Calling functions in external DLL's

Posted on 1999-07-01
9
Medium Priority
?
198 Views
Last Modified: 2010-04-06
I have a Delphi class that has to call some functions
in an external DLL !! . . .
It works fine when it's not in a class !!

But it can't compile when it's in a class

const
  DLLNAME = 'DEng32.dll';

type
TDesEng = class(TComponent)
public
 function APICreateSession() : longint; cdecl; external DLLNAME;

I get this errormessage
'Field definitions not allowed after mothods or properties'
0
Comment
Question by:Jeb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
9 Comments
 
LVL 15

Expert Comment

by:simonet
ID: 1385643
Take a look at CommCtrl.pas. In that files there are tons of examples on how to import class procedures from DLLs.

The basic approach is to declare the class as if it where a regular class and, only in the implementation section you specify the proc is in a DLL.

there are examples in controls.pas too.

Yours,

Alex

end;

0
 
LVL 10

Accepted Solution

by:
Lischke earned 330 total points
ID: 1385644
This compiler message is a bit irritating. It means that you cannot use an external function in a class definition. Rewrite the class so that you have a method which then calls the external function:

interface
  function APICreateSession() : longint; cdecl; external DLLNAME;

type
  TDesEng = class(TComponent)
  public
   function CreateSession: longint;

implementation

function TDesEng.CreateSession: Longint;

begin
  Result := APICreateSession;
end;

Ciao, Mike

0
 

Author Comment

by:Jeb
ID: 1385645
I don't wan't to call a delphi function that then calls a
Dll !!  

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 15

Expert Comment

by:simonet
ID: 1385646
>I don't wan't to call a delphi function that then calls a
>Dll !!  

But that's the way to do it. Lischke is right.

When you make call a class procedure/function, there is one hidden parameter. You cannot pass this parameter to the DLL, simply because you cannot define it.

Alex
0
 

Author Comment

by:Jeb
ID: 1385647
I don't wan't to call a delphi function that then calls a
Dll !!  

0
 

Author Comment

by:Jeb
ID: 1385648
ok . . . if it not that much overhead then it's ok !!

I just had it alle in a Unit with the external declaratin, but if it doesn't work for classes then I'll do it the
hard way !!
0
 
LVL 10

Expert Comment

by:Lischke
ID: 1385649
Jeb, there isn't really much overhead. Actually by calling the class' method it will read pointer and call this then according to the definitions. This happens all the time in much of the Window controls wrapper classe. Believe me, the overhead is marginal compared to all the other things happening in an application.

Ciao, Mike
0
 

Author Comment

by:Jeb
ID: 1385650
ok . . . if it not that much overhead then it's ok !!

I just had it alle in a Unit with the external declaratin, but if it doesn't work for classes then I'll do it the
hard way !!
0
 

Author Comment

by:Jeb
ID: 1385651
ok  better late than never !!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

705 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