From: ptkwt@... (Phil Tomson) Date: 2004-01-13T13:51:38+09:00 Subject: Re: Ruby and Swig -- creating Pointers (typedef) In article , Phil Tomson wrote: >RubyQuestions@yahoo.com (RubyQuestions) wrote in message >news:<599e9746.0401121052.61a0520c@posting.google.com>... >> I have been using Swig for the first time since Lyle mentioned Phil >> Tomson's article in C/C++ Users Journal (http://www.cuj.com) >> >> My goal is to use my company's C/C++ API with Ruby. >> I think it would add a great deal of flexibility to my current >> programming. >> >> I let Swig automatically create the wrapper for me (using the "truly >> lazy way" interface...just pointing it to the header file.) >> >> Amazingly, everything is working. >> I'm able to load the shared library into Ruby. >> I can see all the methods, constants, etc... >> >> However, some of the methods I want to use require pointers. >> Sorry for my ignorance, but I have only programmed in Java and Ruby... >> so I don't have any true exposure to C/C++ or the concept of pointers. >> >> In the header file (API) I have are some typedef statments: >> >> /********************************/ >> /* */ >> /* Global Data Types */ >> /* */ >> /********************************/ >> typedef char CoolBoolean; >> typedef char* CoolString; >> typedef int CoolClientHandle; >> typedef int CoolTableHandle; >> >> >> >> Now I want use the following connect method. >> >> /****************************************************************/ >> /* */ >> /* Opening and Closing Connection */ >> /* */ >> /****************************************************************/ >> >> extern DllDecl CoolReturnCode CoolConnect >> PROTO((char *srvHostName, >> int serverNumber, >> CoolClientHandle *handle)); >> >> >> How do I create a pointer of type "CoolClientHandle" in Ruby? >> Am I thinking about this incorrectly? >> >> I have read over the pointer documentation, however I have been unable >> to make it work. >> >> Any advice would be appreciated. >> >> Thanks. > >You need to include the typedefs in the declaration portion of the .i file >(or you need to include the .h file where the typedef is defined). > >for example, let's say you have the typedef in the file foo.h: > >//foo.h >typedef int CoolClientHandle; >typedef int CoolTableHandle; >///end of file/// > A couple of corrections: >You need to include it in your .i file like: > >%{ >/*this get's put in your wrapper code*/ >#include "foo.h" >%} >/*now you've got to tell SWIG about it*/ >typedef int CoolClientHandle; >typedef int CoolTableHandle; //yeah, I know it seems redundant > It would be better if you just did: %include "foo.h" > > >You should be able to pass in an integer from the Ruby side, swig will take >care of the pointer referencing/dereferencing. > See Lyle's post on pointers being used as output parameters. Phil