Solved

C DLL in Delphi 2

Posted on 1998-11-14
5
245 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

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

Suggested Solutions

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

738 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