Recent

Author Topic: [SOLVED] External DLL function name  (Read 8929 times)

linnemann

  • New Member
  • *
  • Posts: 34
[SOLVED] External DLL function name
« on: February 20, 2017, 07:34:14 pm »
Hi

I am interfacing with an external C dll that works fine with the index number, but not the name

Code: Pascal  [Select][+][-]
  1. function AdsGetDllVersion():Longint; stdcall; external 'TcAdsDll.dll' index 10; //name '_AdsGetDllVersion@0';  

So if I change the above to the name it give me an error that the function is not available.

I cannot change the name of the external C functions as it comes from a PLC vendor.
The name part is supposed to work with Delphi.

Just wanted to hear if anybody know if it is possible to do it by name and not index?

I have used http://www.nirsoft.net/utils/dll_export_viewer.html to inspect the dll and the name and index is copied directly from that.

« Last Edit: February 21, 2017, 12:23:43 pm by linnemann »

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: External DLL function name
« Reply #1 on: February 20, 2017, 07:53:50 pm »
Just wanted to hear if anybody know if it is possible to do it by name and not index?

Yes, that's always worked. Here's an example of what I use to call the C interface in the GDAL library (where gdallib is the name of the library file, which depends on platform so defined elsewhere):

function GDALGetDriverCount: cint; {$IFDEF MSWINDOWS}stdcall{$ELSE}cdecl{$ENDIF}; external gdallib {$IFDEF WIN32}name '_GDALGetDriverCount@0'{$ENDIF};

Offhand I don't see anything wrong with what you're doing, although as written it's for 32-bit Windows only.

Here are my notes on dynamic libraries:

https://macpgmr.github.io/MacXPlatform/PascalDynLibs.html

linnemann

  • New Member
  • *
  • Posts: 34
Re: External DLL function name
« Reply #2 on: February 20, 2017, 07:58:41 pm »
Hi thx for the feedback

Although what I am saying is that it works fine by using by index, but not fine by name.
Can't see why it should not work by name.

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: External DLL function name
« Reply #3 on: February 20, 2017, 08:07:39 pm »
I've always used Dependency Walker:

http://dependencywalker.com/

depends TcAdsDll.dll

You should see the decorated function name in the Function list:

_AdsGetDllVersion@0

If you don't see exactly that, then that's the problem.

linnemann

  • New Member
  • *
  • Posts: 34
Re: External DLL function name
« Reply #4 on: February 20, 2017, 08:16:29 pm »
Code: Pascal  [Select][+][-]
  1. function AdsGetDllVersion():Longint; stdcall; external 'TcAdsDll.dll' name '_AdsGetDllVersion@0'; //index 10; //  


howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: External DLL function name
« Reply #5 on: February 20, 2017, 09:10:09 pm »
Could the fact that _AdsGetDllVersion@0 is an invalid Pascal identifier be a stumbling block at the linking stage?

linnemann

  • New Member
  • *
  • Posts: 34
Re: External DLL function name
« Reply #6 on: February 21, 2017, 09:24:25 am »
@howardpc, that is also my theory, was just asking to see if anyone have had a similar issue.

The reason I ask is that the index number in the vendor DLL could change whereas the name would probably remain the same (not guaranteed, but likely since it is in the PLC world).

No problem in using index, just a matter of maintaining the code.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: External DLL function name
« Reply #7 on: February 21, 2017, 09:30:48 am »
Only when really necessary you would need a mangled name or the underscore.
Does this work?
Code: Pascal  [Select][+][-]
  1. function AdsGetDllVersion():Longint; stdcall; external 'TcAdsDll.dll' name 'AdsGetDllVersion';   // note the name can be CASE SENSITIVE
  2.  
Note the name can be CASE SENSITIVE so  check that!!! Most of the time it resolves anyway, but check it. And don't use the underscore. The linker will resolve that.
« Last Edit: February 21, 2017, 09:32:52 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

linnemann

  • New Member
  • *
  • Posts: 34
Re: External DLL function name
« Reply #8 on: February 21, 2017, 12:23:31 pm »
@Thaddy, works perfectly, would newer have tried that. Thank you very much.

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: External DLL function name
« Reply #9 on: February 21, 2017, 07:18:23 pm »
@Thaddy, works perfectly, would newer have tried that. Thank you very much.

Hmm, very puzzling. Now you have code that's incompatible with Delphi. That doesn't sound right at all.

I assume you got your function declaration from Beckhoff's TcAdsApi.pas. That should work the same with both Delphi and FPC.

http://infosys.beckhoff.com/english.php?content=../content/1033/tcadscomlib/html/tcadscomlib_intro.htm

Have you tested with Delphi to see if it still works there? And what version of FPC are you using?

In my own tests, specifying the decorated function name is required if that's how the library entry point is named. For example, in this simple program that tests the GDAL library, it works fine with both Delphi and FPC 3.0.0. However, if I compile with either of the commented out declarations, I get the error message you posted above when the app is run.

program TestDecName;

type
  cint = LongInt;

function GDALGetDriverCount: cint; stdcall; external 'gdal111.dll' name '_GDALGetDriverCount@0';
//function GDALGetDriverCount: cint; stdcall; external 'gdal111.dll' name 'GDALGetDriverCount';
//function GDALGetDriverCount: cint; stdcall; external 'gdal111.dll' name 'GDALGetDriverCount@0';

begin
  WriteLn(GdalGetDriverCount);
end.

Another thing that's puzzling me is why your error message indicates that the project1.exe is the DLL.

Something going on here, I believe.
« Last Edit: February 21, 2017, 07:21:07 pm by Phil »

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: External DLL function name
« Reply #10 on: February 21, 2017, 07:28:02 pm »
@Thaddy, works perfectly, would newer have tried that. Thank you very much.
Hmm, very puzzling. Now you have code that's incompatible with Delphi. That doesn't sound right at all.
Why... It is perfectly Delphi compatible too.. I am afraid...
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

linnemann

  • New Member
  • *
  • Posts: 34
Re: [SOLVED] External DLL function name
« Reply #11 on: February 21, 2017, 10:31:44 pm »
Using the .pas from beckhoff.

Do not have delphi to test with.

FPC 3.0.1 lazarus 1.6

 

TinyPortal © 2005-2018