C DLL in Delphi 2

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)
Who is Participating?
philipleighsConnect With a Mentor Commented:
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.


An example:

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

  Resource :PChar;
  InFile   :PChar;
  x        :boolean;
  x := UnpackFile(Resource, Infile);

I don't know for *sure* if this works, but try it...
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.
Ooops. Forgotten the "external":

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


function UnpackFile(resource, inFile: pchar) : bool; cdecl;
         external 'filename.dll" index CUnpackFileExportIndex;
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).
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.