title: How do you use Ruby/DL? Is this right?
body: <p>I am trying to write an interface between RSPEC (ruby flavoured BDD) and a Windows application.  The application itself is written in an obscure language, but it has a C API to provide access.  I've gone with Ruby/DL but am having difficulties getting even the most basic call to a DLL method to work.  Here is what I have so far, in a file called gt4r.rb:</p>
  
  <pre><code>require 'dl/import'
  
  module Gt4r
    extend DL::Importable
    dlload 'c:\\gtdev\\r321\\bin\\gtvapi'
  
    # GTD initialization/termination functions
    extern 'int GTD_init(char *[], char *, char *)'
    extern 'int GTD_initialize(char *, char *, char *)'
    extern 'int GTD_done(void)'
    extern 'int GTD_get_error_message(int, char **)'
  end
  </code></pre>
  
  <p>My reading so far suggests that this is all I need to get going, so I wrote up a RSPEC example:</p>
  
  <pre><code>require 'gt4r'
  
  @@test_environment = "INCLUDE=C:\\graphtalk\\env\\aiadev\\config\\aiadev.ini"
  @@normal_user = "BMCHARGUE"
  
  describe Gt4r do
    it 'initializes' do
        rv = Gt4r.gTD_initialize @@normal_user, @@normal_user, @@test_environment
        rv.should == 0
    end
  end
  </code></pre>
  
  <p>And when run...</p>
  
  <pre><code>C:\code\GraphTalk&gt;spec -fs -rgt4r gt4r_spec.rb
  
  Gt4r
  - initializes (FAILED - 1)
  
  1)
  'Gt4r initializes' FAILED
  expected: 0,
       got: 13 (using ==)
  ./gt4r_spec.rb:9:
  
  Finished in 0.031 seconds
  
  1 example, 1 failure
  </code></pre>
  
  <p>The return value (13) is an actual return code, meaning an error, but when I try to add the gTD_get_error_message call to my RSPEC, I can't get the parameters to work.</p>
  
  <p>Am I heading in the right direction and can anyone point to the next thing I can try?</p>
  
  <p>Thanks,
  Brett</p>
  
  <hr>
  
  <p>A follow up to this question, showing the part that fails when I try to get the error message from my target library:</p>
  
  <pre><code>require 'gt4r'
  
  @@test_environment = "INCLUDE=C:\\graphtalk\\env\\aiadev\\config\\aiadev.ini"
  @@normal_user = "BMCHARGUE"
  
  describe Gt4r do
    it 'initializes' do
        rv = Gt4r.gTD_initialize @@normal_user, @@normal_user, @@test_environment
        Gt4r.gTD_get_error_message rv, @msg
        @msg.should == ""
        rv.should == 0
    end
  end
  </code></pre>
  
  <p>I expect the error message to be returned in @msg, but when run I get the following:</p>
  
  <pre><code>Gt4r
  (eval):5: [BUG] Segmentation fault
  ruby 1.8.6 (2008-08-11) [i386-mswin32]
  
  
  This application has requested the Runtime to terminate it in an unusual way.
  Please contact the application's support team for more information.
  </code></pre>
  
  <p>And this if I use a symbol (:msg) instead:</p>
  
  <pre><code>C:\code\GraphTalk\gt4r_dl&gt;spec -fs -rgt4r gt4r_spec.rb
  
  Gt4r
  - initializes (ERROR - 1)
  
  1)
  NoMethodError in 'Gt4r initializes'
  undefined method `to_ptr' for :msg:Symbol
  (eval):5:in `call'
  (eval):5:in `gTD_get_error_message'
  ./gt4r_spec.rb:9:
  
  Finished in 0.046 seconds
  
  1 example, 1 failure
  </code></pre>
  
  <p>Clearly I am missing something about passing parameters between ruby and C, but what?</p>