?
Solved

C DLL in Delphi 2

Posted on 1998-11-14
5
Medium Priority
?
248 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 100 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

Industry Leaders: 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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

777 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