******jni高手请进,关于在jni中调用c/c++编写的dll的问题

java悠悠 2004-05-12 05:14:54
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菜鸟,有什么不对的地方请尽管说,我乐于接受:)).
请帮帮忙,急用,小弟在此多谢各位计划回贴的大虾了!!!

只要能解决问题,分绝对不是问题。
...全文
139 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
bigcrazy 2004-05-12
  • 打赏
  • 举报
回复
你看看JAVA核心技术这本书吧。里面把怎么调用DLL说得很详细。
java悠悠 2004-05-12
  • 打赏
  • 举报
回复
不好意思,能说详细一点吗?
错误提示中的“1 unresolved externals”指的是什么呢?
为什么说“_dostr”是“unresolved external symbol ”呢?
那位大虾能帮我?
能写出解决问题的代码最好。
多谢啦 :)
fast_time 2004-05-12
  • 打赏
  • 举报
回复
你取得了dll的函数句柄,但是你调用的是什么??
还有,你没有进行const char*的转换

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧