vc java_VC实现java定义的接口的一些方法 | 学步园

用java的javah生成的c头文件/* 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

#include

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]=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);

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值