java 调用jni方法_Java JNI 本地方法调用Java对象方法

// c 代码

///

/**************************************

Util.h

**************************************/

#ifndef ROB_UTIL_H

#define ROB_UTIL_H

void printInt(int i, LPCSTR title)

{

LPSTR m = (LPSTR)malloc(10);

itoa((int)i, m, 10);

MessageBox(NULL, m, title,MB_OK);

free(m);

}

BOOL prints(PVOID msg, LPCSTR title)

{

LPSTR m = (LPSTR)malloc(10);

itoa((int)msg, m, 10);

MessageBox(NULL, m, title,MB_OK);

free(m);

if(msg == NULL)

return FALSE;

return TRUE;

}

#endif

/*******************************

test_jni_WinMsgBox.h

*******************************/

/* DO NOT EDIT THIS FILE - it is machine generated */

#include

/* Header for class test_jni_WinMsgBox */

#ifndef _Included_test_jni_WinMsgBox

#define _Included_test_jni_WinMsgBox

#ifdef __cplusplus

extern "C" {

#endif

/*

* Class:     test_jni_WinMsgBox

* Method:    showMsgBox

* Signature: (Ljava/lang/String;)V

*/

JNIEXPORT void JNICALL Java_test_jni_WinMsgBox_showMsgBox

(JNIEnv *, jobject, jstring);

/* Class:     test_jni_WinMsgBox

* Method:    start

* Signature: ()V

*/

JNIEXPORT void JNICALL Java_test_jni_WinMsgBox_start

(JNIEnv *, jobject);

#ifdef __cplusplus

}

#endif

#endif

/*******************************

WinMsgDll.cpp

*******************************/

#include "windows.h"

#include "test_jni_WinMsgBox.h"

#include "Util.h"

static jobject myObj;

static JNIEnv *myEnv;

static LPBYTE buffer;

void clean()

{

if(buffer != NULL)

free(buffer);

}

void init()

{

buffer = (LPBYTE)malloc(261);

}

void power(IN BYTE option)

{

clean();

init();

}

void transmit(IN LPBYTE inPutBuffer, IN int inPutLen,

OUT LPBYTE outPutBuffer, OUT int &outPutLen)

{

jbyteArray apdu = myEnv->NewByteArray(261);

// jbyteArray _apdu = myEnv->NewByteArray(261);

myEnv->SetByteArrayRegion(apdu, 0, inPutLen, (jbyte*)inPutBuffer);

jclass myClass = myEnv->GetObjectClass(myObj);

jmethodID mid = myEnv->GetMethodID(myClass, "processAPDU", "([BI[B)I");

outPutLen = myEnv->CallIntMethod(myObj, mid, apdu, inPutLen, apdu);

jboolean a = 1;

jbyte* response = myEnv->GetByteArrayElements(apdu, &a);

memcpy(outPutBuffer, response, outPutLen);

//释放

myEnv->ReleaseByteArrayElements(apdu, response, JNI_ABORT);

}

void testSend()

{

buffer[0] = 0x20;

buffer[1] = 0x11;

buffer[2] = 0x12;

int refLen;

transmit(buffer, 3, buffer, refLen);

for(int i = 0; i < refLen; i ++)

{

printInt(buffer[i], "response");

}

}

/*

* Class:     test_jni_WinMsgBox

* Method:    start

* Signature: ()V

*/

JNIEXPORT void JNICALL Java_test_jni_WinMsgBox_start

(JNIEnv* env, jobject obj)

{

myObj = env->NewGlobalRef(obj);

myEnv = env;

power(0);

}

/*

* Class:     test_jni_WinMsgBox

* Method:    showMsgBox

* Signature: (Ljava/lang/String;)V

*/

JNIEXPORT void JNICALL Java_test_jni_WinMsgBox_showMsgBox

(JNIEnv * env, jobject obj, jstring str)

{

testSend();

}

//

// Java代码

/

/**************************

Main.java

**************************/

package main;

import test.jni.*;

public class Main

{

public static void main(String[] args)

{

WinMsgBox msg = new WinMsgBox();

msg.start();

msg.showMsgBox("");

}

}

/***********************

WinMsgBox.java

***********************/

package test.jni;

public class WinMsgBox

{

public int processAPDU(byte[] apdu, int apduLen, byte[] response)

{

System.out.println("WinMsgBox process Apdu");

for(int i = 0; i < apduLen; i ++)

System.out.println(apdu[i]);

response[0] = (byte)0x90;

response[1] = 0x00;

return 2;

}

static

{

System.loadLibrary("WinMsgDll");

}

public native void showMsgBox(String str);

public native void start();

}

最后修改于 2006-09-22 02:33

阅读(?)评论(0)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值