从 JNI 方法调用纯 C 函数
我正在研究 JNi 示例。我有一个 C 文件:
#include "test_lib.h"
#include <mobile_crypto.h>
#include <stdio.h>
int mainCrypto(void){
.
.
.
.
return 1 ;
}
现在我想从编写 JNI 方法的另一个 C 文件访问 mainCrypto() 方法:
#include "com_My_NativeLib.h"
#include "test_lib.h"
#include <stdio.h>
#include <jni.h>
JNIEXPORT jint JNICALL Java_com_My_NativeLib_crypt(JNIEnv * env, jobject obj){
int status = 0;
status = mainCrypto();
return status;
}
我想知道是否可以按照我的方式从另一个 C 文件调用纯 C 函数 mainCrypto()在这个例子中做的事情。我没有得到任何返回值,因此状态变量值没有得到更新。
提前致谢。
我遇到的一些错误: 调试/NDK_NativeLib(3640): [ 09-12 15:58:40.766 3640:0xe38 F//system/bin/app_process ] DEBUG/NDK_NativeLib(3640):检测到堆栈损坏:已中止 DEBUG/Zygote(33):进程 3640 由信号 (6) 终止 INFO/ActivityManager(41):进程 com.crypto(pid 3640) 已死亡。
I'm working on JNi example . I have a C file :
#include "test_lib.h"
#include <mobile_crypto.h>
#include <stdio.h>
int mainCrypto(void){
.
.
.
.
return 1 ;
}
Now i want to access the mainCrypto() method from another C file where my JNI methods are written :
#include "com_My_NativeLib.h"
#include "test_lib.h"
#include <stdio.h>
#include <jni.h>
JNIEXPORT jint JNICALL Java_com_My_NativeLib_crypt(JNIEnv * env, jobject obj){
int status = 0;
status = mainCrypto();
return status;
}
I want to know whether i can call pure C function mainCrypto() from another C file in the way i am doing in this example. I'm not getting any return value and therefore status variable value is not getting updated.
Thanks in advance.
Some errors that i'm getting :
DEBUG/NDK_NativeLib(3640): [ 09-12 15:58:40.766 3640:0xe38 F//system/bin/app_process ]
DEBUG/NDK_NativeLib(3640): stack corruption detected: aborted
DEBUG/Zygote(33): Process 3640 terminated by signal (6)
INFO/ActivityManager(41): Process com.crypto(pid 3640) has died.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建 h 文件,例如 maincrypto.h,其中包含以下文本
int mainCrypto(void);
在带有 Java_com_My_NativeLib_crypt 的 cpp 文件中添加#include“maincrypto.h”,仅此而已
create h file, for example maincrypto.h with follow text
int mainCrypto(void);
in your cpp file with Java_com_My_NativeLib_crypt add #include "maincrypto.h" and thats all
为什么您认为您可能无法从其他 C 函数调用
mainCrypto()
?很明显,你没有理由不能,但我认为你不会问这个问题,除非你有理由认为你不能。除非您遗漏了一些奇怪的细节,是的,您也可以从其他地方调用它。Why would you think you might not be able to call
mainCrypto()
from some other C function? It seems pretty obvious that there's no reason why you couldn't, however I don't think you would ask the question unless you had reason to think you couldn't. Barring some strange detail you're leaving out, yes, you can call it from other places too.