1. Dan Cooperstock
  2. PowerBuilder
  3. Monday, 30 November 2020 19:27 PM UTC

I'm trying to call an external function in a DLL that has been provided to me. It was written in Delphi, but at least apparently declared to use C-style strings and the stdcall calling convention, so it should work.

But when I call it (whether or not I add ";Ansi" to the Alias section of the declaration) I get "Error calling external function %s at line ...".

That "%s" part of the message seems a bit unusual.

I did confirm that I get a different error message if the DLL isn't present. I also get a different error message (that fills in the function name instead of "%s") if I use an incorrect method name in the alias clause.

In case it's helpful, the declaration is like:

Function String Foo(String arg1, string arg2, String arg3) LIBRARY "libname.dll" alias for "FOO"

Yes, the name is capitalized in the DLL, when I look at it with "dumpbin /exports", though I doubt that matters. In case it's helpful, here's the relevant part of that output:

ordinal hint RVA name

1 0 0015C2E8 FOO

(I have changed the names for privacy of the 3rd party.)

Adding REF before the argument definitions also doesn't work, then I get an error about incorrect reference argument.

Any bright ideas? Thanks.

Accepted Answer
Dan Cooperstock Accepted Answer Pending Moderation
  1. Monday, 30 November 2020 22:22 PM UTC
  2. PowerBuilder
  3. # Permalink
0
Votes
Undo

So does anyone reading this also know Delphi? I'm told that the declaration of the function I'm trying to call, in Dephi, is like:

function FOO(arg1, arg2, arg3:PCHAR): PCHAR; export; stdcall;

As I understand it, PCHAR is C strings (ANSI). So it sounds like it should work. But I'm still getting the same error message.

Comment
I really thought that might solve it, after reading it Chris, but unfortunately it didn't.
  1. Dan Cooperstock
  2. Monday, 30 November 2020 22:54 PM UTC
I take it back, that really did solve it. I just had no idea the result string from this function was so long. When I assigned 200 spaces to ls_return before calling the function, it failed. But when I assigned 1000 spaces to it, it worked!



Thanks so much.
  1. Dan Cooperstock
  2. Monday, 30 November 2020 23:11 PM UTC
That's awesome Dan! Sorry that it took me so long to access those old Delphi synapse joints. ;-)
  1. Chris Pollach @Appeon
  2. Monday, 30 November 2020 23:32 PM UTC
There are no comments made yet.
John Fauss Accepted Answer Pending Moderation
  1. Monday, 30 November 2020 20:56 PM UTC
  2. PowerBuilder
  3. # 1
0
Votes
Undo

Hi, Dan -

The issue is likely to be either:

1) The string being returned is not null-delimited, as Chris said, and/or

2) The string being returned is not Unicode16LE encoded, if it is not ANSI-encoded.

FYI - I have had run-time errors occur when the name of the entry point/function in the DLL does not EXACTLY match the name supplied in the PB External Function Declaration.

Regards, John

Comment
There are no comments made yet.
Dan Cooperstock Accepted Answer Pending Moderation
  1. Monday, 30 November 2020 20:03 PM UTC
  2. PowerBuilder
  3. # 2
0
Votes
Undo

Chris, I have created a lot of C-language DLLs for calling from PB. I understand how to make that work.

The problem here is that this DLL is being created in Delphi, by a 3rd party, for our use. I did explain to them what was required and I'm sure they at least tried to follow those rules (C-style strings, stdcall calling convention).

Comment
FWIW: PB only supports NULL Terminated Strings - so make sure that the Pascal DLL declares the passing String argument as the data type PCHAR. HTH
  1. Chris Pollach @Appeon
  2. Monday, 30 November 2020 20:21 PM UTC
There are no comments made yet.
Dan Cooperstock Accepted Answer Pending Moderation
  1. Monday, 30 November 2020 19:46 PM UTC
  2. PowerBuilder
  3. # 3
0
Votes
Undo

They sent me some Pascal code demonstrating their calling of the method and it does declare it with the word "stdcall", so my first reaction would be that that is not the problem. But I'll ask.

Comment
Another thing I should point out is that if I change the declaration of the arguments to a different data type (like changing "String arg1" to "double arg1") I get "Bad runtime function reference" instead of "Error calling external function %s". So obviously what I have in my declaration is close to working, and being somewhat correctly called.
  1. Dan Cooperstock
  2. Monday, 30 November 2020 19:52 PM UTC
  1. Chris Pollach @Appeon
  2. Monday, 30 November 2020 19:58 PM UTC
There are no comments made yet.
Chris Pollach @Appeon Accepted Answer Pending Moderation
  1. Monday, 30 November 2020 19:40 PM UTC
  2. PowerBuilder
  3. # 4
0
Votes
Undo

Hi Dan;

   My guess is that the Delphi DLL is not following standard C++ API declarations.

   FYI:  https://community.appeon.com/index.php/articles-blogs/tutorials-articles/2-powerbuilder/302-interfacing-pb-applications-with-the-windows-api-part-1

Regards ... Chris

Comment
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.