Solved

C DLL in Delphi 2

Posted on 1998-11-14
5
246 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
[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
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

707 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