# Help: SWIG & Ruby

**URL:** <https://www.ruby-forum.com/t/help-swig-ruby/81139>\
**Category:** Ruby\
**Created:** [January 4, 2007, 2:48pm UTC](https://www.ruby-forum.com/t/help-swig-ruby/81139 "2007-01-04T14:48:17Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![syepes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bc8723/32.png) [@syepes](https://www.ruby-forum.com/u/syepes)\
**Post date:** [January 4, 2007, 2:48pm UTC](https://www.ruby-forum.com/t/help-swig-ruby/81139/1 "2007-01-04T14:48:17Z")

</div>

Hi All,

I need some help with SWIG and Ruby, I am trying to use some C functions  
in ruby.  
I have just started with SWIG and have been reading the manual en posts  
around the net, but I have not been able to get this to work properly,  
here are some if the questions I have:

- When I run opc\_connect why isnâ€™t â€œconn\_idâ€ assigned any reference?

- Whatâ€™s wrong with the â€œ%apply int \*INOUTâ€¦â€, when I run opcif\_open,  
â€œif\_idâ€ does not get any reference?

- How do I work with the typedefs â€œtypedef void \* XXâ€ in ruby?, when I  
run opcdata\_create, the variable data should get some data or  
referenced?

- Is it necessary to create some custom typemaps to make this work?

Any help or example would be appreciated, thanks in advanced for the  
help.

## ruby Test.rb (OUTPUT)

TEST: opc\_connect  
RC: -20 / ID: \<\<\<-- ???  
TEST: opcif\_open  
TYPE: 4 / MODE: 24 / ID: \<\<\<-- ???  
RC: -21 / TYPE: 4 / MODE: 24 / ID: \<\<\<-- ???  
TEST: opcdata\_create  
RC: -21 / DATA: \<\<\<-- ???

## Test.rb

require ‘api’  
include Api

puts “TEST: opc\_connect\n”  
conn\_id = nil  
rc\_conn = opc\_connect(“opc\_adm”, “OpC\_adm”, conn\_id)  
puts “\tRC: #{rc\_conn} / ID: #{conn\_id}\n”

puts “TEST: opcif\_open\n”  
if\_type = 4  
if\_name = “SEBASTST”  
if\_mode = 24  
if\_id = nil

print “\tTYPE: #{if\_type} / MODE: #{if\_mode} / ID: #{if\_id}\n”  
rc\_open = opcif\_open(if\_type, if\_name, if\_mode, 0, if\_id)  
print “\tRC: #{rc\_open} / TYPE: #{if\_type} / MODE: #{if\_mode} / ID:  
#{if\_id}\n”

## puts “TEST: opcdata\_create\n” data = nil rc\_data = opcdata\_create(0 , data) puts “\tRC: #{rc\_data} / DATA: #{data}\n”

## Interface file:

%module api

%inline %{  
#include “api.h”  
%}

## %include typemaps.i %apply int \*INOUT { int \*interface\_id };

## api.h

#define OPCAPI  
#define OPCLIBAPI extern

typedef void \* opcdata;  
typedef void \* opcregcond;  
typedef void \* opc\_connection;  
typedef void \* opc\_iterator;  
typedef int pipe\_handle;  
typedef pipe\_handle \*p\_pipe\_handle;

OPCLIBAPI int OPCAPI opc\_connect (  
const char \*operator\_name,  
const char \*operator\_pwd,  
opc\_connection \*opc\_conn );

OPCLIBAPI int OPCAPI opcif\_open (  
int interface\_type,  
const char \*instance,  
int mode,  
int max\_entries,  
int \*interface\_id );

## OPCLIBAPI int OPCAPI opcdata\_create ( int data\_type, opcdata \*data );
