Skip to content

ffi-swig generating a :string instead of a :pointer #4

@stepheneb

Description

@stepheneb

See: http://groups.google.com/group/ruby-ffi/browse_thread/thread/98eaf51ab0386347

I have a C++ function that takes a pointer to a buffer where the function stores a C-string.

This is what I wrote and got working.

attach_function :GoIO_GetNthAvailableDeviceName, [:pointer, :int, :int, :int, :int], :int

However if I use ffi-swig it generates a :string as the first parameter.

attach_function :GoIO_GetNthAvailableDeviceName, [ :string, :int, :int, :int, :int ], :int

Is there anyway this could work? The function needs an address to a location to store a C-string.

Here's the C++ function:

GOIO_DLL_INTERFACE_DECL gtype_int32 GoIO_GetNthAvailableDeviceName(
  char *pBuf,            // [out] ptr to buffer to store device name string.
  gtype_int32 bufSize,   // [in] number of bytes in buffer pointed to by pBuf.
                         // Strlen(pBuf) < bufSize, because the string is NULL terminated.
  gtype_int32 vendorId,  // [in] USB vendor id
  gtype_int32 productId, //[in] USB product id
  gtype_int32 N);        //[in] index into list of known devices, 0 => first device in list.

Looking more closely at ffi-swig it's type_spec.rb does expect:

char *string;

to be turned into a :string.

I'm not much of a C programmer but is that correct? It seems in this case it should be a pointer.

Types like these are correctly turned into :string

const char *string;

The choice about turning a pointer into a string appears to be made in ffi-swig at lib/generator/type.rb:74:

def pointer
  if @declaration.is_pointer? or @is_pointer > 0
    @is_pointer += 1
    if @full_decl.scan(/^p\.(.+)/).flatten[0]
      ffi_type_from(@full_decl.scan(/^p\.(.+)/).flatten[0])
    elsif @full_decl == 'char' and @is_pointer == 2
      ':string'
    else
      ':pointer'
    end
  end       
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions