简体   繁体   中英

Mapping of type: char**& in JNA

My client gave me dll with a couple of functions. 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:

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:

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

but I got here 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. for example.

c code.

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

jna:

PointerByReference xs_directory(XenstoreLibrary.xs_handle h, int t, String path, IntBuffer num);

this a xenstore api get contents of a directory,the num is PointerByReference pointer count. i use this code to get the pointer value.

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:

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**& 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).

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. 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 :

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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