Recent

Author Topic: How to export Dll function with underscore with @  (Read 1924 times)

wytwyt02

  • Jr. Member
  • **
  • Posts: 83
How to export Dll function with underscore with @
« on: January 27, 2020, 02:58:51 pm »
In C++, I can declared a function with:

Code: C  [Select][+][-]
  1. __declspec(dllexport) int WINAPI MYBBI(CALCINFO* pData);

the compiled dll function is `_MYBBI@4`.

However I declared a function in Pascal:

Code: Pascal  [Select][+][-]
  1. function SubStr(CString: PChar; FromPos, ToPos: longint): PChar; stdcall;

the compiled function is `SubStr`.

How to compile the pascal dll like the C++

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: How to export Dll function with underscore with @
« Reply #1 on: January 28, 2020, 10:13:00 am »
The compiler can't generate those names automatically for you, because it exports in a C compatible way, not C++. C functions are not mangled like this.

You can force a specific name by using the name clause in the exports section:

Code: Pascal  [Select][+][-]
  1. exports
  2.   SubStr name '_SubStr@12';

However you'll have to calculate that mangled name manually.

If you want to use Pascal functions in a C++ program you should import them as extern "C" instead:

Code: C  [Select][+][-]
  1. extern "C" __declspec(dllimport) int __stdcall SubStr(char* CString, int frompos, int topos);

In both cases you'll need to generate an import library. In theory FPC is able to generate a *.def file that you can then use the Microsoft tools to generate an import library of, but that seems to be buggy currently.

Thus you'll need to manually create an import library:

For the C case:

Code: [Select]
LIBRARY MyLib
EXPORTS
  SubStr

For the C++ case:

Code: [Select]
LIBRARY MyLib
EXPORTS
  _SubStr@12

And then create an import library from that (not tested):

Code: [Select]
lib.exe -machine:IX86 -def:mylib.def -out:mylib.lib
Please note that this is only for Windows (and OS/2 and DOS). For *nix systems that topic is different again.

wytwyt02

  • Jr. Member
  • **
  • Posts: 83
Re: How to export Dll function with underscore with @
« Reply #2 on: January 29, 2020, 03:44:26 am »
The compiler can't generate those names automatically for you, because it exports in a C compatible way, not C++. C functions are not mangled like this.

You can force a specific name by using the name clause in the exports section:

Code: Pascal  [Select][+][-]
  1. exports
  2.   SubStr name '_SubStr@12';

However you'll have to calculate that mangled name manually.

If you want to use Pascal functions in a C++ program you should import them as extern "C" instead:

Code: C  [Select][+][-]
  1. extern "C" __declspec(dllimport) int __stdcall SubStr(char* CString, int frompos, int topos);

In both cases you'll need to generate an import library. In theory FPC is able to generate a *.def file that you can then use the Microsoft tools to generate an import library of, but that seems to be buggy currently.

Thus you'll need to manually create an import library:

For the C case:

Code: [Select]
LIBRARY MyLib
EXPORTS
  SubStr

For the C++ case:

Code: [Select]
LIBRARY MyLib
EXPORTS
  _SubStr@12

And then create an import library from that (not tested):

Code: [Select]
lib.exe -machine:IX86 -def:mylib.def -out:mylib.lib
Please note that this is only for Windows (and OS/2 and DOS). For *nix systems that topic is different again.

Thanks!

 

TinyPortal © 2005-2018