我有以下的C函數:如何將jna的一個字節[]映射到void *緩衝區?
int read(int dev, void* buffer, unsigned int count)
這通常是在調用C這樣的:
read(data->dev, data->buffer, 32000);
數據是一個結構,有以下幾點:
typedef struct {
ssize_t dev;
char buffer[32000];
} DATA;
而且我有將其轉換爲java,以jna與以下內容:
public class Data{//not neccesary to extends of Structure, because is only used to package both variables together
public int dev;
public byte[] buffer;//in the constructor of the class set to 32000 elements
}
int read(int playdev, Buffer buffer, int count);
//clib is the class to connect with de C library
ByteBuffer bf = ByteBuffer.wrap(data.buffer);
clib.read(data.dev, bf , READ_SIZE);
當我做「clib.read」時,它給了我一個「java.lang.Error:無效的內存訪問」
任何想法如何經歷這個錯誤?我試過做一個: int vox_playstr_read(int playdev,指針緩衝區,int count);
與
ByteBuffer bf = ByteBuffer.wrap(data.buffer);
Pointer pbuf = Native.getDirectBufferPointer(bf);
clib.read(data.dev, pbuf, READ_SIZE);
,它給了我同樣的結果。
請問,任何想法,使其工作?
您可以直接傳遞'byte []',使用NIO緩衝區或使用JNA'Memory'。 JNA支持所有三個作爲緩衝區類型參數。 – technomage
請用您的*實際*代碼更新您的問題。你不指出你如何初始化'buf'或'data.buffer';那很可能是你錯誤所在的地方。 – technomage
我編輯了答案。使用「buf」,我的意思是「bf」,它是相同的,我創建緩衝區,並用它構建de指針。我已經直接嘗試了byte [],但沒有使用Memory,我不知道你怎麼說可以在這裏應用。 – Selvaya