dll里的方法必须在这里声明为本地的 dll是c++builder做的 public class GetData { public native String GetSerial(String S1); public native String GetIP(String S1); public native String GetMAC(String S1); public native String GetCorp(String S1); public native byte[] baGetCorp(String S1); static { System.loadLibrary("Antiscan"); } public String getcorp(String S1) { String bS = null; byte ba[] = baGetCorp(S1); //这里调用dll里的方法 if (ba != null) { bS = new String(ba); } return bS; } } 这个是我已经成功使用的例子
1.Write a Class to declare the native method: public class ShowMsg { static { System.loadLibrary("showlib"); }
public String getEncodeString(String sin) { byte[] bts = sin.getBytes(); return new String(getNativeEncodeBytes(bts,bts.length)); }
public native byte[] getNativeEncodeBytes(byte[] sinbts,int btlen); }
2.Use javah.exe to get a C++ head file: /* DO NOT EDIT THIS FILE - it is machine generated */ #include <c:\jdk\include\jni.h> /* Header for class ShowMsg */#ifndef _Included_ShowMsg #define _Included_ShowMsg #ifdef __cplusplus extern "C" { #endif /* * Class: ShowMsg * Method: getNativeEncodeBytes * Signature: ([BI)[B */ JNIEXPORT jbyteArray JNICALL Java_ShowMsg_getNativeEncodeBytes (JNIEnv *, jobject, jbyteArray, jint);#ifdef __cplusplus } #endif #endif
3.Write your implemention code in a C++ file: #include <C:\Microsoft\Microsoft Visual Studio\VC98\Include\CTYPE.H> #include <c:\jdk\include\jni.h> #include <c:\jdk\include\win32\jni_md.h> #include "c:\jni\ShowMsg.h" #include <C:\Microsoft\Microsoft Visual Studio\VC98\Include\stdio.h>#define BUFFSIZE 256JNIEXPORT jbyteArray JNICALL Java_ShowMsg_getNativeEncodeBytes(JNIEnv *env, jobject this, jbyteArray buf, jint bufsize) { char sIn[BUFFSIZE]; char sOut[BUFFSIZE];
(*env)->GetByteArrayRegion(env,(jbyteArray)buf,0,bufsize,sIn); sIn[bufsize] = 0; // This is my C++ api,you must NOT use!!!! :) pwd_encode(sIn,sOut); //printf("%s=>%s",sIn,sOut);
这个方法必须被静态初始化static { System.loadLibrary("LibFile"); }
好象在你调用这个dll的类里必须先声明dll里的方法 而且必须声明为静态的。具体要等到我回去查查原来的程序 周一再继续
public class GetData {
public native String GetSerial(String S1); public native String GetIP(String S1); public native String GetMAC(String S1); public native String GetCorp(String S1); public native byte[] baGetCorp(String S1); static {
System.loadLibrary("Antiscan");
} public String getcorp(String S1) {
String bS = null;
byte ba[] = baGetCorp(S1); //这里调用dll里的方法
if (ba != null) {
bS = new String(ba);
}
return bS;
}
}
这个是我已经成功使用的例子
public class ShowMsg
{
static
{
System.loadLibrary("showlib");
}
public String getEncodeString(String sin)
{
byte[] bts = sin.getBytes();
return new String(getNativeEncodeBytes(bts,bts.length));
}
public native byte[] getNativeEncodeBytes(byte[] sinbts,int btlen);
}
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <c:\jdk\include\jni.h>
/* Header for class ShowMsg */#ifndef _Included_ShowMsg
#define _Included_ShowMsg
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: ShowMsg
* Method: getNativeEncodeBytes
* Signature: ([BI)[B
*/
JNIEXPORT jbyteArray JNICALL Java_ShowMsg_getNativeEncodeBytes
(JNIEnv *, jobject, jbyteArray, jint);#ifdef __cplusplus
}
#endif
#endif
#include <C:\Microsoft\Microsoft Visual Studio\VC98\Include\CTYPE.H>
#include <c:\jdk\include\jni.h>
#include <c:\jdk\include\win32\jni_md.h>
#include "c:\jni\ShowMsg.h"
#include <C:\Microsoft\Microsoft Visual Studio\VC98\Include\stdio.h>#define BUFFSIZE 256JNIEXPORT jbyteArray JNICALL Java_ShowMsg_getNativeEncodeBytes(JNIEnv *env, jobject this, jbyteArray buf, jint bufsize)
{
char sIn[BUFFSIZE];
char sOut[BUFFSIZE];
(*env)->GetByteArrayRegion(env,(jbyteArray)buf,0,bufsize,sIn);
sIn[bufsize] = 0;
// This is my C++ api,you must NOT use!!!! :)
pwd_encode(sIn,sOut);
//printf("%s=>%s",sIn,sOut);
buf = (*env)->NewByteArray(env,bufsize*2);
(*env)->SetByteArrayRegion(env,buf,0,bufsize*2,sOut);
return buf;
}
不过我希望你能关注我的Kylin