Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Delphi function in VC Project

I created a function in delphi as below: function CheckResult():integer; stdcall; export;

The compiler is Delphi 64 and the output is CheckFunctions.dll

Then in the VC++ project (VS 2012), I write: extern "C" __declspec(dllimport) int __stdcall CheckResult();

and use it in a function, then I get the compiler error in C++ project:

error LINK2019: unresolved external symbol __imp_CheckResult referenced in function *

How can I do? Thanks

like image 956
frank Avatar asked Dec 06 '25 14:12

frank


2 Answers

You are missing an import library (.lib file) for the DLL. That's why the linker is giving that error message. Unfortunately Delphi won't generate a .lib file which is a bit of a weakness in my view.

You can solve the problem by either:

  1. Linking with LoadLibrary/GetProcAddress.
  2. Generate a suitable .lib file.

Option 2 is easy enough. Create a fake DLL project in Visual Studio. Arrange for it to export the same functions as your Delphi DLL. Implement these functions with empty stubs. Use a .def file rather than __declspec(dllexport) to avoid name decoration of your exports.

It's really obvious really. Make a fake DLL that has an identical interface to the real DLL. The same name, the same functions. The fake DLL needs no implementation because all you are doing is getting the MS tools to make the .lib file that Delphi cannot.

More details here: http://support.microsoft.com/kb/131313


FWIW I believe that the Delphi export modifier is ignored. Use an exports clause instead.

like image 84
David Heffernan Avatar answered Dec 09 '25 19:12

David Heffernan


Use VC's command-line LIB utility to create a VC-compatible import .lib file for the DLL, then add that file to your VC project.

like image 31
Remy Lebeau Avatar answered Dec 09 '25 19:12

Remy Lebeau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!