Using Ruby /DL to invoke function in windows dll


D

di vi

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);
};
}

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)

Gives me an error saying undefined symbol.
 
Ad

Advertisements

A

Aaron Patterson

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.
 
A

Albert Schlef

di said:
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.
 
A

Albert Schlef

Albert said:
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.)
 
Ad

Advertisements

Ad

Advertisements

M

Martin DeMello

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top