Skip to content

Commit dac4007

Browse files
committed
增加 string 类型的接口
1 parent 7b5a91e commit dac4007

File tree

5 files changed

+31
-1
lines changed

5 files changed

+31
-1
lines changed

CppCallJni.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "com_durongze_jni_CallC.h"
22
#include <Windows.h>
33

4+
#include <stdio.h>
5+
46
#define JAVA_HOME "D:\\Program Files\\Java\\jre1.8.0_60"
57
#define JVM_DLL JAVA_HOME "\\bin\\server\\" "jvm.dll"
68

@@ -50,7 +52,12 @@ extern "C" {
5052
penv->ReleaseStringUTFChars(ns, 0);
5153
}
5254

53-
jobjectArray res = (jobjectArray)Java_com_durongze_jni_CallC_CInterface(penv, NULL, names, ages, heights, num);
55+
jobjectArray res = Java_com_durongze_jni_CallC_CInterface(penv, NULL, names, ages, heights, num);
56+
jstring func = penv->NewStringUTF("main");
57+
jstring retStr = Java_com_durongze_jni_CallC_CInterfaceString(penv, NULL, func);
58+
jboolean b = false;
59+
printf("retStr:%s\n", penv->GetStringUTFChars(retStr, &b));
60+
penv->ReleaseStringUTFChars(retStr, 0);
5461

5562
pvm->DestroyJavaVM();
5663
}

callc/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ cmake_minimum_required(VERSION 3.4.1)
33
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDYZ_DBG")
44
set(JAVA_HOME "$ENV{JAVA_HOME}")
55
file(GLOB C_LIBRARY_SRC *.cpp *.h)
6+
#list(REMOVE_ITEM C_LIBRARY_SRC main.cpp)
67

78
MESSAGE(STATUS "JAVA_HOME DIR: ${JAVA_HOME} ENV_JAVA_HOME DIR: $ENV{HOME}")
89

910
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/ ${JAVA_HOME}/include/ ${JAVA_HOME}/include/win32)
1011
add_library(CLibrary SHARED ${C_LIBRARY_SRC})
1112

13+
file(GLOB CPP_CALL_JNI_SRC ../*.cpp )
14+
add_executable(CppCallJni ${C_LIBRARY_SRC} ${CPP_CALL_JNI_SRC})

callc/CallC.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
public class CallC{
44
public native String[] CInterface(String[] name, int[] age, float[] height, int num);
55
public native int CInterfaceTest();
6+
public native String CInterfaceString(String func);
67
static {
78
System.loadLibrary("CLibrary");
89
}
@@ -27,5 +28,7 @@ public static void main(String[] args){
2728
}
2829
int ret = new CallC().CInterfaceTest();
2930
System.out.println("ret :" + ret);
31+
String retStr = new CallC().CInterfaceString(name[0]);
32+
System.out.println("retStr :" + retStr);
3033
}
3134
};

callc/com_durongze_jni_CallC.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,15 @@ JNIEXPORT jint JNICALL Java_com_durongze_jni_CallC_CInterfaceTest
113113
#endif
114114
}
115115

116+
JNIEXPORT jstring JNICALL Java_com_durongze_jni_CallC_CInterfaceString
117+
(JNIEnv *env, jobject, jstring name)
118+
{
119+
jboolean b = false;
120+
printf("retStr:%s\n", env->GetStringUTFChars(name, &b));
121+
env->ReleaseStringUTFChars(name, 0);
122+
return env->NewStringUTF(__FUNCTION__);
123+
}
124+
116125
#ifdef __cplusplus
117126
#if __cplusplus
118127
}

callc/com_durongze_jni_CallC.h

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)