简体   繁体   中英

How do I return a String Array from a C# function to a C++ function calling it?

I'm working with JNI and I have .net library written in C# that I want to pass values from to Java, using C++ and JNI as an intermediary.

I've not worked with C++ in quite some time. This is what I have for my C# method that I need to pass the return value to the C++ method :

public string[] getSystemFingerPrint(){ //<---- This method is called from a C++ class.
    /* Code Body */
    return FingerPrint._fingerprint;
}

That method is being called from the native library I'm writing in C++. The only problem is, I have no idea how to translate the string array from .net into something readable by C++. This is the C++ method I have that would be responsible for returning the value called from the C# .net library :

FingerPrintC __gc *t;
FingerPrintC() {
    t = new FingerPrint();
}
.
.
.
char[] getSystemFingerPrint(){ //<----I know that's the wrong return value.
    return t->getSystemFingerPrint(); //<----What will .net return the string array as here?
}

So I guess the question is : What does .net return a string array as to C++ when C++ is calling a .net function?

EDIT 1 :

I was able to get a bit of a clue when I resolved some of the other bugs : When I tried to compile returning a value of char * it gave me this error :

error C2440: 'return' : cannot convert from 'System::String __gc * __gc[]' to 'char *'

That helps a little but when I tried to use that as the return value, well, it didn't like it very much... So now the question becomes : How do I make System::String __gc * __gc[] into a return type that C++ likes?

EDIT 2:

More progress : So the proper way to format the function header is System::String __gc *<FunctionName>()[] . Unfortunately C++ isn't very forgiving and it won't simply let you return that value as a jobjectArray. So my next step is going to be grabbing the values from the array and dropping them into a new jobjectArray...

EDIT 3:

So now I'm here :

JNIEXPORT jobjectArray JNICALL Java_RegistrationControl_GetSystemFingerPrint (JNIEnv *jn, jobject jobj){
    FingerPrintC* t = new FingerPrintC();
    System::String __gc * t2[] = t->GetSystemFingerPrint();
    jobjectArray ret =
        (jobjectArray)jn->NewObjectArray(
            6,
            jn->FindClass("java/lang/String"),
            jn->NewStringUTF("")
        );
    for (int c = 0; c < 6; c++) jn->SetObjectArrayElement(ret, c, jn->NewStringUTF(t2[c])); //<---- This line fails.
    return ret;
}

The error I get is : JNIEnv_::NewStringUTF' : cannot convert parameter 1 from 'System::String __gc *' to 'const char * So now I need to figure out how to convert System::String __gc * into const char * .

So after a long and arduous trek we have come to our answer. The resolution to be able to pass an array of strings from a C# function to Java via JNI is thus :

using namespace System::Runtime::InteropServices; //<-----NameSpace with a method we need...

JNIEXPORT jobjectArray JNICALL Java_RegistrationControl_GetSystemFingerPrint (JNIEnv *jn, jobject jobj){ //<----- The Native Function to be called by Java
    System::String __gc * t2[] = FingerPrint::GetSystemFingerPrint(); //<----- The method called from the C# library.
    jobjectArray ret = (jobjectArray)jn->NewObjectArray( //<-----Define the jobjectArray
        6, //<-----Must know exactly how many elements are in the array. This works for me.
        jn->FindClass("java/lang/String"), //<<-----Type of the array.
        jn->NewStringUTF("") //<-----Initialize each array value, I guess?
    );
    for (int c = 0; c < 6; c++){
        const char *i = (char*)(void*)Marshal::StringToHGlobalAnsi(t2[c]); //<-----Transform the "string" into something that the JNI method can tolerate.
        jn->SetObjectArrayElement(ret, c, jn->NewStringUTF(i)); //<-----jn->NewStringUTF(i) transforms the string into something Java can tolerate. This line transforms the transformed string and adds it to the array to be returned.
    }
    return ret;
}

I can only hope in the future someone comes across this and is able to make use of it.

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