-1

I am looking for the correct method to create a DLL in C++ and call it in Delphi. I use CodeBlocks for the DLL and Delphi RAD Studio 10.2.

My C++ header and source code for building the DLL as described in How to create dll in C++ for using in C# is as follows:

Main.h:

#ifndef MATH_HPP
#define MATH_HPP

extern "C"
{
    __declspec(dllexport) int __stdcall math_add(int a, int b);
}

#endif 

Main.Cpp :

#include "main.h"

int __declspec(dllexport) __stdcall math_add(int a, int b)
{
    return a + b;
}

This code in CodeBlocks builds math_dll.dll without any error.

Calling the DLL in Delphi:

function math_add(X, Y: Integer): Integer; stdcall; external 'math_dll.dll' name 'math_add';

But when I run Delphi and call this function, I have the following error:

"the procedure entry point math_add could not be located in the dynamic link library math_dll.dll"

Which part of my code is wrong?

Hamed
  • 13
  • 3

1 Answers1

1

The default name mangling for the __stdcall calling convention is _<name>@<bytes_in_arguments>. So your DLL function is most likely being exported as '_math_add@8' instead of as 'math_add' like you are expecting. Use a tool like PEDUMP to verify that.

You can use a .DEF file when compiling the DLL to change the exported name, or you can update your Delphi function declaration to use the correct exported name for the name attribute.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Hi Remy , thank you for response. but I was wrong in error report, my error is :"the procedure entry point math_add could not be located in the dynamic link library math_dll.dll" – Hamed Oct 16 '19 at 20:37
  • It is true. Thank you. – Hamed Oct 16 '19 at 21:10