Passing C++ const char* (String from JAVA) to C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eldadno1
    New Member
    • Mar 2010
    • 1

    Passing C++ const char* (String from JAVA) to C#

    I'm actually a Java programmer, and I'm messing with JNI a bit,

    so I've managed to pass an int from java to c++ and to c# and back.
    no problems cause it gets serialized automatically.

    but when dealing with string - the object JNI passes is jstring which
    casts to char* but how to pass it to c#?

    Do I need to Marshal it on the c# side??? can someone post a bit of
    code example????

    my C++ code is like so :
    Code:
    ...
    JNIEXPORT jint JNICALL Java_jni_ExecuteString_executeString (JNIEnv *env, jobject, jstring strinput) {
           ExecuteStringC* myObj = new ExecuteStringC();
            const char *str;
    	int result;
    	str = env->GetStringUTFChars(strinput, 0);
            result = myObj->callExecuteString(str);
    	env->ReleaseStringUTFChars(strinput, str);  
    	return result;
    The ExecuteStringC class :
    Code:
    public __gc class ExecuteStringC
    {
    public:
    	CSharpExecuteString __gc *execObj; 
    	ExecuteStringC() {
    		execObj = new CSharpExecuteString();
    	}
    	int callExecuteString(const char* str) {
    		return execObj->executeString(str); 
      	}
    };
    and finally the C# part :
    Code:
      class CSharpExecuteString
        {
            public int executeString(string  stringToExec)
            {
                Console.WriteLine("C# Says : " + stringToExec);
                return 0;
            }
        }
    Since I'm executing this code from Java it fails but I can't tell where, is it in the first or 2nd classes (C++) ??? or maybe in the C# side.
    I do believe it's the lather, but if anyone can shed some info on the subject (posting some code will be nice...)


    10x in advance to all contributors :)
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    There are a lot of C++/C# interoperabilit y topics in MSDN complete with examples. Microsoft calls this Managed C++.

    Comment

    Working...