7

Problem:

I'd like to use in my Ruby program an algorithm that is coded in C and exposed through a DLL.

I would like to treat the algorithm as a black box that I can call from within Ruby -- simply pass in the required parameters and use the result.

Ruby (both 1.8.7 and 1.9.3) has the Win32API module which seems intended to make it quite easy to interface with dynamic libraries to do exactly what I'm after.

But the problem is that I can't seem to get the Win32API call to send back a string.

Details:

The third-party C function is CodeGen(). It take 6 parameters, including a source string, an arbitrary string to serve as encryption key, and, for simplicity, 4 numerical parameters, one signed int, one unsigned long, and two unsigned shorts. From these, CodeGen() implements a black-box algorithm to return a resulting string.

The C prototype for CodeGen() is:

const char *CodeGen(  int encryp_level, 
                     const char *source_str, const char *encryp_key,
                     unsigned long param_a, 
                     unsigned short param_b, unsigned short param_c
                  )

Note that both the input strings are constants, i.e. they are supplied to CodeGen() as strings -- so pointers to constant strings

The return value for CodeGen() is also a string, of fixed maximum length, so it will return a pointer.

My Question:

How do I go about setting up the call to CodeGen() and getting back the string it is supposed to generate?

My attempts:

The code below simply gives me integers as the return value, when I am expecting to obtain a string.

require 'Win32API'

codeGen = Win32API.new("encrypt.dll", "CodeGen", "ISSIII", "S")

ret_str = codeGen.Call(3, "foo", "bar", 0, 0, 0)

puts ret_str

However, instead of getting a string back, I get back an integer. Edit: Could this be a pointer?

Although I'm using Ruby 1.9.3 on Windows 7, 64-bit edition, I've also tested the above on Windows XP, 32-bits, and using Ruby 1.8.7, so I'm pretty sure it's something to do with my use of the Win32API itself.

Not sure whether the problem is any of these:

  • do the integers (3, 0, ...) need to be packed?
  • do I need to distinguish between short and long types?
  • am I not properly handling the return value?
  • if the return value is a pointer, how do I use this in Ruby?
  • something else?

Any insight would be much appreciated!

Assad Ebrahim
  • 6,234
  • 8
  • 42
  • 68

1 Answers1

4

While I don't know why the Win32API approach did not work, I've found an easier solution using FFI to the problem of calling C functions from within Ruby or interfacing with DLLs.


Solution using FFI

Use FFI in Ruby for interfacing with DLLs, as follows:

  • (1) Install ffi (works with ruby 1.9.3, ymmv with previous versions)

    gem install ffi

  • (2) Create your own Ruby module to wrap the C function

require 'ffi'

module CodeGen                   # Ruby wrapper  (your choice)
  extend FFI::Library
  ffi_lib 'codegen'              # DLL name  (given)
  attach_function 
        :create_code,            # method name  (your choice)
        :CreateCodeShort3,       # DLL function name (given)
          [ :int, :string, :string, :uint, :ushort, :ushort], :string  
                          # specify C param / return value types 
end

ret_str = CodeGen.create_code(3, "foo", "bar", 0,0,0)

puts ret_str
  • (3) Run. Result gives a string as desired.

Done!

Assad Ebrahim
  • 6,234
  • 8
  • 42
  • 68
  • I have a similar issue. My returned char* pointer actually points to a UTF-16 text (it's not ASCII text like in your example). I cannot find a way to bring such string into Ruby – Gianluca Ghettini Mar 24 '15 at 14:25
  • @G_G: See if String#inspect works. So if your string is called ret_str, try puts ret_str.inspect That shoud show you whether the C function is bringing the string into Ruby. – Assad Ebrahim Mar 25 '15 at 20:48
  • If you built your own dll with something like msbuild, can you just point FFI to the full path of the DLL instead of in this example where we're using `user_32` ? – Schneems Aug 02 '16 at 01:59
  • @Schneems: should work. The example above used a custom dll (not user32). I ran the program in the same directory as the dll, but you should be able to point to a general directory. Just remember to properly escape the directory separators in the path using "\\". – Assad Ebrahim Aug 03 '16 at 19:52