# Newbie: Calling dll functions from Ruby for a customer dll

**URL:** <https://rubytalk.org/t/newbie-calling-dll-functions-from-ruby-for-a-customer-dll/64197>\
**Category:** ruby-talk\
**Created:** [23 November 2011 11:41 UTC](https://rubytalk.org/t/newbie-calling-dll-functions-from-ruby-for-a-customer-dll/64197 "2011-11-23T11:41:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Omran\_N](https://avatars.discourse-cdn.com/v4/letter/o/bc79bd/32.png) [@Omran\_N](https://rubytalk.org/u/Omran_N)\
**Post date:** [23 November 2011 11:41 UTC](https://rubytalk.org/t/newbie-calling-dll-functions-from-ruby-for-a-customer-dll/64197/1 "2011-11-23T11:41:28Z")

</div>

Hello,

I have searched to forum previosly but have found little in the way on  
concrete examples. This might just be my inexperience hence the 'Newbie'  
in the title. I want to introduce Ruby into my Python addicted office.  
One way I would like to acheive that is by converting some of our useful  
scripts to Ruby.

The script essentially calls a dll which was created internally long ago  
by our engineers. It controlls a peice of hardware and also quesries it  
based on an API which we provide. If I can quickly convert it to Ruby it  
would help me to prove its worth. Please have patience with me if I am  
being a bit thick here. Here is the initial crux of the code in Python  
which I want to Rubify:

> **···**
>
> -------------------------------------------------------------------------
> 
> import os  
> import sys  
> import re  
> import time  
> from ctypes import \*  
> from datetime import datetime  
> from threading import Thread
> 
> #Open a file for logging  
> AutomationLogsPath = os.getcwd()
> 
> #Open a file for logging  
> currentTime = time.strftime('%d.%m.%y\_%H\_%M\_%S')  
> filename = AutomationLogsPath + "\\WCDMA\_DATACALL" + currentTime +  
> ".log"  
> fp = open(filename,"a")
> 
> #Change the current working directory to the one containing dlls.  
> os.chdir("C:\\Program Files\\Aeroflex\\7100\\Remote Control\\Bin")
> 
> try:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h\_rrcApi = windll.LoadLibrary('afRrcApiInterfaceDll.Dll')  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h\_wcdmaApi = windll.LoadLibrary('afWCDMAApiInterfaceDll.dll')  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h\_ControlLib = windll.LoadLibrary('afApplicationControlDll.Dll')  
> except WindowsError:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time = datetime.now().isoformat()  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fp.write(time + "\tLoadLibrary failed to find the dll!\n")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "LoadLibrary failed to find the dll!\n"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fp.close()  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(-1)  
> -------------------------------------------------------------------------
> 
> Thank you for reading this.
> 
> Imran
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Omran\_N](https://avatars.discourse-cdn.com/v4/letter/o/bc79bd/32.png) [@Omran\_N](https://rubytalk.org/u/Omran_N)\
**Post date:** [23 November 2011 14:39 UTC](https://rubytalk.org/t/newbie-calling-dll-functions-from-ruby-for-a-customer-dll/64197/2 "2011-11-23T14:39:56Z")

</div>

An example of calling a method would then be:

ret = h\_rrcLib.afRRCLibraryDll\_RunTestScript ( "REGISTER",  
len("REGISTER"), "7100\_system\_param.xml", len("7100\_system\_param.xml"))

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

---

<div class="post-metadata">

**Author:** ![Chuck\_Remes](https://avatars.discourse-cdn.com/v4/letter/c/3be4f8/32.png) [@Chuck\_Remes](https://rubytalk.org/u/Chuck_Remes)\
**Post date:** [23 November 2011 16:46 UTC](https://rubytalk.org/t/newbie-calling-dll-functions-from-ruby-for-a-customer-dll/64197/3 "2011-11-23T16:46:27Z")

</div>

I recommend taking a look at the win32-utils project. Don't worry too much that there haven't been a lot of updates since 2009; that's probably because the sub-project "just work."

[http://rubyforge.org/projects/win32utils/](http://rubyforge.org/projects/win32utils/)

There is probably some code here that can help you achieve your goal.

cr

> **···**
>
> On Nov 23, 2011, at 8:39 AM, Omran N. wrote:
> 
> > An example of calling a method would then be:
> > 
> > ret = h\_rrcLib.afRRCLibraryDll\_RunTestScript ( "REGISTER",  
> > len("REGISTER"), "7100\_system\_param.xml", len("7100\_system\_param.xml"))

---

<div class="post-metadata">

**Author:** ![Omran\_N](https://avatars.discourse-cdn.com/v4/letter/o/bc79bd/32.png) [@Omran\_N](https://rubytalk.org/u/Omran_N)\
**Post date:** [23 November 2011 17:18 UTC](https://rubytalk.org/t/newbie-calling-dll-functions-from-ruby-for-a-customer-dll/64197/4 "2011-11-23T17:18:21Z")

</div>

Great, thank for your response. I'll start here. TC

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