用java的javah生成的c头文件
C++源文件
java调用C++dll部分
/* DO NOT EDIT THIS FILE - it is machine generated */
#include "jni.h"
/* Header for class com_softeem_test_TestNative */
#ifndef _Included_com_softeem_test_TestNative
#define _Included_com_softeem_test_TestNative
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_softeem_test_TestNative
* Method: sayHello
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_com_softeem_test_TestNative_sayHello
(JNIEnv *, jobject, jstring);
/*
* Class: com_softeem_test_TestNative
* Method: getHello
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_softeem_test_TestNative_getHello
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
c的实现方法
// MYDLL.cpp: implementation of the CMYDLL class.
//
//
//#include "MYDLL.h"
#include <iostream>
#include <windows.h>
using namespace std;
#include "com_softeem_test_TestNative.h"
/**
第一个参数是虚拟机的环境指针
第二个参数为待转换的Java字符串定义
第三个参数是本地存储转换后字符串的内存块
第三个参数是内存块的大小
*/
int JStringToChar(JNIEnv *env, jstring str, LPTSTR desc, int desc_len)
{
int len = 0;
if(desc==NULL||str==NULL)
return -1;
wchar_t *w_buffer = new wchar_t[1024];
ZeroMemory(w_buffer,1024*sizeof(wchar_t));
wcscpy(w_buffer,env->GetStringChars(str,0));
env->ReleaseStringChars(str,w_buffer);
ZeroMemory(desc,desc_len);
len = WideCharToMultiByte(CP_ACP,0,w_buffer,1024,desc,desc_len,NULL,NULL);
if(len>0 && len<desc_len)
desc[len]=0;
delete[] w_buffer;
return strlen(desc);
}
/*
* Class: com_softeem_test_TestNative
* Method: sayHello
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_com_softeem_test_TestNative_sayHello
(JNIEnv *env, jobject, jstring str)
{
TCHAR t_name[128];
JStringToChar(env,str,t_name,sizeof(t_name));
// ::MessageBox(NULL,"Hello,JNI!","Hello,JNI!",MB_OK);
Sleep(5000);
printf("%s",t_name);
}
/*
* Class: com_softeem_test_TestNative
* Method: getHello
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_softeem_test_TestNative_getHello
(JNIEnv *env, jobject)
{
char str[]="Fuck the world";
return env->NewStringUTF(str);
}
JAVA调用C++dll的方法
c++头文件
com_softeem_interImpl_CInterImpl.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include "jni.h"
/* Header for class com_softeem_interImpl_CInterImpl */
#ifndef _Included_com_softeem_interImpl_CInterImpl
#define _Included_com_softeem_interImpl_CInterImpl
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_softeem_interImpl_CInterImpl
* Method: turnleft
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_softeem_interImpl_CInterImpl_turnleft
(JNIEnv *, jobject);
/*
* Class: com_softeem_interImpl_CInterImpl
* Method: turnright
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_softeem_interImpl_CInterImpl_turnright
(JNIEnv *, jobject);
/*
* Class: com_softeem_interImpl_CInterImpl
* Method: turnback
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_softeem_interImpl_CInterImpl_turnback
(JNIEnv *, jobject);
/*
* Class: com_softeem_interImpl_CInterImpl
* Method: walkahead
* Signature: (F)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_softeem_interImpl_CInterImpl_walkahead
(JNIEnv *, jobject, jfloat);
/*
* Class: com_softeem_interImpl_CInterImpl
* Method: walkbackon
* Signature: (F)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_softeem_interImpl_CInterImpl_walkbackon
(JNIEnv *, jobject, jfloat);
/*
* Class: com_softeem_interImpl_CInterImpl
* Method: stop
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_softeem_interImpl_CInterImpl_stop
(JNIEnv *, jobject);
/*
* Class: com_softeem_interImpl_CInterImpl
* Method: play
* Signature: (Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_softeem_interImpl_CInterImpl_play
(JNIEnv *, jobject, jstring);
/*
* Class: com_softeem_interImpl_CInterImpl
* Method: getsensorstate
* Signature: (I)Z
*/
JNIEXPORT jboolean JNICALL Java_com_softeem_interImpl_CInterImpl_getsensorstate
(JNIEnv *, jobject, jint);
/*
* Class: com_softeem_interImpl_CInterImpl
* Method: getrobotdirect
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_softeem_interImpl_CInterImpl_getrobotdirect
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
C++源文件
JNIEXPORT jstring JNICALL Java_com_softeem_interImpl_CInterImpl_turnleft
(JNIEnv *env, jobject jobj)
{
char str[]="Fuck the world";
printf("%s",str);
return env->NewStringUTF(str);
}
java调用C++dll部分
package com.softeem.interImpl;
public class CInterImpl {
public native String turnleft();
static{
System.loadLibrary("SimpleDll");
}
public static void main(String[] args) throws Exception {
CInterImpl aa=new CInterImpl();
String aaa=aa.turnleft();
System.out.println(aaa);
}
}