1

可能重复:
将数组作为指针+大小或范围传递给包装函数

我目前正在学习swig,并且正在使用 C api 来包装 stdio.h。所以,我的 wig 文件如下所示:

%module jstdio

%{
#include <stdio.h>
%}

FILE* fopen(const char* path, const char*  mode);
int fclose( FILE* fp);
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);

我的java类是:

public class CFile
   {
   private SWIGTYPE_p_JSTDIO ptr;
   public CFile(String path,String mode) throws IOException
         {
         this.ptr=jstdio.fopen(path,mode);
         if(this.ptr==null) ... //throw...
         }
   (...)
   public int write(byte[] array,int pos,int length) throws IOException
        {
        (... here I should call jstdio.fwrite )
        }
   public int read(byte[] array,int pos,int length) throws IOException
        {
        (... here I should call jstdio.fread )
        }
   (...)
   }

现在,对于读/写,告诉 swig 将我的 java 字节数组转换为 (void*) ptr 的正确方法是什么?我想我需要使用 %typemap 但我没有正确的使用方法?

4

0 回答 0