簡體   English   中英

如何將字符串數組從C#函數返回到調用它的C ++函數?

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

我正在使用JNI,我有一個用C#編寫的.net庫,我希望使用C ++和JNI作為中介將值傳遞給Java。

我已經有一段時間沒有使用C ++了。 這是我需要將返回值傳遞給C ++方法的C#方法的功能:

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

從我用C ++編寫的本機庫中調用了該方法。 唯一的問題是,我不知道如何將.net中的字符串數組轉換為C ++可讀的內容。 這是我擁有的C ++方法,負責返回從C#.net庫調用的值:

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?
}

所以我想問題是:當C ++調用.net函數時,.net返回關於C ++的字符串數組是什么?

編輯1:

當我解決了其他一些錯誤時,我可以得到一些線索:當我嘗試編譯時返回char *的值時,它給了我這個錯誤:

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

這有一點幫助,但是當我嘗試使用它作為返回值時,它並不太喜歡...所以現在的問題變成了:如何將System::String __gc * __gc[]變成一個返回C ++喜歡的類型?

編輯2:

更大的進步:所以格式化函數頭的正確方法是System::String __gc *<FunctionName>()[] 不幸的是,C ++並不是很寬容,它不會簡單地讓您將該值作為jobjectArray返回。 因此,我的下一步將是從數組中獲取值並將其放入新的jobjectArray中...

編輯3:

所以現在我在這里:

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;
}

我得到的錯誤是: JNIEnv_::NewStringUTF' : cannot convert parameter 1 from 'System::String __gc *' to 'const char *所以現在我需要弄清楚如何將System::String __gc *轉換為const char *

因此,經過漫長而艱苦的跋涉,我們終於找到了答案。 因此,能夠通過JNI將字符串數組從C#函數傳遞給Java的分辨率為:

使用名稱空間System :: Runtime :: InteropServices; // <----- NameSpace以及我們需要的方法...

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;
}

我只能希望將來有人遇到這個問題並能夠加以利用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM