******jni高手请进,关于在jni中调用c/c++编写的dll的问题
jni高手请进,关于在jni中调用c/c++编写的dll的问题
就是在jni生成dll文件所需的c源代码中调用已有的用vc编写的dll.
在要调用的c代码中使用已有的dll的接口函数。
具体代码如下:
java源文件:
yan.java:
import java.io.*;
import java.lang.*;
public class yan{
public String st="wo shi shui";
public char[] str1;
public char[] result1;
public int len1;
public native void Newtry(char str[],char result[],int len);
static { System.loadLibrary("xingxing"); }
public void convert(){
Newtry(str1,result1,len1);//Newtry()功能是将几个中文字符放入result1[]中
System.out.println("shuchu: "+result1);
}
}
用javah生成的头文件yan.h如下:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class yan */
#ifndef _Included_yan
#define _Included_yan
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: yan
* Method: fnNewtry
* Signature: ([C[CI)V
*/
JNIEXPORT void JNICALL Java_yan_Newtry
(JNIEnv *, jobject, jcharArray, jcharArray, jint);
#ifdef __cplusplus
}
#endif
#endif
接下来是我现在已有的一个用vc做好的dll文件:yy.dll
其头文件insun.h如下:
int __declspec(dllexport) dostr(char sz1[],char sz2[],int icount);
源代码insun.cpp如下:
#include <string.h>
int __declspec(dllexport) dostr(char sz1[],char sz2[],int icount)
{
strcpy(sz2,"我 是 谁");
return 1;
}
它的接口函数就是dostr(char sz1[],char sz2[],int icount);
我编写的c源代码ceshi.c:
#include <jni.h>
#include "yan.h"
#include <stdio.h>
#include <windows.h>
#include "insun.h"
typedef UINT (CALLBACK* PRINT_DLL)(char *);
JNIEXPORT void JNICALL Java_yan_Newtry
(JNIEnv *env, jobject this, jcharArray str1, jcharArray result1, jint len1)
{
HINSTANCE hDLL;
PRINT_DLL Print_Dll;
hDLL =LoadLibrary("yy");//你要调用的dll
Print_Dll =(PRINT_DLL)GetProcAddress(hDLL,"dostr");//调用dll里的方法名
if (!Print_Dll)
{
// handle the error
FreeLibrary(hDLL);
printf("dll link failed\n");
}
else
{
const char* sz2;
const char* sz1;
sz2=(*env)->GetCharArrayElements(env,result1,NULL);
sz1=(*env)->GetCharArrayElements(env,str1,NULL);
dostr(sz1,sz2,len1);
}
}
我看过几位大虾的贴子,和我的问题相似,我也照猫画虎的编写了c源文件,但存在很多问题。
我输入命令:
cl -Ic:\jdk1.4\include -Ic:\jdk1.4\include\win32 -LD ceshi.c yy.lib -Fexingxing.dll
错误如下:
ceshi.c
ceshi,c(27):warning c4133:'=':incompatible types - from 'unsigned short *' to 'const char *'
ceshi,c(28):warning c4133:'=':incompatible types - from 'unsigned short *' to 'const char *'
ceshi,c(29):warning c4090:'function':different 'const' qualifiers
ceshi,c(29):warning c4024:'dostr':different types for formal and actualparameter 1
ceshi,c(29):warning c4090:'function':different 'const' qualifiers
ceshi,c(29):warning c4024:'dostr':different types for formal and actualparameter 2
Microsoft <R> Incremental Linker Version 6.00.8168
Copyright <c> Microsoft Crop 1992-1998. All rights reserved.
/dll
/implib:xingxing.lib
/out:xingxing.dll
ceshi.obj
yy.lib
Creating Library xingxing.lib and object xingxing.exp
ceshi.obj:error LNK2001:unresolved external symbol _dostr
xingxing.dll:fatal error LNK1120:1 unresolved externals
(本人属于java菜鸟,有什么不对的地方请尽管说,我乐于接受:)).
请帮帮忙,急用,小弟在此多谢各位计划回贴的大虾了!!!
只要能解决问题,分绝对不是问题。