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
[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);
    };
}
[/code]

Ruby code
[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)
[/code]

Gives me an error saying undefined symbol.

···

--
Posted via http://www.ruby-forum.com/.

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.

···

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.

--
Aaron Patterson
http://tenderlovemaking.com/

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.

···

--
Posted via http://www.ruby-forum.com/\.

Albert Schlef 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.)

···

--
Posted via http://www.ruby-forum.com/\.

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

···

On Thu, Feb 11, 2010 at 2:37 AM, Albert Schlef <albertschlef@gmail.com> wrote:

Or create some 'extern "C"' wrapper functions for your methods.

Albert Schlef wrote:

Albert Schlef 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.

Attachments:
http://www.ruby-forum.com/attachment/4469/dll_debug1.JPG

···

--
Posted via http://www.ruby-forum.com/\.

Also the problem is that the MSVC++ compiler is munging them in a weird
fashion as "Add@MyMathFuncs@MathFuncs@@SANNN@Z" instead of just
"Add".How do I make my compiler build the dll in a proper way?

Please advice.

···

--
Posted via http://www.ruby-forum.com/.

Solved .Thank you all and specifically Albert Schlef

···

--
Posted via http://www.ruby-forum.com/.