1

I want to pass string as a parameter in with my jni. but when I pass the string; the received string is just a meaning less characters. In fact I cant pass my desired string from java to my C# function.

what should I do?

Edit :

jin.java:

public static native void myFunc( String name);

jin.cs:

public static void java_framewindow_myFunc(int env,int object,string name)
{
messagebox.show(name);
}

and I send string "Hi" from java, but the string shown in message box is meaningless.

Edit2: here is my complete c# code :

using System;
using ObjectOrientedJNI; 
using System.Windows.Forms; 
namespace CSharpInJava { 
    public class NativeJavaMethods { 

        static void Java_FrameWindow_myFunc(int env, int obj, string name){
           messagebox.show(name);
        }
    }
} 

and here is my complete java code:

import java.awt.Canvas;
import java.awt.event.ComponentEvent; 
import java.awt.event.ComponentListener;
public class FrameWindow extends Canvas { 
    int ref = 0;
      //Called by JVM to create Canvas' Peer 
    public void buttom_clicked() { 
        myFunc("hi"); 
    }
     native int myFunc(String name);
} 

what am I missing?

5
  • 1
    Please provide source + values in debugger. Commented Jun 24, 2012 at 7:55
  • @user643540 I added the codes Commented Jun 24, 2012 at 8:35
  • Please provide the missing JNI code which glues these two pieces of code together. Commented Jun 24, 2012 at 8:36
  • 1
    I don't know C#, but it is good to know that Java stores string internally in UTF-16. Commented Jun 24, 2012 at 8:58
  • @houman001 tnx for your comment but I know that and in c++ and c we could use GetStringChars function but what should we do in c# to get that parameter? Commented Jun 24, 2012 at 9:02

1 Answer 1

1

I have not used JNI with C#, but with C/C++ your native code should receive a jstring type, instead of string as follows:

static void Java_FrameWindow_myFunc(int env, int obj, jstring name)

Typically, JNI documentation is poor (even more so with C# it seems) so turning to the JNI Specification is a good idea. Note that JNI uses modified UTF-8 strings.

EDIT:

EJP is correct, your whole signature is incorrect. See Native Method Arguments in the JNI Spec for more detail.

static void Java_FrameWindow_myFunc( 
     JNIEnv *env,        /* interface pointer */ 
     jobject obj,        /* "this" pointer */
     jstring s)          /* argument #1 */ 
Sign up to request clarification or add additional context in comments.

1 Comment

Same applies to the 'object' parameter. It isn't an 'int': in fact it isn't even an object, it's a jclass, as the method is static. OP appears to have just made the signature up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.