Using Ruby /DL to invoke function in windows dll

Hi,

I have a C++ code converted into a windows Dll.I want to use Ruby /DL to
invoke the functions in the DLL.

Here are the function prototypes:
C++ code

// MathFuncsDll.h

namespace MathFuncs
{
    class MyMathFuncs
    {
    public:
        // Returns a + b
        static __declspec(dllexport) double Add(double a, double b);

        // Returns a - b
        static __declspec(dllexport) double Subtract(double a, double
b);

        // Returns a * b
        static __declspec(dllexport) double Multiply(double a, double
b);

        // Returns a / b
        // Throws DivideByZeroException if b is 0
        static __declspec(dllexport) double Divide(double a, double b);
    };
}

Ruby code

require 'dl/import'
module MATHFUN
extend DL::Importable
dlload "math funcs.dll"
extern "double Add(double a, double b)"
#extern "int strlen(const char *)"
end
MATHFUN.Add(5,4)

Gives me an error saying undefined symbol.

On Thu, Feb 11, 2010 at 12:10:20AM +0900, di vi wrote:

Hi,

I have a C++ code converted into a windows Dll.I want to use Ruby /DL to
invoke the functions in the DLL.

C++ code? Unless you know how to munge your functions the same way your
compiler does, I don’t think you’re going to get very far.

di vi wrote:

Hi,

I have a C++ code converted into a windows Dll.
[…]
require ‘dl/import’
module MATHFUN
extend DL::Importable
dlload “math funcs.dll”
extern “double Add(double a, double b)”

There are some utility programs that let you look into a library and see
the symbols exposed. This way you see how the compiler mangled them. For
example, in linux you can do nm -D library.so.

Or create some ‘extern “C”’ wrapper functions for your methods.

Albert S. wrote:

There are some utility programs that let you look into a library and see
the symbols exposed. This way you see how the compiler mangled them.

(It goes without saying that if you distribute your source code,
somebody may use a compiler which mangles the names differently and your
ruby/dl won’t see them anymore.)

Albert S. wrote:

Albert S. wrote:

There are some utility programs that let you look into a library and see
the symbols exposed. This way you see how the compiler mangled them.

(It goes without saying that if you distribute your source code,
somebody may use a compiler which mangles the names differently and your
ruby/dl won’t see them anymore.)

I used PE explorer and saw the dill in disassembler.Attached is the
picture showing its names.Please advice.

Solved .Thank you all and specifically Albert S.

Also the problem is that the MSVC++ compiler is munging them in a weird
fashion as “[email protected]@[email protected]@SANNN@Z” instead of just
“Add”.How do I make my compiler build the dll in a proper way?

Please advice.

On Thu, Feb 11, 2010 at 2:37 AM, Albert S. [email protected]
wrote:

Or create some ‘extern “C”’ wrapper functions for your methods.

This is the right thing to do, and will save you headaches in the long
run, even if inspecting the DLL for symbols is quicker and easier
right now.

martin

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs