Forum: Ruby Help: SWIG & Ruby

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
7c7781f21f788c6e0b9a4e179217a33c?d=identicon&s=25 Sebastian YEPES (syepes)
on 2007-01-04 14:48
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

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

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

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: <<<-- ???

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:

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 };

#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 );
This topic is locked and can not be replied to.