// 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)