VOLVO
VOLVO

Reputation: 561

How do I call win32 dll from powerbuilder11.5?

I wrote this code in c++:

extern "C" __declspec(dllexport)    int __stdcall sumx(int a, int b)
{
  int result;

  result = a + b;

  return result;
}

I also tried:

int __stdcall sumx(int a, int b)
{
  int result;

  result = a + b;

  return result;
}

and build win32 dll. then copy it in PB directory.

enter image description here

I define it external function.

enter image description here

And I call it:

enter image description here

when I run it:

enter image description here

Why do error occurs? tnx

Upvotes: 3

Views: 284

Answers (1)

Seki
Seki

Reputation: 11465

After some tests here I think that your problem may result from a name decoration of your exported function. I.E: instead of being named sumx in the dll, it is named _sumx@8 by the compiler.

You can check that by invoking dumpbin /exports keyadll.dll. With my test dll, it shows:

C:\dev\powerbuilder\dlltest>dumpbin.exe /exports keyadll.dll
Microsoft (R) COFF/PE Dumper Version 8.00.50727.762
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file keyadll.dll

File Type: DLL

  Section contains the following exports for keyadll.dll

    00000000 characteristics
    5627876B time date stamp Wed Oct 21 14:39:07 2015
        0.00 version
           1 ordinal base
           1 number of functions
           1 number of names

    ordinal hint RVA      name

          1    0 0000100A _sumx@8 = @ILT+5(_sumx@8)
                             ^====================== HERE is the point!
  Summary

        1000 .data
        1000 .idata
        2000 .rdata
        1000 .reloc
        1000 .rsrc
        2000 .text

BTW, the @8 in the name stands for the 8 bytes (2 x sizeof(int)) of parameters that are given to the function.

You have 2 options to fix that:

  • use the exact _sumx@8 name in the declaration of the external function in PB (you can use an alias for not changing your PB code):

    function int sumx (int a, int b) library "keyadll.dll" alias for '_sumx@8'
    

    I don't consider that solution being very elegant, though.

  • you can force VC to name the exported as YOU want (and not the reverse!) by using a module definition file (a .def).

    1. in VS, choose to add a new item to the project / module definition file
    2. simply put the names of the functions to export. It will contain

      LIBRARY "keyadll.dll"
      EXPORTS
          sumx
      

    Rebuild your dll and it should be OK for PB.

Upvotes: 5

Related Questions