简体   繁体   English

类型的映射:JNA中的char **&

[英]Mapping of type: char**& in JNA

My client gave me dll with a couple of functions. 我的客户给了dll几个功能。 One of them is: 其中之一是:

int GetVersions(char* name, char** &pVersions);

It returns a number of versions for the given name and the array of strings with those versions. 它返回给定名称的多个版本以及具有这些版本的字符串数组。 Using JNA, I'm trying to write equivalent Java method in my interface: 使用JNA,我试图在接口中编写等效的Java方法:

int GetVersions(String name, String ll, ??? pVersions);

The problem is what type should be instead of ??? 问题是应该使用什么类型而不是??? ?

I was trying to put there PointerByReference and after method invocation I had: 我试图将PointerByReference放在那里,在方法调用之后,我得到了:

Pointer ptr = ptrRef.getValue();
String ppp = ptr.getStringarray(0);

but I got here Invalid memory access . 但是我到了这里Invalid memory access

Pointer ptr = ptrRef.getValue();
String ppp = ptr.getString(0, "UTF-8");

returns garbage. 返回垃圾。

Any idea how to solve it? 知道如何解决吗?

Thanks in advance! 提前致谢!

the char ** mapping with PointerByReference. 使用PointerByReference进行char **映射。 for example. 例如。

c code. C代码。

char **xs_directory(struct xs_handle *h, uint32_t t,const char *path, unsigned int *num); char ** xs_directory(struct xs_handle * h,uint32_t t,const char * path,unsigned int * num);

jna: JNA:

PointerByReference xs_directory(XenstoreLibrary.xs_handle h, int t, String path, IntBuffer num); PointerByReference xs_directory(XenstoreLibrary.xs_handle h,int t,字符串路径,IntBuffer num);

this a xenstore api get contents of a directory,the num is PointerByReference pointer count. 这是一个xenstore api获取目录的内容,num是PointerByReference指针计数。 i use this code to get the pointer value. 我使用此代码来获取指针值。

PointerByReference pbr = XenstoreLibrary.INSTANCE.xs_directory(xs_handle, tran, "/local/domain/0", intBuffer); PointerByReference pbr = XenstoreLibrary.INSTANCE.xs_directory(xs_handle,tran,“ / local / domain / 0”,intBuffer);

    int n = intBuffer.get();

    Pointer[] arrays = pbr.getPointer().getPointerArray(0,n);

    for (int i = 0; i < arrays.length; i++) {

        Pointer array = arrays[i];

        System.out.println(array.getString(0));

    }

sorry for my poor english. 对不起,我英语不好。

As far as I understand this works in C like this: 据我了解,这在C语言中的工作方式如下:

char** versions; // char* versions[];
char   name [] = "some_name";

int size = GetVersions(name, &versions);
// now versions[0] to versions[size-1] are string literals of available versions

char** can be interpreted as an array of string literals ( String[] ). char**可以解释为字符串文字数组( String[] )。 char**& is a reference to one, meaning that it can be set from inside function - and it's not available to use in pure C used by JNA (and JNI). char**&是对one的引用,这意味着可以从内部函数中进行设置-不适用于JNA(和JNI)使用的纯C语言。

I'd recommend wrapping it into C-linkable code - make sure it doesn't throw exceptions, use extern "C" and replaces all references with pointers. 我建议将其包装到C可链接的代码中-确保它不会引发异常,使用extern "C"并将所有引用替换为指针。 Eg something like 例如类似

extern "C" int CGetVersions(char* name, char*** pVersions) {
    return GetVersions(name, *pVersions);
}

Then you should be able to use PointerByReference to hold pVersions : 然后,您应该可以使用PointerByReference来保存pVersions

ptrRef;                                       // (void**)
ptrRef.getValue();                            // *(void**) ->(void*)
ptrRef.getValue().getStringArray(0,returned); // (char**) (void*) -> (char**) -> String[]

Basically C++ works different that C, and both JNA and JNI were designed to work with C, so you have to strip down everything that you wouldn't call in plain C. Perhaps here you can find some more information about how wrap your library with C-linkable functions. 基本上,C ++的工作原理与C不同,并且JNA和JNI均设计为可与C一起使用,因此您必须精简您在纯C语言中不会调用的所有内容。也许在这里,您可以找到有关如何包装库的更多信息C链接功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM