3

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

Joe McGrath
  • 1,481
  • 10
  • 26
VOLVO
  • 541
  • 5
  • 16
  • How do you create your dll (Visual Studio, MinGW, ...)? The name of the exported function in the dll may not be what you specify in the external decoration due to *name decoration*. – Seki Oct 21 '15 at 10:42
  • 2
    also *please* post actual code text instead of pictures, it makes local testing of your code a plea :( – Seki Oct 21 '15 at 10:43
  • You can use dependency walker to see the actual name of the function in your Dll. If it's been mangled, you can either put the mangled name in your PB code or change your Dll to not manage the name. See http://stackoverflow.com/questions/1467144/how-do-i-stop-name-mangling-of-my-dlls-exported-function for more – Slapout Oct 21 '15 at 13:00
  • Screenshots of source code are pretty much unsearchable. Please replace those images with more useful text information. – IInspectable Oct 21 '15 at 13:17

1 Answers1

5

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.

Seki
  • 11,135
  • 7
  • 46
  • 70