# Using Ruby /DL to invoke function in windows dll

**URL:** <https://rubytalk.org/t/using-ruby-dl-to-invoke-function-in-windows-dll/57357>\
**Category:** ruby-talk\
**Created:** [10 February 2010 15:10 UTC](https://rubytalk.org/t/using-ruby-dl-to-invoke-function-in-windows-dll/57357 "2010-02-10T15:10:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![di\_vi](https://avatars.discourse-cdn.com/v4/letter/d/d26b3c/32.png) [@di\_vi](https://rubytalk.org/u/di_vi)\
**Post date:** [10 February 2010 15:10 UTC](https://rubytalk.org/t/using-ruby-dl-to-invoke-function-in-windows-dll/57357/1 "2010-02-10T15:10:20Z")

</div>

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  
{  
&nbsp;&nbsp;&nbsp;&nbsp;class MyMathFuncs  
&nbsp;&nbsp;&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;public:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Returns a + b  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;static \_\_declspec(dllexport) double Add(double a, double b);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Returns a - b  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;static \_\_declspec(dllexport) double Subtract(double a, double  
b);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Returns a \* b  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;static \_\_declspec(dllexport) double Multiply(double a, double  
b);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Returns a / b  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Throws DivideByZeroException if b is 0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;static \_\_declspec(dllexport) double Divide(double a, double b);  
&nbsp;&nbsp;&nbsp;&nbsp;};  
}  
[/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/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Aaron\_Patterson1](https://avatars.discourse-cdn.com/v4/letter/a/e0b2c6/32.png) [@Aaron\_Patterson1](https://rubytalk.org/u/Aaron_Patterson1)\
**Post date:** [10 February 2010 19:43 UTC](https://rubytalk.org/t/using-ruby-dl-to-invoke-function-in-windows-dll/57357/2 "2010-02-10T19:43:48Z")

</div>

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/](http://tenderlovemaking.com/)

---

<div class="post-metadata">

**Author:** ![Albert\_Schlef](https://avatars.discourse-cdn.com/v4/letter/a/839c29/32.png) [@Albert\_Schlef](https://rubytalk.org/u/Albert_Schlef)\
**Post date:** [10 February 2010 21:07 UTC](https://rubytalk.org/t/using-ruby-dl-to-invoke-function-in-windows-dll/57357/3 "2010-02-10T21:07:04Z")

</div>

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/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Albert\_Schlef](https://avatars.discourse-cdn.com/v4/letter/a/839c29/32.png) [@Albert\_Schlef](https://rubytalk.org/u/Albert_Schlef)\
**Post date:** [10 February 2010 21:11 UTC](https://rubytalk.org/t/using-ruby-dl-to-invoke-function-in-windows-dll/57357/4 "2010-02-10T21:11:07Z")

</div>

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/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://rubytalk.org/u/Martin_DeMello)\
**Post date:** [11 February 2010 07:54 UTC](https://rubytalk.org/t/using-ruby-dl-to-invoke-function-in-windows-dll/57357/5 "2010-02-11T07:54:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![di\_vi](https://avatars.discourse-cdn.com/v4/letter/d/d26b3c/32.png) [@di\_vi](https://rubytalk.org/u/di_vi)\
**Post date:** [11 February 2010 04:30 UTC](https://rubytalk.org/t/using-ruby-dl-to-invoke-function-in-windows-dll/57357/6 "2010-02-11T04:30:42Z")

</div>

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](http://www.ruby-forum.com/attachment/4469/dll_debug1.JPG)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![di\_vi](https://avatars.discourse-cdn.com/v4/letter/d/d26b3c/32.png) [@di\_vi](https://rubytalk.org/u/di_vi)\
**Post date:** [11 February 2010 04:34 UTC](https://rubytalk.org/t/using-ruby-dl-to-invoke-function-in-windows-dll/57357/7 "2010-02-11T04:34:45Z")

</div>

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/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![di\_vi](https://avatars.discourse-cdn.com/v4/letter/d/d26b3c/32.png) [@di\_vi](https://rubytalk.org/u/di_vi)\
**Post date:** [11 February 2010 06:12 UTC](https://rubytalk.org/t/using-ruby-dl-to-invoke-function-in-windows-dll/57357/8 "2010-02-11T06:12:52Z")

</div>

Solved .Thank you all and specifically Albert Schlef

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
