好了,我已经解决了鼠标点击按钮事件通过JNI方法调用本地方法,但又有一个问题出现了,是我在java中定义的 public native long S2I_Link(char[] ip),我把一个char数组的IP地址通过JNI传到C++中,然后在C++中读出IP地址,但无论怎么做都是一段乱码,C++中存IP地址的数据类型是jchararray型的,我把它转换成了char *,因为函数中的参数要求为char *. 要怎样转换才能保证不是乱码呢?
#include "demo.h" #include <stdio.h> JNIEXPORT jlong JNICALL Java_demo_S2I_1Link (JNIEnv *env, jobject obj, jcharArray char_ip) { const char* szStr = (*env)->GetStringUTFChars(env,char_ip,0 ); }C:\Documents and Settings\Administrator\jbproject\ip_demo\demo.cpp(14) : error C2819: type 'JNIEnv_' does not have an overloaded member 'operator ->' c:\documents and sttings\administrator\jbproject\ip_demo\jni.h746) : see declaration of 'JNIEnv_' C:\Documents and Settings\Administrator\jbproject\ip_demo\demo.cpp(14) : error C2227: left of '->GetStringUTFChars' must point to class/struct/union Error executing cl.exe.这样也报错?
try{
System.loadLibrary("MyHello1");
}catch(UnsatisfiedLinkError e){
System.out.println("Load Dll Failed!");
}
如果调用不了会输出load dll failed,一开始生成的dll会有这种情况,我干脆把dll拷到了system32目录,然后用regsvr32命令注册了dll,好像把dll文件放在java程序目录中也不行.
在applet中就会出错,到底是怎么回事啊?各位救命啊.....
import java.awt.event.*;
import java.applet.*;
public class demo extends Applet {
protected TextField IpText;
protected Button ip_button;
protected String string_ip;
boolean isStandalone = false;
public native void greet(int i);
/**Get a parameter value*/
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
static{
try{
System.loadLibrary("MyHello1");
}catch(UnsatisfiedLinkError e){
System.out.println("Load Dll Failed!");
}
}
/**Construct the applet*/
public demo() {
}
/**Initialize the applet*/
public void init() {
try {
jbInit();
demo ip =new demo();
ip.greet(10);
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
this.setSize(new Dimension(400,300));
}
/**Get Applet information*/
public String getAppletInfo() {
return "Applet Information";
}
/**Get parameter info*/
public String[][] getParameterInfo() {
return null;
} //static initializer for setting look & feel
static {
try {
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch(Exception e) {
}
}
}
这个是我applet的源码,但是首先一点可以肯定,dll是加载成功的.只是不能通过JNI方法来调用DLL中的方法.
sendbox(沙箱)我一点概念都没有,可能我只是一个Java的初学者,安全方面的书我会去看看的,但我现在急于要找到一种解决这个问题的方法.
要怎样转换才能保证不是乱码呢?
#include <stdio.h>
JNIEXPORT jlong JNICALL Java_demo_S2I_1Link
(JNIEnv *env, jobject obj, jcharArray char_ip)
{
const char* szStr = (*env)->GetStringUTFChars(env,char_ip,0 );
}C:\Documents and Settings\Administrator\jbproject\ip_demo\demo.cpp(14) : error C2819: type 'JNIEnv_' does not have an overloaded member 'operator ->'
c:\documents and sttings\administrator\jbproject\ip_demo\jni.h746) : see declaration of 'JNIEnv_'
C:\Documents and Settings\Administrator\jbproject\ip_demo\demo.cpp(14) : error C2227: left of '->GetStringUTFChars' must point to class/struct/union
Error executing cl.exe.这样也报错?
#include "demo.h"
#include <stdio.h>
JNIEXPORT jlong JNICALL Java_demo_S2I_1Link
(JNIEnv *env, jobject obj, jcharArray char_ip)
{
const char* szStr = env->GetStringUTFChars(char_ip,0);
}
我把格式改成这样就正常了,谢谢大家给我的提示.