0

I am having trouble calling a C++ function from dll in VB.net project. I have tried with the simple examples shown below

For C++ dll

#include <cmath>
extern "C" __declspec(dllexport) double SquareRoot(double value)
{
    return pow(value, 0.5);
}

I build the dll and copy it to the VB.net folder

For VB.net project

Module Module1
    <Runtime.InteropServices.DllImport("DLL_Test.dll")> _
    Private Function SquareRoot(ByVal value As Double) As Double

    End Function

    Sub Main()
        MsgBox(SquareRoot(2))
    End Sub

End Module

I keep getting Additional information: Unable to find an entry point named 'SquareRoot' in DLL 'DLL_Test.dll'. When I run dumpbin.exe on DLL_Test.dll I get the following

File Type: DLL

  Summary

        1000 .data
        1000 .idata
        2000 .rdata
        1000 .reloc
        1000 .rsrc
        4000 .text
       10000 .textbss

I am not sure what I am missing, any ideas? Thanks in advance.

  • 1
    Did you use `/exports` when you ran `dumpbin` ? – Ben Voigt Oct 05 '15 at 20:54
  • Also, the global `pow` function comes from ``. `` provides `std::pow` instead. – Ben Voigt Oct 05 '15 at 20:54
  • Sounds like you might be running into name mangling on the C++ side. You can use Dependency Walker(http://www.dependencywalker.com/) to view the actual function name that the C++ code is exporting. Also, this question may help: http://stackoverflow.com/questions/1467144/how-do-i-stop-name-mangling-of-my-dlls-exported-function – Slapout Oct 05 '15 at 21:22

1 Answers1

1

Name mangling. extern "C" doesn't turn it off, it just changes the rules.

You also have a calling convention mismatch.

You can solve both at once via __stdcall keyword on the C++ function.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Thanks for you answer, but I have changed `extern "C" __declspec(dllexport) double SquareRoot(double value)` to `double __stdcall SquareRoot(double value)` but I am still getting the same error –  Oct 05 '15 at 21:46
  • I meant to combine that with the existing code, not take away the old annotations. – Ben Voigt Oct 05 '15 at 22:03
  • I am not sure I am following, sorry it has been a long day :) –  Oct 05 '15 at 22:04
  • `__stdcall` on the C++ side will still lead to a decorate name. Simplest is `__cdecl` on both sides to avoid decoration. – David Heffernan Oct 06 '15 at 12:50