环境:MyEclipse或者eclipse
visual studio 2013
描述:向Native层传递一个类,并从Native层返回一个类。这个类的全限定名称是com.example2.Test。
Test类共有两个属性,分别是一个数组px,和一个字符串upper。通过调用mydll2.dll实现数组px中元素的从小到大排序,以及字符串upper中的字母全部变大写字母。
Test类:
package com.example2;
public class Test {
private int[]px;
private String upper;
public Test(){
}
public Test(int[] px,String upper) {
this.px = px;
this.upper = upper;
}
public int[] getPx() {
return px;
}
public void setPx(int[] px) {
this.px = px;
}
public String getUpper() {
return upper;
}
public void setUpper(String upper) {
this.upper = upper;
}
}
PxUper类:
package com.example2;
public class PxUper {
public native Test objectMethodTest(Test test);
public static void main(String[] args){
System.loadLibrary("com_example2_PxUper");
PxUper pxUper = new PxUper();
Test test = new Test();
test.setPx(new int[]{10,4,9,6,2,20,7,6,2,20,7});
test.setUpper("zhsafwetggdsgdffssdgsd");
Test test1 = pxUper.objectMethodTest(test);
System.out.println(test.getUpper());
System.out.println(test1.getUpper());
for(int i=0; i<test.getPx().length; i++){
System.out.print(test.getPx()[i] +"、");
}
System.out.println();
for(int i=0; i<test1.getPx().length; i++){
System.out.print(test1.getPx()[i] +"、");
}
}
}
com_example2_PxUper.dll的h文件:
#include <jni.h>
#ifndef _Included_com_example2_PxUper
#define _Included_com_example2_PxUper
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT jobject JNICALL Java_com_example2_PxUper_objectMethodTest
(JNIEnv *, jobject, jobject);
#ifdef __cplusplus
}
#endif
#endif
com_example2_PxUper.dll的cpp文件:
// com_example2_PxUper.cpp : 定义 DLL 应用程序的导出函数。
#include "stdafx.h"
#include "com_example2_PxUper.h"
#include <jni.h>
#include <stdio.h>
#include <Windows.h>
#include <iostream>
using namespace std;
typedef int* (*fPx)(int*,int);
typedef char* (*fUp)(char*);
JNIEXPORT jobject JNICALL Java_com_example2_PxUper_objectMethodTest
(JNIEnv *env,jobject obj,jobject obj1){
HMODULE hModule = LoadLibrary(L"mydll2.dll");
if (!hModule){
cout << "Error!" << endl;
}
else{
cout << "Success!" << endl;
}
jclass ob = env->GetObjectClass(obj1);
jfieldID pxFieldID = env->GetFieldID(ob,"px","[I");
jfieldID upperFieldID = env->GetFieldID(ob,"upper","Ljava/lang/String;");
jintArray px = (jintArray)env->GetObjectField(obj1, pxFieldID);
jsize px_len = env->GetArrayLength(px);
jint *p = new jint[px_len];
int *p1 = new int[px_len];
env->GetIntArrayRegion(px, 0, px_len, p);
for (int i = 0; i < px_len; i++)
{
p1[i] = p[i];
}
fPx ppx = (fPx)GetProcAddress(hModule,"Px");
int *ppx1 = ppx(p1, px_len);
jint *ppx2 = new jint[px_len];
for (int i = 0; i < px_len; i++)
{
ppx2[i] = ppx1[i];
}
jintArray pxx = env->NewIntArray(px_len);
env->SetIntArrayRegion(pxx, 0, px_len, ppx2);
jstring upper = (jstring)env->GetObjectField(obj1, upperFieldID);
const char * upp =env->GetStringUTFChars(upper,NULL);
char u[128];
strcpy(u,upp);
env->ReleaseStringUTFChars(upper,upp);
fUp uup = (fUp)GetProcAddress(hModule,"Up");
const char * uppp = uup(u);
jstring upper11;
upper11 = env->NewStringUTF(uppp);
jclass objClass = env->FindClass("Lcom/example2/Test;");
jmethodID constrocMID = env->GetMethodID(objClass, "<init>","([ILjava/lang/String;)V");
jobject reobj = env->NewObject(objClass, constrocMID, pxx, upper11);
return reobj;
}
mydll2.dll的h文件:
#ifdef MYDLL2_EXPORTS
#define MYDLL2_API __declspec(dllexport)
#else
#define MYDLL2_API __declspec(dllimport)
#endif
extern "C" MYDLL2_API int* Px(int*,int);
extern "C" MYDLL2_API char* Up(char*);
mydll2.dll的cpp文件:
// mydll2.cpp : 定义 DLL 应用程序的导出函数。
#include "stdafx.h"
#include <iostream>
#include "mydll2.h"
extern "C" MYDLL2_API int* Px(int *result,int size){
static int *r =new int[size];
int i, j, temp;
for (i = 0; i < size; i++)
{
r[i] = result[i];
}
for (i = 0; i < size; i++)
{
for (j = i + 1; j < size; j++)
{
if (r[i]>r[j])
{
temp = r[i];
r[i] = r[j];
r[j] = temp;
}
}
}
return r;
}
extern "C" MYDLL2_API char* Up(char *result){
char cap[128];
strcpy(cap, result);
int i = 0;
for (i = 0; i < strlen(cap); i++){
cap[i] = toupper(cap[i]);
}
char* c = (char*)calloc(128,sizeof(char*));
strcpy(c, cap);
return c;
}
代码下载地址:http://download.csdn.net/detail/bide_tootight/9862707
详情参照:许你一城个人博客