Solved

C DLL in Delphi 2

Posted on 1998-11-14
5
236 Views
Last Modified: 2010-04-04
I wrote a DLL in MS Visual C++, and I want to call it from a Delphi 2 app. How do I do this? Here is my declaration of the function in C++:

BOOL UnpackFile(const char* Resource, const char* InFile)
0
Comment
Question by:mmarvi
  • 2
  • 2
5 Comments
 
LVL 5

Expert Comment

by:scrapdog
ID: 1346799
An example:

function UnpackFile; external 'filename.dll'; cdecl;

var
  Resource :PChar;
  InFile   :PChar;
  x        :boolean;
begin
  x := UnpackFile(Resource, Infile);
end.

I don't know for *sure* if this works, but try it...
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1346800
I never did this, but don't you have to include the parameters in the UnpackFile pascal definition? Otherwise the compiler won't compile this, or am I wrong?

function UnpackFile(resource, inFile: pchar) : bool; cdecl;

Then I'm not sure about the "cdecl". Is it this way? Why have the winAPI "stdcall"? Windows is written in C(++), too. But I don't know it...

Regards, Madshi.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1346801
Ooops. Forgotten the "external":

function UnpackFile(resource, inFile: pchar) : bool; cdecl;
         external 'filename.dll" name "UnpackFile";

or

function UnpackFile(resource, inFile: pchar) : bool; cdecl;
         external 'filename.dll" index CUnpackFileExportIndex;
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1346802
stdcall is used for API functions, cdecl is used for DLLs.

I believe the only difference is that cdecl doesn't remove parameters from the stack.  And as far as a parameter list, I think you're right.

I posted the above comment before I realized that this is a duplicate question...I think the way shown in the other one is correct (it is in the locked section).
0
 
LVL 3

Accepted Solution

by:
philipleighs earned 50 total points
ID: 1346803
In C, declare the function like this:

#define DllExport    __declspec( dllexport )

DllExport BOOL UnpackFile(LPCSTR Resource, LPCSTR InFile);

In Delphi, declare it like this:
function UnpackFile(Resource: PChar; InFile: PChar): Boolean; stdcall; external 'DllName.dll';

When you call the function from Delphi, you'll have to cast the string arguments to PChar if they are huge strings.

Cheers,
Phil.

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

21 Experts available now in Live!

Get 1:1 Help Now